1.实验项目名称
空战游戏
2.实验项目功能描述
利用数组进一步该井空战游戏,实现多台敌机,发射散弹等效果;
3.项目模块介绍
//清屏功能
void HideCuresor()
{
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()
{
pos_x = high - 1;
pos_y = width / 2;
canvas[pos_x][pos_y] = 1;
int k;
for (k = 0; k < enum; k++)
{
enemy_x[k] = rand() % 2;
enemy_y[k] = rand() % width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
score = 0;
bulwidth = 0;
enespeed = 20;
}
//显示飞机
void show()
{
gotoxy(0, 0);
int i, j;
for (i = 0; i < high; i++)
{
for (j = 0; j < width; j++)
{
if (canvas[i][j] == 0)
printf(" ");
else if (canvas[i][j] == 1)
printf("*");
else if (canvas[i][j] == 2)
printf("|");
else if (canvas[i][j] == 3)
printf("@");
}
printf("\n");
}
printf("得分:%d\n", score);
Sleep(20);
}
//击中敌机,多台敌机,击落得分,敌机刷新
void updateWithoutInput()
{
int i, j, k;
for (i = 0; i < high; i++)
{
for (j = 0; j < width; j++)
{
if (canvas[i][j] == 2)
{
for (k = 0; k < enum; k++)
{
if ((i == enemy_x[k]) && (j == enemy_y[k]))
{
score++;
if (score % 5 == 0 && enespeed > 3)
enespeed--;
if (score % 5 == 0)
bulwidth++;
canvas[enemy_x[k]][enemy_y[k]] = 3;
canvas[i][j] = 0;
}
}
canvas[i][j] = 0;
if (i > 0)
canvas[i - 1][j] = 2;
}
}
}
static int speed = 0;
if (speed < enespeed)
speed++;
for (k = 0; k < enum; k++)
{
if ((pos_x == enemy_x[k]) && (pos_y == enemy_y[k]))
{
printf("失败!\n");
Sleep(3000);
system("pause");
exit(0);
}
if (enemy_x[k] > high)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = rand() % 2;
enemy_y[k] = rand() % width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
score--;
}
if (speed == enespeed)
{
for (k = 0; k < enum; k++)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k]++;
speed = 0;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
}
}
}
//控制我方移动
void updateWithInput()
{
char input;
if (kbhit())
{
input = getch();
if (input == 'a'&&pos_y > 0)
{
canvas[pos_x][pos_y] = 0;
pos_y--;
canvas[pos_x][pos_y] = 1;
}
else if (input == 'd'&&pos_y < width - 1)
{
canvas[pos_x][pos_y] = 0;
pos_y++;
canvas[pos_x][pos_y] = 1;
}
else if (input == 'w')
{
canvas[pos_x][pos_y] = 0;
pos_x--;
canvas[pos_x][pos_y] = 1;
}
else if (input == 's')
{
canvas[pos_x][pos_y] = 0;
pos_x++;
canvas[pos_x][pos_y] = 1;
}
else if (input == ' ')
{
int left = pos_y - bulwidth;
int right = pos_y + bulwidth;
if (left < 0)
left = 0;
if (right > width - 1)
right = width - 1;
int k;
for (k = left; k <= right; k++)
canvas[pos_x - 1][k] = 2;
}
}
}
4.实现界面显示
5.代码托管链接
https://gitee.com/scfyer/first
6.实验总结
空战游戏变得比原来更有意思,进一步加深了对数组的理解与与运用。