|
|
@ -3,6 +3,9 @@
|
|
|
|
#include <iostream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <random>
|
|
|
|
#include <random>
|
|
|
|
#include <chrono>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include "Bird.h"
|
|
|
|
|
|
|
|
#include "Pipe.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef linux
|
|
|
|
#ifdef linux
|
|
|
|
#elif defined _WIN32
|
|
|
|
#elif defined _WIN32
|
|
|
@ -36,15 +39,17 @@ int main()
|
|
|
|
|
|
|
|
|
|
|
|
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "flappy");
|
|
|
|
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "flappy");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<std::vector<sf::Texture>> texture_list = std::make_shared<std::vector<sf::Texture>>();
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
|
|
|
sf::Texture t;
|
|
|
|
|
|
|
|
t.loadFromFile("../Assets/bird.png", sf::IntRect(0, i*12, 34, 24));
|
|
|
|
|
|
|
|
texture_list->push_back(t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Init flappy
|
|
|
|
// Init flappy
|
|
|
|
sf::Texture flappy_texture[4] = { sf::Texture(), sf::Texture(), sf::Texture(), sf::Texture() };
|
|
|
|
Bird bird(WINDOW_X/2, WINDOW_Y/2, texture_list);
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
|
|
|
flappy_texture[i].loadFromFile("../Assets/bird.png", sf::IntRect(0, i*12, 34, 24));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sf::RectangleShape flappy(sf::Vector2f(34, 24));
|
|
|
|
std::list<Pipe> pipes;
|
|
|
|
flappy.setTexture(&flappy_texture[0],false);
|
|
|
|
|
|
|
|
flappy.setPosition(WINDOW_X / 2, WINDOW_Y / 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init world
|
|
|
|
// Init world
|
|
|
|
sf::Texture background = sf::Texture(); background.loadFromFile("../Assets/sky.png");
|
|
|
|
sf::Texture background = sf::Texture(); background.loadFromFile("../Assets/sky.png");
|
|
|
@ -61,6 +66,12 @@ int main()
|
|
|
|
double frame_time = 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;
|
|
|
|
float speed = 250;
|
|
|
|
float speed = 250;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<sf::Shader> shader = std::make_shared<sf::Shader>();
|
|
|
|
|
|
|
|
if (!shader->loadFromFile("/home/mrh/source/floppy-bird/Assets/shaders/pipe_shader.vert", "/home/mrh/source/floppy-bird/Assets/shaders/pipe_shader.frag"))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cout << "asodijfoqijwef" << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (window.isOpen())
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sf::Event event; // Handle input
|
|
|
|
sf::Event event; // Handle input
|
|
|
@ -72,22 +83,23 @@ int main()
|
|
|
|
speed += event.mouseWheelScroll.delta * 20;
|
|
|
|
speed += event.mouseWheelScroll.delta * 20;
|
|
|
|
if(event.type == sf::Event::KeyPressed)
|
|
|
|
if(event.type == sf::Event::KeyPressed)
|
|
|
|
if (event.key.code == sf::Keyboard::Space)
|
|
|
|
if (event.key.code == sf::Keyboard::Space)
|
|
|
|
momentum = -2;
|
|
|
|
bird.impulse(3.0);
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
if (event.type == sf::Event::MouseButtonPressed) {
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Right)
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Right)
|
|
|
|
pipe_dist -= 10;
|
|
|
|
pipe_dist -= 10;
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Middle)
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Middle)
|
|
|
|
pipe_dist += 10;
|
|
|
|
pipe_dist += 10;
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Left)
|
|
|
|
if (event.mouseButton.button == sf::Mouse::Left)
|
|
|
|
momentum = -2;
|
|
|
|
bird.impulse(3.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
elapsed_time = elap_time(); // Handle 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 the frame has sim time, update
|
|
|
|
while ((accumulator_time - step_size) >= step_size) { // While the frame has sim time, update
|
|
|
@ -96,13 +108,17 @@ int main()
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipes.emplace_back(100, 100, std::make_shared<sf::Texture>(pipe_up), std::make_shared<sf::Texture>(pipe_shaft), shader);
|
|
|
|
|
|
|
|
std::cout << "added one" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
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)
|
|
|
@ -110,40 +126,33 @@ int main()
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
sf::Vector2f f_pos = flappy.getPosition();
|
|
|
|
|
|
|
|
sf::FloatRect f_rec = flappy.getGlobalBounds();
|
|
|
|
|
|
|
|
sf::Vector2f p_pos = pipe_up_sprite.getPosition();
|
|
|
|
|
|
|
|
sf::FloatRect p_rec = pipe_up_sprite.getGlobalBounds();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check collisions
|
|
|
|
// Check collisions
|
|
|
|
if (f_pos.y > land_sprite.getPosition().y) {
|
|
|
|
if (bird.collides(pipe_up_sprite.getGlobalBounds()) ||
|
|
|
|
f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2);
|
|
|
|
bird.collides(pipe_down_sprite.getGlobalBounds()) ||
|
|
|
|
momentum = -2;
|
|
|
|
bird.collides(land_sprite.getGlobalBounds())
|
|
|
|
std::cout << "\ndead " + std::to_string(flappy.getPosition().y);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
//std::cout << "dead!!!!!!!!" << std::endl;
|
|
|
|
if (f_pos.x < p_pos.x + p_rec.width &&
|
|
|
|
}
|
|
|
|
f_pos.x + f_rec.width > p_pos.x &&
|
|
|
|
|
|
|
|
(f_pos.y > p_pos.y || f_pos.y < p_pos.y - pipe_dist) &&
|
|
|
|
bird.tick(step_size);
|
|
|
|
(f_rec.height + f_pos.y > p_pos.y || f_rec.height + f_pos.y < p_pos.y + p_rec.height - pipe_dist)) {
|
|
|
|
for (auto pipe = pipes.begin(); pipe != pipes.end(); ++pipe)
|
|
|
|
|
|
|
|
{
|
|
|
|
f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2);
|
|
|
|
pipe->tick(step_size, speed);
|
|
|
|
momentum = -2;
|
|
|
|
}
|
|
|
|
std::cout << "\ndead " + std::to_string(flappy.getPosition().y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
momentum += g * step_size; // Impart gravity
|
|
|
|
|
|
|
|
f_pos.y += momentum;
|
|
|
|
|
|
|
|
flappy.setPosition(f_pos);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.draw(background_sprite); // Render
|
|
|
|
window.draw(background_sprite, &*shader); // Render
|
|
|
|
window.draw(land_sprite);
|
|
|
|
window.draw(land_sprite);
|
|
|
|
window.draw(flappy);
|
|
|
|
window.draw(bird);
|
|
|
|
window.draw(pipe_up_sprite);
|
|
|
|
window.draw(pipe_up_sprite);
|
|
|
|
window.draw(pipe_down_sprite);
|
|
|
|
window.draw(pipe_down_sprite);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto ptr = pipes.begin(); ptr != pipes.end(); ++ptr)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
window.draw(*ptr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pipe_shaft_sprite.setPosition(pipe_up_sprite.getPosition()); // Render the bottom pipe
|
|
|
|
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) {
|
|
|
|