代码拉取完成,页面将自动刷新
#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 = 20;
width = 50;
bird_x = high / 2;
bird_y = 3;
bar1_y = width / 2;
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("*"); //输出墙壁
else
printf(" "); //输出空格
}
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);
}
}
}
void updateWithInput() ///与用户输入有关的更新
{
char input;
if (kbhit()) //判断是否输入有关
{
input = getch(); //根据用户的不同输入来移动,不必输入回车
if (input == ' ')
bird_x = bird_x - 2;
}
}
int main()
{
startup(); //数据的初始化
while (1) //游戏循环执行
{
show(); //显示画面
updateWithoutInput(); ///与用户输入无关的更新
updateWithInput(); //与用户输入有关更新
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。