具体详情看代码:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int bird_x, bird_y;//小鸟位置
int high, width;//地图大小
int barl_y, barl_xDown, barl_xTop;//障碍物
int score;//通过的障碍物
void HideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup()//初始赋值
{
high = 20;
width = 50;
bird_x = high/2;
bird_y = 3;
barl_y = width / 2;
barl_xDown = high / 3;
barl_xTop = high / 2;
score = 0;
}
void show()//显示画面
{
gotoxy(0, 0);//移动光标到(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 ((i < barl_xDown||i > barl_xTop) && j == barl_y)
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("得分:%d", score);
}
void updateWithoutInput()
{
bird_x++;
barl_y--;//障碍物向左移
if (bird_y == barl_y)//判断是否通过障碍物
{
if ((bird_x >= barl_xDown && bird_x <= barl_xTop))
score++;
else
{
printf("GameOver\n");
system("pause");
exit(0);
}
}
if (barl_y <= 0)//循环生成障碍物
{
barl_y = width;
int temp = rand()%int(high * 0.8);//随机生成必须为整形
barl_xDown = temp - high / 10;
barl_xTop = temp + high / 10;
barl_y = width / 2;
}
Sleep(150);
}
void updateWithInput()
{
if (_kbhit())
{
char input;
input = _getch();
switch (input)//控制移动
{
case ' ':if (bird_x == 0);else bird_x -= 2; break;
}
}
}
int main()
{
HideCursor();//隐藏光标
startup();//游戏循环执行
while (1)
{
show();//显示画面
updateWithoutInput();//与用户无关的数据更新
updateWithInput();//与用户相关的数据更新
}
return 0;
}
以上就是我的增加的内容,思考烧脑,大佬们点个赞;