Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <string>
- #include <random>
- #include <fstream>
- #include <algorithm>
- #include <cstdlib>
- #include <ctime>
- #include <cstring>
- class Bird{
- public:
- Bird(){
- bird_x = 100;
- bird_y = 0;
- bird.push_back(sf::CircleShape(30.f));
- bird[0].setFillColor(sf::Color::Red);
- bird[0].setPosition(bird_x, bird_y);
- bird.push_back(sf::CircleShape(10, 3));
- bird[1].setFillColor(sf::Color::Yellow);
- bird[1].setRotation(90);
- bird[1].setPosition(bird_x + 71, bird_y + 20);
- bird.push_back(sf::CircleShape(3.f));
- bird[2].setFillColor(sf::Color::Blue);
- bird[2].setPosition(bird_x + 20, bird_y + 10);
- }
- void getPosition(float *x, float *y){
- *x = bird_x + 15;
- *y = bird_y + 15;
- }
- void moveVertically(float mov){
- bird_y += mov;
- for(unsigned int i = 0; i < bird.size(); i++){
- bird[i].move(0, mov);
- }
- }
- std::vector<sf::CircleShape> getBirdVector(){
- return bird;
- }
- private:
- std::vector<sf::CircleShape> bird;
- float bird_x;
- float bird_y;
- };
- class Pipe{
- public:
- Pipe(unsigned int space, unsigned int window_width, unsigned int window_height){
- printf("Spawning new pipe with gap from %u to %u\n", space - 100, space + 100);
- upper_pipe = new sf::RectangleShape(sf::Vector2f(100, space - 100));
- lower_pipe = new sf::RectangleShape(sf::Vector2f(100, window_height - space - 100));
- upper_pipe->setFillColor(sf::Color::Green);
- lower_pipe->setFillColor(sf::Color::Green);
- current_x = window_width + 100;
- controlled_space = space;
- upper_pipe->setPosition(current_x, 0);
- lower_pipe->setPosition(current_x, space + 100);
- }
- bool checkIfCollision(float colliding_x, float colliding_y){
- if(colliding_x > current_x + 50 || colliding_x < current_x - 50) return false;
- if(colliding_y < controlled_space + 100 && colliding_y > controlled_space - 100) return false;
- return true;
- }
- bool update(){
- if(current_x < -100) return true;
- current_x -= 1;
- upper_pipe->move(-1, 0);
- lower_pipe->move(-1, 0);
- return false;
- }
- void drawSelf(sf::RenderWindow *to_draw_to){
- to_draw_to->draw(*upper_pipe);
- to_draw_to->draw(*lower_pipe);
- return;
- }
- void properDestruct(){
- delete upper_pipe;
- delete lower_pipe;
- }
- ~Pipe(){}
- private:
- sf::RectangleShape *upper_pipe;
- sf::RectangleShape *lower_pipe;
- signed int current_x;
- unsigned int controlled_space;
- };
- void save(unsigned int score){
- char *name = (char *)calloc(1, 1);
- char buf = 0;
- printf("Enter your name: ");
- while(1){
- scanf("%c", &buf);
- if(buf == '\n')break;
- name = (char *)realloc(name, strlen(name) + 2);
- name[strlen(name)] = buf;
- }
- std::ofstream savefile;
- savefile.open(".flappy_bird_save", std::ios::out | std::ios::app);
- savefile << name;
- savefile << ':';
- savefile.write((char *)(void *)(&score), sizeof(unsigned int));
- savefile << '\n';
- savefile.close();
- }
- void get_hs(){
- printf("====LEADERBOARD====\n");
- struct player{
- unsigned int score;
- char *name;
- };
- std::vector<player> players;
- std::ifstream savefile;
- char buf = 0;
- savefile.open(".flappy_bird_save");
- while(savefile.good()){
- buf = 0;
- players.resize(players.size() + 1);
- players[players.size() - 1].name = (char *)calloc(1, 1);
- while(1){
- buf = savefile.get();
- if(buf == ':' || !savefile.good()) break;
- players[players.size() - 1].name = (char *)realloc(players[players.size() - 1].name, strlen(players[players.size() - 1].name) + 2);
- players[players.size() - 1].name[strlen(players[players.size() - 1].name)] = buf;
- }
- savefile.read((char *)(void *)&(players[players.size() - 1].score), sizeof(unsigned int));
- savefile.get(); //drop the newline
- }
- bool is_sorted = false;
- if(players.size() < 3) return; //don't bother
- while(1){
- for(unsigned int i = 0; i < players.size(); i++){
- unsigned int pivot_pos = players.size() / 2;
- if(players[pivot_pos].score == 0) pivot_pos -= 1;
- if(i == pivot_pos) pivot_pos -= 1;
- if(players[i].score < players[pivot_pos].score){
- players.push_back(players[i]);
- players.erase(players.begin() + i);
- }
- }
- is_sorted = true;
- for(unsigned int i = 0; i < players.size() - 2; i++){
- if(players[i].score < players[i + 1].score){
- is_sorted = false;
- break;
- }
- }
- if(is_sorted) break;
- }
- for(unsigned int i = 0; i < players.size(); i++){
- if(players[i].score == 0) break;
- printf("%s:\t%8u\n", players[i].name, players[i].score);
- }
- }
- int main(int argc, char *argv[]){
- if(argc < 2) return 1;
- std::default_random_engine rand_generator;
- rand_generator.seed(time(0));
- get_hs();
- printf("Insert quarter to play\n");
- getchar();
- unsigned int window_width = (argc < 3) ? 1000 : atoi(argv[2]); //we have to do it this way because SFML team is too lazy to let you
- unsigned int window_height = (argc < 4) ? 800 : atoi(argv[3]); //actually get the size of the window.
- sf::RenderWindow window(sf::VideoMode(window_width, window_height), argv[1]); //damn kids...
- printf("Created the window\n");
- std::uniform_int_distribution<int> pipe_pos_spawn(100, window_height - 100);
- Bird protagonist;
- std::vector<sf::CircleShape> protagonist_parts;
- std::vector<Pipe> pipes;
- float protagonist_x;
- float protagonist_y;
- unsigned int score = 0;
- char *score_text = (char *)calloc(1, 11);
- sprintf(score_text, "%u", score);
- float current_gravity_pull = 0.5;
- sf::Clock time_tracker;
- sf::Time check_time;
- sf::RectangleShape *current_upper_pipe;
- sf::RectangleShape *current_lower_pipe;
- sf::Font font;
- if(!font.loadFromFile("Hack-Regular.ttf")) return 1;
- sf::Text score_view;
- score_view.setCharacterSize(32);
- score_view.setFont(font);
- score_view.setFillColor(sf::Color::White);
- score_view.setStyle(1);
- score_view.setString("Score: " + std::string(score_text));
- printf("Scoreview is currently %s\n", score_view.getString().toAnsiString().c_str());
- pipe_pos_spawn(rand_generator); //throwaway first value
- protagonist.moveVertically(window_height / 2);
- while(window.isOpen()){
- sf::Event event;
- while(window.pollEvent(event)){
- if(event.type == sf::Event::Closed) window.close();
- }
- protagonist.getPosition(&protagonist_x, &protagonist_y);
- current_gravity_pull = (current_gravity_pull * 2 < 16) ? current_gravity_pull : 16;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && protagonist_y > 0){
- current_gravity_pull = 0.5;
- protagonist.moveVertically(-2);
- }else{
- protagonist.moveVertically(current_gravity_pull);
- }
- protagonist_parts = protagonist.getBirdVector();
- check_time = time_tracker.getElapsedTime();
- if(check_time.asMilliseconds() % 1000 == 0){
- pipes.push_back(Pipe(pipe_pos_spawn(rand_generator), window_width, window_height));
- }
- window.clear();
- for(unsigned int i = 0; i < protagonist_parts.size(); i++){ //draw the bird
- window.draw(protagonist_parts[i]);
- }
- for(unsigned int i = 0; i < pipes.size(); i++){
- pipes[i].drawSelf(&window);
- if(pipes[i].update()){
- printf("Update returned true\n");
- pipes[i].properDestruct();
- pipes[i].~Pipe();
- pipes.erase(pipes.begin() + i);
- score += 1;
- sprintf(score_text, "Score: %u", score);
- score_view.setString(std::string(score_text));
- printf("Scoreview is currently %s\n", score_view.getString().toAnsiString().c_str());
- continue;
- }else if(pipes[i].checkIfCollision(protagonist_x, protagonist_y) || protagonist_y >= window_height){
- printf("You died\nScore: %u\n", score);
- window.close();
- save(score);
- return 0;
- }
- }
- window.draw(score_view);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement