项目: flappy bird
生活随笔
收集整理的這篇文章主要介紹了
项目: flappy bird
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、項目描述和最終項目展示
- 二、實現下落的小鳥
- 三、顯示小鳥和障礙物
- 四、障礙物移動
- 五、循環移動多個障礙物
一、項目描述和最終項目展示
通過按空格來控制小鳥的高度,來通過障礙物。二、實現下落的小鳥
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h>//全局變量 int high,width;//游戲畫面大小 int bird_x,bird_y;//小鳥的坐標 int bar1_y,bar1_xDown,bar1_xTop;//障礙物void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); }void startup()//數據的初始化 {high = 15;width = 20;bird_x = 0;bird_y = width/3; }void show()//顯示畫面 {gotoxy(0,0);//光標移動到原點位置,以下重畫清屏int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if((i == bird_x)&&(j == bird_y))printf("@");//輸出小鳥elseprintf(" ");//輸出空格}printf("\n");} }void updateWithoutInput()//與用戶輸入無關的更新 {bird_x++;Sleep(150); }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷是否有輸入{input = getch();//根據用戶得不同輸入來移動if(input == ' ')bird_x =bird_x -2;} } int main() {startup();//數據的初始化while(1){show();//顯示畫面updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新} }效果圖如下:
三、顯示小鳥和障礙物
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h>//全局變量 int high,width;//游戲畫面大小 int bird_x,bird_y;//小鳥的坐標 int bar1_y,bar1_xDown,bar1_xTop;//障礙物void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); }void startup()//數據的初始化 {high = 15;width = 20;bird_x = 0;bird_y = width/3;bar1_y=width/2;bar1_xDown = high/3;bar1_xTop = high/2; }void show()//顯示畫面 {gotoxy(0,0);//光標移動到原點位置,以下重畫清屏int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if((i == bird_x)&&(j == bird_y))printf("@");//輸出小鳥else if( (j == bar1_y) && ((i<bar1_xDown) || (i>bar1_xTop)))printf("*");elseprintf(" ");//輸出空格}printf("\n");} }void updateWithoutInput()//與用戶輸入無關的更新 {bird_x++;Sleep(150); }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷是否有輸入{input = getch();//根據用戶得不同輸入來移動if(input == ' ')bird_x =bird_x -2;} } int main() {startup();//數據的初始化while(1){show();//顯示畫面updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新} }效果圖如下:
四、障礙物移動
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h>//全局變量 int high,width;//游戲畫面大小 int bird_x,bird_y;//小鳥的坐標 int bar1_y,bar1_xDown,bar1_xTop;//障礙物 int score;//得分,經過障礙物的個數void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); }void startup()//數據的初始化 {high = 15;width = 20;bird_x = high/2;bird_y = width/3;bar1_y=width;bar1_xDown = high/3;bar1_xTop = high/2;score=0; }void show()//顯示畫面 {gotoxy(0,0);//光標移動到原點位置,以下重畫清屏int i,j;for(i=0;i<high;i++){for(j=0;j<width;j++){if((i == bird_x)&&(j == bird_y))printf("@");//輸出小鳥else if( (j == bar1_y) && ((i<bar1_xDown) || (i>bar1_xTop)))printf("*");elseprintf(" ");//輸出空格}printf("\n");}printf("得分: %d\n",score); }void updateWithoutInput()//與用戶輸入無關的更新 {bird_x++;bar1_y--;if( bird_y == bar1_y ){if( (bird_x >= bar1_xDown)&& (bird_x<=bar1_xTop) )score++;else{printf("游戲失敗\n");system("pause");exit(0);}}Sleep(150); }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷是否有輸入{input = getch();//根據用戶得不同輸入來移動if(input == ' ')bird_x =bird_x -2;} } int main() {startup();//數據的初始化while(1){show();//顯示畫面updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新} }效果圖如下:
五、循環移動多個障礙物
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h>//全局變量 int high,width;//游戲畫面大小 int bird_x,bird_y;//小鳥的坐標 int bar1_y,bar1_xDown,bar1_xTop;//障礙物1 int bar2_y,bar2_xDown,bar2_xTop;//障礙物2 int score;//得分,經過障礙物的個數void gotoxy(int x,int y)//將光標移動到(x,y)位置 {HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle,pos); }void startup()//數據的初始化 {high = 15;width = 50;bird_x = high/2;bird_y = width/3;bar1_y=width-20;bar1_xDown = high/3;bar1_xTop = high/2;bar2_y=width-1;bar2_xDown = high/3;bar2_xTop = high/2;score=0;system("color 09");system("title 游戲中"); }void show()//顯示畫面 {gotoxy(0,0);//光標移動到原點位置,以下重畫清屏int i,j;for(i=0;i<=high;i++){for(j=0;j<width;j++){if((i == bird_x)&&(j == bird_y))printf("@");//輸出小鳥else if( (j == bar1_y) && ((i<bar1_xDown) || (i>bar1_xTop && i<high)))//輸出擋板1printf("*");else if( (j == bar2_y) && ((i<bar2_xDown) || (i>bar2_xTop && i<high)))//輸出擋板2printf("*");else if( i == high)printf("-");elseprintf(" ");//輸出空格}printf("\n");}printf("得分: %d\n",score); }void updateWithoutInput()//與用戶輸入無關的更新 {bird_x++;bar1_y--;bar2_y--;if( bird_y == bar1_y ){if( (bird_x >= bar1_xDown)&& (bird_x<=bar1_xTop))score++;else{printf("游戲失敗\n");system("pause");exit(0);}}if( bird_y == bar2_y ){if( (bird_x >= bar2_xDown)&& (bird_x<=bar2_xTop))score++;else{printf("游戲失敗\n");system("pause");exit(0);}}if( bird_x==0 || bird_x == high )//挨著底部或頂部,游戲結束{printf("游戲失敗\n");system("pause");exit(0);}if(bar1_y<=0){bar1_y=width;int temp = rand()%(int)(high*0.8);bar1_xDown = temp-high/10;bar1_xTop = temp+high/10; }if(bar2_y<=0){bar2_y=width;int temp = rand()%(int)(high*0.8);bar2_xDown = temp-high/10;bar2_xTop = temp+high/10; }Sleep(150); }void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷是否有輸入{input = getch();//根據用戶得不同輸入來移動if(input == ' ')bird_x =bird_x -2;} } int main() {startup();//數據的初始化while(1){show();//顯示畫面updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新} }效果圖如下:
總結
以上是生活随笔為你收集整理的项目: flappy bird的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目: 用函数实现反弹球消砖块
- 下一篇: 项目: 用数组实现反弹球消砖块