#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define a 2
typedef struct Bird
{
int width, high; //屏幕的宽、高
int bird_x, bird_y; //鸟的位置
int wall_top, wall_down; //上下障碍物的上表面与上表面位置
int wall_x; //障碍物出现的位置
int score; //分数
}Bird;
void gotoxy(int x, int y)//清屏
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()//隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Init(Bird *bird)//初始化结构体成员
{
bird->width = 30;
bird->high = 30;
bird->wall_top = 13;
bird->wall_down = 18;
bird->bird_x = 10;
bird->bird_y = 10;
bird->wall_x = 17;
bird->score = 0;
HideCursor();
system("mode con cols=100 lines=80");
}
void show(Bird *bird)//显示游戏画面
{
gotoxy(0, 0);
static int i = 0;
i++;
if (i == 10)//main函数的循环执行10次进行一次清屏
{
system("cls");
}
for (int y = 0; y < bird->high; y++)
{
for (int x = 0; x < bird->width; x++)
{
if ((x == bird->wall_x) && ((y < bird->wall_top) || (y > bird->wall_down)))//显示障碍物
printf("*");
else if ((x == bird->bird_x) && (y == bird->bird_y)) //显示鸟
printf("@");
else
printf(" ");
}
printf("\n");
}
printf("得分:%d\n", bird->score);
}
void PeopleVin(Bird *bird)//玩家控制小鸟运动
{
char input;
if (kbhit())
{
input = getch();
switch (input)
{
case 32:bird->bird_y -= 2; break; //空格键上升
case 80:bird->bird_y++; break; //下键下降
}
}
}
int Updatecomputer(Bird *bird)//电脑自主更新的数据
{
bird->bird_y++; //小鸟下降
Sleep(100);
bird->wall_x--; //墙往左移
if (bird->bird_x == bird->wall_x)//当墙的位置与小鸟的位置相同时,小鸟从空白区域通过时分数加1
{
if ((bird->bird_y<bird->wall_down) && (bird->bird_y>bird->wall_top))
{
bird->score++;
}
else
{
bird->bird_x--;
int t = 1;
while (bird->bird_y < bird->high)//小鸟碰到障碍物时,小鸟沿墙壁加速下降,最终退出游戏
{
bird->bird_y = bird->bird_y+(t*t);
show(bird);
t++;
Sleep(50);
}
printf("游戏结束\n");
return 0;
}
}
if (bird->wall_x < 0)//更新墙出现的位置,及安全区域 (bird->wall_top,bird->wall_down)
{
bird->wall_x = rand() % 7 + 15;
bird->wall_top = rand() % (bird->high / 2) + 5;
bird->wall_down = bird->wall_top + 5;
}
return 1;
}
int main()
{
Bird bird;
int flag = 1;
Init(&bird);
while (flag)
{
show(&bird);
PeopleVin(&bird);
flag=Updatecomputer(&bird);
}
system("pause");
return 0;
}
飞翔的小鸟小游戏
最新推荐文章于 2023-01-31 15:40:56 发布