如何用C语言编写类似Flappy Bird的游戏?
- 内容介绍
- 文章标签
- 相关推荐
本文共计735个文字,预计阅读时间需要3分钟。
本例为家用分享了C语言实现Flappy Bird的整体代码,供大家参考。具体内容如下:
c#include #include
#define WIDTH 100#define HEIGHT 30#define G 0.5#define JUMP 3
int birdY=HEIGHT / 2;int birdX=WIDTH / 2 - 2;int pipeTop=HEIGHT / 2;int pipeBottom=HEIGHT / 2;int score=0;
void drawBird() { printf( %c%c%c , 219, 219, 219);}
void drawPipeTop() { for (int i=0; i void drawPipeBottom() { for (int i=0; i void drawScore() { printf(Score: %d, score);} void drawGame() { for (int y=0; y void jump() { birdY -=JUMP; pipeTop +=JUMP; pipeBottom +=JUMP;} void update() { birdY +=G; if (birdY > HEIGHT - 2 || birdY <0) { printf(Game Over!\n); exit(0); } if (birdX WIDTH - 2) { printf(Game Over!\n); exit(0); } if (birdY >=pipeTop && birdY <=pipeBottom) { printf(Game Over!\n); exit(0); } if (birdY==pipeBottom + 1 && pipeTop==pipeBottom - 1) { score++; }} int main() { while (1) { drawGame(); update(); if (_kbhit()) { jump(); } } return 0;} 本文实例为大家分享了C语言实现flappy bird的具体代码,供大家参考,具体内容如下
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//定义全局变量
int high,width; //边界
int bird_x,bird_y; //小鸟坐标
int bar_y; //挡板坐标
int bar_xTop,bar_xDown; //挡板开口上下坐标
int score; //得分
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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=18; //初始化边界
width=26;
bird_x=high/2; //初始化小鸟坐标
bird_y=width/2;
bar_y=width; //挡板坐标
bar_xTop=high/2; //挡板开口
bar_xDown=bar_xTop+4;
score=0; //得分
HideCursor(); //隐藏光标
}
void show() //显示界面
{
int i,j;
gotoxy(0,0);
for(i=0;i<=high+1;i++)
{
for(j=0;j<=width;j++)
{
if((i==bird_x)&&(j==bird_y))//输出小鸟@
printf("@");
else if(i==high+1) //输出下边界
printf("_");
else if(j==width) //输出右边界
printf("|");
else if((j==bar_y)&&((i<=bar_xTop)||(i>=bar_xDown))) //输出挡板
printf("|");
else printf(" ");
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updateWithoutInput() //与用户输入无关的更新
{
if(bird_y==bar_y-1)
{
if((bird_x>bar_xTop)&&(bird_x<bar_xDown)) //小鸟通过挡板开口加分
score++;
else
{
printf("小鸟死亡\n"); //小鸟没没通过挡板开口小鸟死亡
exit(0);
}
}
if(bar_y>0)
bar_y--;
else
{
bar_y=width;
bar_xTop=rand()%(high-4);
bar_xDown=bar_xTop+4;
}
if(bird_x<high+1)
bird_x++;
else
{
printf("小鸟死亡\n");
exit(0);
}
Sleep(200);
}
void updateWithInpute() //与用户输入有关的更新
{
char input;
if(kbhit())
{
input=getch();
if((input==' ')&&(bird_x>0)&&(bird_x<=high) ) //小鸟向上跳
bird_x=bird_x-2;
}
}
int main()
{
startup(); //数据初始化
while(1)
{
show(); //显示界面
updateWithoutInput(); //与用户输入无关的更新
updateWithInpute(); //与用户输入有关的更新
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计735个文字,预计阅读时间需要3分钟。
本例为家用分享了C语言实现Flappy Bird的整体代码,供大家参考。具体内容如下:
c#include #include
#define WIDTH 100#define HEIGHT 30#define G 0.5#define JUMP 3
int birdY=HEIGHT / 2;int birdX=WIDTH / 2 - 2;int pipeTop=HEIGHT / 2;int pipeBottom=HEIGHT / 2;int score=0;
void drawBird() { printf( %c%c%c , 219, 219, 219);}
void drawPipeTop() { for (int i=0; i void drawPipeBottom() { for (int i=0; i void drawScore() { printf(Score: %d, score);} void drawGame() { for (int y=0; y void jump() { birdY -=JUMP; pipeTop +=JUMP; pipeBottom +=JUMP;} void update() { birdY +=G; if (birdY > HEIGHT - 2 || birdY <0) { printf(Game Over!\n); exit(0); } if (birdX WIDTH - 2) { printf(Game Over!\n); exit(0); } if (birdY >=pipeTop && birdY <=pipeBottom) { printf(Game Over!\n); exit(0); } if (birdY==pipeBottom + 1 && pipeTop==pipeBottom - 1) { score++; }} int main() { while (1) { drawGame(); update(); if (_kbhit()) { jump(); } } return 0;} 本文实例为大家分享了C语言实现flappy bird的具体代码,供大家参考,具体内容如下
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//定义全局变量
int high,width; //边界
int bird_x,bird_y; //小鸟坐标
int bar_y; //挡板坐标
int bar_xTop,bar_xDown; //挡板开口上下坐标
int score; //得分
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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=18; //初始化边界
width=26;
bird_x=high/2; //初始化小鸟坐标
bird_y=width/2;
bar_y=width; //挡板坐标
bar_xTop=high/2; //挡板开口
bar_xDown=bar_xTop+4;
score=0; //得分
HideCursor(); //隐藏光标
}
void show() //显示界面
{
int i,j;
gotoxy(0,0);
for(i=0;i<=high+1;i++)
{
for(j=0;j<=width;j++)
{
if((i==bird_x)&&(j==bird_y))//输出小鸟@
printf("@");
else if(i==high+1) //输出下边界
printf("_");
else if(j==width) //输出右边界
printf("|");
else if((j==bar_y)&&((i<=bar_xTop)||(i>=bar_xDown))) //输出挡板
printf("|");
else printf(" ");
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updateWithoutInput() //与用户输入无关的更新
{
if(bird_y==bar_y-1)
{
if((bird_x>bar_xTop)&&(bird_x<bar_xDown)) //小鸟通过挡板开口加分
score++;
else
{
printf("小鸟死亡\n"); //小鸟没没通过挡板开口小鸟死亡
exit(0);
}
}
if(bar_y>0)
bar_y--;
else
{
bar_y=width;
bar_xTop=rand()%(high-4);
bar_xDown=bar_xTop+4;
}
if(bird_x<high+1)
bird_x++;
else
{
printf("小鸟死亡\n");
exit(0);
}
Sleep(200);
}
void updateWithInpute() //与用户输入有关的更新
{
char input;
if(kbhit())
{
input=getch();
if((input==' ')&&(bird_x>0)&&(bird_x<=high) ) //小鸟向上跳
bird_x=bird_x-2;
}
}
int main()
{
startup(); //数据初始化
while(1)
{
show(); //显示界面
updateWithoutInput(); //与用户输入无关的更新
updateWithInpute(); //与用户输入有关的更新
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

