C + + version simple Flappy Bird_c language

Source: Internet
Author: User
Tags rand sprintf

Freshman, last semester finished C, wrote a few console games
This semester self-study C + +, because the school curriculum seventh weeks only C + +

While learning to write this little game, the SDL graphics library finishes the graphic painting
Time is in a hurry, bugs are there, code is less efficient
And the original work is still very different from the source code in the Attachment game folder

Demo diagram

#include <stdlib.h> #include <windows.h> #include <time.h> #include <conio.h> #include <iost ream> #include <mmsystem.h> #include "sdl\sdl_image.h" #include "sdl\sdl_ttf.h" #include "sdl\sdl.h" using name
Space Std; #pragma comment (lib, "Winmm.lib") #pragma comment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "") int iii=
 0;
 int score = 0;
 int y=0;
int posi[3][2]= {{640,-300},{940,-300},{1240,-300}};  int i=0,n=3;
n is the pipe logarithmic sdl_color textcolor_1 = {37, 134, 108};//green//sdl_color textcolor_2 = {112, 112, 35}; Sdl_color textcolor_2 = {255, 255, 255}; White Sdl_color textcolor_3 = {0, 88, 132};
  White class Event {Public:sdl_event event;
  Uint8 *keystates;
  Event () {keystates=sdl_getkeystate (NULL);
}
 
 
};
Event thing;
  Class Data {Public:int bird_y;
  int score;
  int posi[20][2];
  Sdl_rect bird_1;
  Sdl_rect bird_2;
  Sdl_rect BG;
  Sdl_rect Welcome;
  Sdl_rect pipe_up;
  Sdl_rect Pipe_down; Sdl_rect bIrd_cls;
 
  Sdl_rect Foot;
    sdl_surface* Load_imag (char *s) {sdl_surface* temp;
 
    Temp=img_load (s);
    bg.x=0;
    bg.y=0;
    bg.w=640;
 
    bg.h=480;
    welcome.x=681;
    welcome.y=0;
    welcome.w=640;
 
    welcome.h=480;
    bird_1.x=17;
    bird_1.y=508;
    bird_1.w=52;
 
    bird_1.h=34;
    bird_2.x=461;
    bird_2.y=531;
    bird_2.w=52;
 
    bird_2.h=33;
    bird_cls.w=52;
    bird_cls.h=33;
    bird_cls.x=0;
     
    bird_cls.y=0;
    pipe_up.x=1361;
    pipe_up.y=11;
    pipe_up.w=103;
 
    pipe_up.h=410;
    pipe_down.x=1487;
    pipe_down.y=14;
    pipe_down.w=103;
 
    pipe_down.h=443;
    foot.x=0;
    foot.y=404;
    foot.w=640;
 
 
   
    foot.h=80;
  return Temp;//sdl_displayformat (temp);
    } ttf_font* Load_font () {sdl_init (sdl_init_everything);
    Ttf_init ();
 
  Return Ttf_openfont ("Main_1.dat", 36);
 
}
};
 
Data data;
  Class Pri {public:sdl_surface* out;//output sdl_surface* main;//main window Ttf_font *font; Sdl_rECT A;
  Set the initial position sdl_surface*bird;
  Sdl_rect XY;
  sdl_surface*font_1;
  sdl_surface*font_2;
  Sdl_surface*font_11;
 
 
  sdl_surface*font_22;
      Pri () {Ttf_font *font = NULL;
    Init ();//Initialize output device} int init () {sdl_init (sdl_init_everything);
    Ttf_init ();
    Main= Sdl_setvideomode (640, 478, sdl_swsurface);
    Sdl_wm_setcaption ("Flappy Bird", NULL);//title Font=data.load_font ();
    Out=data.load_imag ("Main_2.dat");
   
    y=150;
    posi[0][0]=640;
    posi[0][1]=-300;
    posi[1][0]=940;
    posi[1][1]=-300;
    posi[2][0]=1240;
    posi[2][1]=-300;
  return 1;
  int pipe () {} int bir (int y)//x=150 {int II;
  I+=1;
  xy.x=150;
  Xy.y=y;
      if (i<14) {xy.x=150;
      Xy.y=y;
 
       
      Sdl_blitsurface (Out,&data.bg,main,null);
        for (ii=0;ii<n;ii++) {xy.x=posi[ii][0];
        XY.Y=POSI[II][1];
        Sdl_blitsurface (OUT,&AMP;DATA.PIPE_DOWN,MAIN,&AMP;XY); xy.x=posi[ii][0];
        xy.y=posi[ii][1]+600;
 
      Sdl_blitsurface (OUT,&AMP;DATA.PIPE_UP,MAIN,&AMP;XY);
      } xy.x=150;
      Xy.y=y;
    Sdl_blitsurface (OUT,&AMP;DATA.BIRD_1,MAIN,&AMP;XY);
      xy.x=0;
      xy.y=404;
   
    Sdl_blitsurface (OUT,&AMP;DATA.FOOT,MAIN,&AMP;XY);
      else {xy.x=150;
      Xy.y=y;
      Sdl_blitsurface (Out,&data.bg,main,null);
        for (ii=0;ii<n;ii++) {xy.x=posi[ii][0];
        XY.Y=POSI[II][1];
 
        Sdl_blitsurface (OUT,&AMP;DATA.PIPE_DOWN,MAIN,&AMP;XY);
        XY.X=POSI[II][0];
        xy.y=posi[ii][1]+600;
 
      Sdl_blitsurface (OUT,&AMP;DATA.PIPE_UP,MAIN,&AMP;XY);
      } xy.x=150;
      Xy.y=y;
      Sdl_blitsurface (OUT,&AMP;DATA.BIRD_2,MAIN,&AMP;XY);
      xy.x=0;
      xy.y=404;
      Sdl_blitsurface (OUT,&AMP;DATA.FOOT,MAIN,&AMP;XY);
    if (i>28) i=0;
     
 
  return 0;
       
      int bg () {sdl_blitsurface (OUT,&AMP;DATA.BG,MAIN,&AMP;XY); RetUrn 0;
 
    int over () {int x=0,y=0,i=0;
    xy.x=250;
    xy.y=150;
    Font_1=ttf_rendertext_solid (font, "Game over!", textcolor_3);
 
    Sdl_blitsurface (Font_1, NULL, Main, &xy);
 
    Sdl_flip (main);
    xy.x=350;
    xy.y=200;
    Font_1=ttf_rendertext_solid (Font, "Retry game", textcolor_1);
    Font_11=ttf_rendertext_solid (Font, "Retry game", textcolor_2); 
     
 
    Sdl_blitsurface (Font_1, NULL, Main, &xy);
    xy.x=350;
 
    xy.y=280;
    Font_2=ttf_rendertext_solid (Font, "Exit game", textcolor_1);
    Font_22=ttf_rendertext_solid (Font, "Exit game", textcolor_2); 
Sdl_blitsurface (Font_2, NULL, Main, &xy);
    /* XY.X=10;
    xy.y=440; Wild Pointer sdl_blitsurface (ttf_rendertext_solid (Font, "Esc:exit game space:to Jump Hekun", textcolor_1), NULL, Main, 
    &AMP;XY); Sdl_flip (main);
      Update window */while (Sdl_pollevent (&thing.event), 1) {i+=2;
      Sleep (1); if (thing.keystates[Sdlk_escape]) EXIT (0);
      if (Thing.event.type = = sdl_quit)//Click on the SDL Close button {exit (0);  } if ((x>350&&x<540) && (y<240) && (y>200)) {if (thing. Event.type) = = Sdl_mousebuttondown) if (thing.event.button.button== sdl_button_left) {SDL_FREESURFAC
            E (font_1);
            Sdl_freesurface (Font_11);
            Sdl_freesurface (font_22);
          Sdl_freesurface (font_2);
          WinExec ("Flappy Bird.exe", NULL);
            Exit (0);
          return 11; } else {} if ((x>350&&x<540) && (y<320) && (Y&G t;280)//end Game {if (thing. Event.type = = Sdl_mousebuttondown) if (thing.event.button.button== Sdl_
 
      Button_left) exit (0);
        } if (thing. Event.type = = sdl_mousemotion) {x=thing.event.motion.x;
 
        Y=THING.EVENT.MOTION.Y; Gets the mouse coordinates if (thing.event.mOtion.x>350&&thing.event motion. x<540) && (thing.event.motion.y<240) && (
          thing.event.motion.y>200)) {xy.x=350;
          xy.y=200; 
          Sdl_blitsurface (Font_11, NULL, Main, &xy);
        Sdl_flip (main);
          else {xy.x=350;
          xy.y=200; 
          Sdl_blitsurface (Font_1, NULL, Main, &xy);
        Sdl_flip (main); if (thing.event.motion.x>350&&thing.event. Motion x<540) && (thing.event.motion.y<
          ) && (thing.event.motion.y>280)) {xy.x=350;
          xy.y=280; 
          Sdl_blitsurface (font_22, NULL, Main, &xy);
 
 
        Sdl_flip (main);
          else {xy.x=350;
          xy.y=280; 
          Sdl_blitsurface (Font_2, NULL, Main, &xy);
        Sdl_flip (main);
  }} return 0;
 
 int welcome () {int x=0,y=0,i=0;
    Sdl_blitsurface (Out,&data.welcome,main,null);
    Bird=out;
    xy.x=350;
    xy.y=200;
    Font_1=ttf_rendertext_solid (Font, "Start game", textcolor_1);
    Font_11=ttf_rendertext_solid (Font, "Start game", textcolor_2); 
 
    Sdl_blitsurface (Font_1, NULL, Main, &xy);
    xy.x=350;
 
    xy.y=280;
    Font_2=ttf_rendertext_solid (Font, "Exit game", textcolor_1);
    Font_22=ttf_rendertext_solid (Font, "Exit game", textcolor_2); 
 
    Sdl_blitsurface (Font_2, NULL, Main, &xy);
    xy.x=10;
    xy.y=440; Wild Pointer sdl_blitsurface (ttf_rendertext_solid (Font, "Esc:exit game space:to Jump Hekun", textcolor_1), NULL, Main, 
    &AMP;XY); Sdl_flip (main);
      Update window while (Sdl_pollevent (&thing.event), 1) {i+=2;
      Sleep (1);
      if (thing.keystates[Sdlk_escape]) exit (0);
      if (Thing.event.type = = sdl_quit)//Click on the SDL Close button {exit (0); } if ((x>350&&x<540) && (y<240) &AMP;&AMP; (y>200)) {if (thing. Event.type = = Sdl_mousebuttondown) if (thing.event.button.button== sdl_b
            Utton_left) {sdl_freesurface (font_1);
            Sdl_freesurface (Font_11);
            Sdl_freesurface (font_22);
            Sdl_freesurface (font_2);
          return 0; } else {} if ((x>350&&x<540) && (y<320) && (y>280))//end Game {if (thing. Event.type = = Sdl_mousebuttondown) if (Thing.ev
 
      ent.button.button== sdl_button_left) exit (0);
        } if (thing. Event.type = = sdl_mousemotion) {x=thing.event.motion.x;
 
        Y=THING.EVENT.MOTION.Y; Gets the mouse coordinates if (thing.event.motion.x>350&&thing.event. Motion x<540) && (THING.EVENT.MOTION.Y&L
          t;240) && (thing.event.motion.y>200)) {xy.x=350;
          xy.y=200; Sdl_bLitsurface (Font_11, NULL, Main, &xy);
        Sdl_flip (main);
          else {xy.x=350;
          xy.y=200; 
          Sdl_blitsurface (Font_1, NULL, Main, &xy);
        Sdl_flip (main); if (thing.event.motion.x>350&&thing.event. Motion x<540) && (thing.event.motion.y&lt
          ;) && (thing.event.motion.y>280)) {xy.x=350;
          xy.y=280; 
          Sdl_blitsurface (font_22, NULL, Main, &xy);
       
 
        Sdl_flip (main);
          else {xy.x=350;
          xy.y=280; 
          Sdl_blitsurface (Font_2, NULL, Main, &xy);
        Sdl_flip (main);
        } if (i==400) {xy.x=150;
        xy.y=250;
        Sdl_blitsurface (BIRD,&AMP;DATA.BIRD_1,MAIN,&AMP;XY);
      Sdl_flip (main);
        } if (i==800) {xy.x=150;
        xy.y=250; Sdl_blitsurface (Bird,&data.BIRD_2,MAIN,&AMP;XY);
        i=0;
      Sdl_flip (main);
   
  } return 0;
 
}
 
 
 
};
 
 
 
PRI pri;
  int Game_judge () {int i=0; if (y>370| |
 
  Y&LT;-10) {if (Pri.over () ==11) return 12; for (i=0;i<3;i++) {if (posi[i][0]<202 &&posi[i][0]>50) {if (y<posi[i][1]+ 420 | |
    y>posi[i][1]+570) {if (Pri.over () ==11) return 12;}
} return 0;
  int Game_loop () {y=150,i=0;
  int ii=0,iii=0;
  Sdl_surface *font;
 
  Pri.bg ();
  Char s[50];
  Uint32 start = 0;
 
  sprintf (S, "fps:%d\t\t\t\t\t\t\t\tscore:%d", 1000/(Sdl_getticks ()-start), score);
int q=0;
  while (Sdl_pollevent (&thing.event), 1) {start = Sdl_getticks ();
  i++;
  Sleep (1);
  Sdl_flip (Pri.main);
  y+=4;
 
  if (y>430) y=100;
  if (thing.event.button.state==sdl_pressed) y-=20;
  if (thing.keystates[sdlk_space]) y-=20;
  if (thing.keystates[Sdlk_escape]) exit (0);
  if (Thing.event.type = = sdl_quit)//Click on the SDL Close button {exit (0); }
   
  if (Game_judge () ==12) return 0;
   
    Pri.bir (y);
    Shows the number of cycles per second ii++;
      if (ii>20) {sprintf (S, "fps:%d\t\t\t\t\t\t\t\tscore:%d", 1000/(Sdl_getticks ()-start) +rand ()%5,score);
    ii=0;
      } font=ttf_rendertext_solid (Pri.font, S, textcolor_2); 
      Sdl_blitsurface (font, null,pri. Main, NULL);
      Sdl_flip (Pri.main);
 
      Sdl_freesurface (font);
      posi[0][0]-=2;
      posi[1][0]-=2;
 
      posi[2][0]-=2;
   
      if (posi[0][0]<= ( -110)) {posi[0][0]=posi[2][0]+300;posi[0][1]=-(rand ()%180)-220;}
 
 
      if (posi[1][0]<= ( -110)) {posi[1][0]=posi[0][0]+300;posi[1][1]=-(rand ()%180)-220;}
       
 
     
      if (posi[2][0]<= ( -110)) {posi[2][0]=posi[1][0]+300;posi[2][1]=-(rand ()%180)-220;}
      iii++;
        if (iii>50) for (iii=0;iii<3;iii++) {if (posi[iii][0]<50);
        score++;
        iii=0;
      Break
 
} return 0; int main (int argc, char* arGv[]) {while (1) {pri.init ();//Initialize output device pri.welcome ();
   
  Game_loop ();
return 0;
  }

The above is the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.