|
|
@ -7,7 +7,6 @@
|
|
|
|
const float g = 7.8;
|
|
|
|
const float g = 7.8;
|
|
|
|
const int WINDOW_X = 600;
|
|
|
|
const int WINDOW_X = 600;
|
|
|
|
const int WINDOW_Y = 800;
|
|
|
|
const int WINDOW_Y = 800;
|
|
|
|
const int pipe_dist = 200;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float elap_time() {
|
|
|
|
float elap_time() {
|
|
|
|
static __int64 start = 0;
|
|
|
|
static __int64 start = 0;
|
|
|
@ -29,6 +28,8 @@ int main()
|
|
|
|
std::mt19937 rng(time(NULL));
|
|
|
|
std::mt19937 rng(time(NULL));
|
|
|
|
std::uniform_int_distribution<int> rgen(100, 400);
|
|
|
|
std::uniform_int_distribution<int> rgen(100, 400);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int pipe_dist = 200;
|
|
|
|
|
|
|
|
|
|
|
|
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "flappy");
|
|
|
|
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "flappy");
|
|
|
|
|
|
|
|
|
|
|
|
// Init flappy
|
|
|
|
// Init flappy
|
|
|
@ -45,7 +46,7 @@ int main()
|
|
|
|
sf::Texture background = sf::Texture(); background.loadFromFile("Assets\\sky.png");
|
|
|
|
sf::Texture background = sf::Texture(); background.loadFromFile("Assets\\sky.png");
|
|
|
|
sf::Sprite background_sprite = sf::Sprite(background); background_sprite.setPosition(0, 0); background_sprite.setScale(8, 8);
|
|
|
|
sf::Sprite background_sprite = sf::Sprite(background); background_sprite.setPosition(0, 0); background_sprite.setScale(8, 8);
|
|
|
|
sf::Texture land = sf::Texture(); land.loadFromFile("Assets\\land.png");
|
|
|
|
sf::Texture land = sf::Texture(); land.loadFromFile("Assets\\land.png");
|
|
|
|
sf::Sprite land_sprite = sf::Sprite(land); land_sprite.setPosition(0, WINDOW_Y - WINDOW_Y / 10); land_sprite.setScale(2, 2);
|
|
|
|
sf::Sprite land_sprite = sf::Sprite(land); land_sprite.setPosition(0, WINDOW_Y - WINDOW_Y / 10); land_sprite.setScale(3, 2);
|
|
|
|
sf::Texture pipe_up, pipe_down = sf::Texture(); pipe_down.loadFromFile("Assets\\pipe-down.png"); pipe_up.loadFromFile("Assets\\pipe-up.png");
|
|
|
|
sf::Texture pipe_up, pipe_down = sf::Texture(); pipe_down.loadFromFile("Assets\\pipe-down.png"); pipe_up.loadFromFile("Assets\\pipe-up.png");
|
|
|
|
sf::Sprite pipe_up_sprite = sf::Sprite(pipe_up); sf::Sprite pipe_down_sprite = sf::Sprite(pipe_down);
|
|
|
|
sf::Sprite pipe_up_sprite = sf::Sprite(pipe_up); sf::Sprite pipe_down_sprite = sf::Sprite(pipe_down);
|
|
|
|
sf::Texture pipe_shaft = sf::Texture(); pipe_shaft.loadFromFile("Assets\\pipe.png");
|
|
|
|
sf::Texture pipe_shaft = sf::Texture(); pipe_shaft.loadFromFile("Assets\\pipe.png");
|
|
|
@ -53,37 +54,41 @@ int main()
|
|
|
|
|
|
|
|
|
|
|
|
double momentum = 0;
|
|
|
|
double momentum = 0;
|
|
|
|
float step_size = 0.005f;
|
|
|
|
float step_size = 0.005f;
|
|
|
|
double frame_time = 0.0, instant_fps = 0.0, render_fps = 0.0, physics_fps = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
|
|
|
|
double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
|
|
|
|
int render_frame_count = 0, physics_frame_count = 0;
|
|
|
|
|
|
|
|
float speed = 250;
|
|
|
|
float speed = 250;
|
|
|
|
|
|
|
|
|
|
|
|
while (window.isOpen())
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sf::Event event;
|
|
|
|
sf::Event event; // Handle input
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
if (event.type == sf::Event::Closed) {
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
window.close();
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
if (event.type == sf::Event::MouseWheelScrolled)
|
|
|
|
if (event.type == sf::Event::MouseWheelScrolled) {
|
|
|
|
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
|
|
|
|
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
|
|
|
|
|
|
|
|
speed += event.mouseWheelScroll.delta * 20;
|
|
|
|
speed += event.mouseWheelScroll.delta * 20;
|
|
|
|
}
|
|
|
|
if(event.type == sf::Event::KeyPressed)
|
|
|
|
|
|
|
|
if (event.key.code == sf::Keyboard::Space)
|
|
|
|
|
|
|
|
momentum = -2;
|
|
|
|
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Right)
|
|
|
|
|
|
|
|
pipe_dist -= 10;
|
|
|
|
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Middle)
|
|
|
|
|
|
|
|
pipe_dist += 10;
|
|
|
|
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Left)
|
|
|
|
|
|
|
|
momentum = -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
elapsed_time = elap_time();
|
|
|
|
elapsed_time = elap_time(); // Handle time
|
|
|
|
delta_time = elapsed_time - current_time;
|
|
|
|
delta_time = elapsed_time - current_time;
|
|
|
|
current_time = elapsed_time;
|
|
|
|
current_time = elapsed_time;
|
|
|
|
if (delta_time > 0.02f)
|
|
|
|
if (delta_time > 0.02f)
|
|
|
|
delta_time = 0.02f;
|
|
|
|
delta_time = 0.02f;
|
|
|
|
accumulator_time += delta_time;
|
|
|
|
accumulator_time += delta_time;
|
|
|
|
|
|
|
|
|
|
|
|
while ((accumulator_time - step_size) >= step_size) {
|
|
|
|
while ((accumulator_time - step_size) >= step_size) { // While the frame has sim time, update
|
|
|
|
accumulator_time -= step_size;
|
|
|
|
accumulator_time -= step_size;
|
|
|
|
|
|
|
|
|
|
|
|
// Update logic
|
|
|
|
|
|
|
|
// Move pipes, background, floor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pipe_down_sprite.getPosition().x < -pipe_down_sprite.getGlobalBounds().width) {
|
|
|
|
if (pipe_down_sprite.getPosition().x < -pipe_down_sprite.getGlobalBounds().width) {
|
|
|
|
pipe_down_sprite.setPosition(WINDOW_X, rgen(rng));
|
|
|
|
pipe_down_sprite.setPosition(WINDOW_X, rgen(rng));
|
|
|
|
pipe_up_sprite.setPosition(WINDOW_X, pipe_down_sprite.getPosition().y + pipe_dist);
|
|
|
|
pipe_up_sprite.setPosition(WINDOW_X, pipe_down_sprite.getPosition().y + pipe_dist);
|
|
|
@ -92,58 +97,50 @@ int main()
|
|
|
|
pipe_up_sprite.setPosition(pipe_up_sprite.getPosition().x - step_size * speed, pipe_up_sprite.getPosition().y);
|
|
|
|
pipe_up_sprite.setPosition(pipe_up_sprite.getPosition().x - step_size * speed, pipe_up_sprite.getPosition().y);
|
|
|
|
pipe_down_sprite.setPosition(pipe_down_sprite.getPosition().x - step_size * speed, pipe_down_sprite.getPosition().y);
|
|
|
|
pipe_down_sprite.setPosition(pipe_down_sprite.getPosition().x - step_size * speed, pipe_down_sprite.getPosition().y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (background_sprite.getPosition().x + background_sprite.getGlobalBounds().width < WINDOW_X)
|
|
|
|
if (background_sprite.getPosition().x + background_sprite.getGlobalBounds().width < WINDOW_X) {
|
|
|
|
|
|
|
|
background_sprite.setPosition(0, 0);
|
|
|
|
background_sprite.setPosition(0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else {
|
|
|
|
|
|
|
|
background_sprite.setPosition(background_sprite.getPosition().x - step_size * (speed - 100), background_sprite.getPosition().y);
|
|
|
|
background_sprite.setPosition(background_sprite.getPosition().x - step_size * (speed - 100), background_sprite.getPosition().y);
|
|
|
|
}
|
|
|
|
if (land_sprite.getPosition().x + 10 + land_sprite.getGlobalBounds().width < WINDOW_X)
|
|
|
|
|
|
|
|
|
|
|
|
if (land_sprite.getPosition().x + 10 + land_sprite.getGlobalBounds().width < WINDOW_X) {
|
|
|
|
|
|
|
|
land_sprite.setPosition(14, land_sprite.getPosition().y);
|
|
|
|
land_sprite.setPosition(14, land_sprite.getPosition().y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else {
|
|
|
|
|
|
|
|
land_sprite.setPosition(land_sprite.getPosition().x - step_size * speed, land_sprite.getPosition().y);
|
|
|
|
land_sprite.setPosition(land_sprite.getPosition().x - step_size * speed, land_sprite.getPosition().y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check collisions
|
|
|
|
|
|
|
|
if (flappy.getPosition().y > land_sprite.getPosition().y) {
|
|
|
|
|
|
|
|
flappy.setPosition(WINDOW_X / 2, WINDOW_Y / 2);
|
|
|
|
|
|
|
|
momentum = 0;
|
|
|
|
|
|
|
|
std::cout << "dead\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sf::Vector2f f_pos = flappy.getPosition();
|
|
|
|
sf::Vector2f f_pos = flappy.getPosition();
|
|
|
|
|
|
|
|
sf::FloatRect f_rec = flappy.getGlobalBounds();
|
|
|
|
sf::Vector2f p_pos = pipe_up_sprite.getPosition();
|
|
|
|
sf::Vector2f p_pos = pipe_up_sprite.getPosition();
|
|
|
|
|
|
|
|
sf::FloatRect p_rec = pipe_up_sprite.getGlobalBounds();
|
|
|
|
|
|
|
|
|
|
|
|
if (((f_pos.x < p_pos.x + 26) && (f_pos.x > p_pos.x)) && ((f_pos.y > p_pos.y) || (f_pos.y < p_pos.y - pipe_dist))) {
|
|
|
|
// Check collisions
|
|
|
|
f_pos = sf::Vector2f(WINDOW_X / 2, WINDOW_Y / 2);
|
|
|
|
if (f_pos.y > land_sprite.getPosition().y) {
|
|
|
|
momentum = 0;
|
|
|
|
f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2);
|
|
|
|
std::cout << "dead\n ";
|
|
|
|
momentum = -2;
|
|
|
|
|
|
|
|
std::cout << "\ndead " + std::to_string(flappy.getPosition().y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get input
|
|
|
|
if (f_pos.x < p_pos.x + p_rec.width &&
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
|
|
|
f_pos.x + f_rec.width > p_pos.x &&
|
|
|
|
|
|
|
|
(f_pos.y > p_pos.y || f_pos.y < p_pos.y - pipe_dist) &&
|
|
|
|
|
|
|
|
(f_rec.height + f_pos.y > p_pos.y || f_rec.height + f_pos.y < p_pos.y + p_rec.height - pipe_dist)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2);
|
|
|
|
momentum = -2;
|
|
|
|
momentum = -2;
|
|
|
|
|
|
|
|
std::cout << "\ndead " + std::to_string(flappy.getPosition().y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
momentum += g * step_size;
|
|
|
|
|
|
|
|
|
|
|
|
momentum += g * step_size; // Impart gravity
|
|
|
|
f_pos.y += momentum;
|
|
|
|
f_pos.y += momentum;
|
|
|
|
flappy.setPosition(f_pos);
|
|
|
|
flappy.setPosition(f_pos);
|
|
|
|
|
|
|
|
|
|
|
|
physics_frame_count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window.draw(background_sprite); // Render
|
|
|
|
window.clear(sf::Color::Black);
|
|
|
|
|
|
|
|
window.draw(background_sprite);
|
|
|
|
|
|
|
|
window.draw(land_sprite);
|
|
|
|
window.draw(land_sprite);
|
|
|
|
window.draw(flappy);
|
|
|
|
window.draw(flappy);
|
|
|
|
window.draw(pipe_up_sprite);
|
|
|
|
window.draw(pipe_up_sprite);
|
|
|
|
window.draw(pipe_down_sprite);
|
|
|
|
window.draw(pipe_down_sprite);
|
|
|
|
|
|
|
|
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_up_sprite.getPosition());
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_up_sprite.getPosition()); // Render the bottom pipe
|
|
|
|
int y_pos = pipe_up_sprite.getPosition().y + pipe_up_sprite.getGlobalBounds().height;
|
|
|
|
int y_pos = pipe_up_sprite.getPosition().y + pipe_up_sprite.getGlobalBounds().height;
|
|
|
|
while (y_pos < WINDOW_Y) {
|
|
|
|
while (y_pos < WINDOW_Y) {
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_shaft_sprite.getPosition().x, y_pos);
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_shaft_sprite.getPosition().x, y_pos);
|
|
|
@ -151,7 +148,7 @@ int main()
|
|
|
|
window.draw(pipe_shaft_sprite);
|
|
|
|
window.draw(pipe_shaft_sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
y_pos = pipe_down_sprite.getPosition().y;
|
|
|
|
y_pos = pipe_down_sprite.getPosition().y; // Render the top pipe
|
|
|
|
while (y_pos > 0) {
|
|
|
|
while (y_pos > 0) {
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_shaft_sprite.getPosition().x, y_pos);
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_shaft_sprite.getPosition().x, y_pos);
|
|
|
|
y_pos--;
|
|
|
|
y_pos--;
|
|
|
|