用C++写一个飞机大战的游戏
时间: 2023-06-15 08:05:32 浏览: 101
这是一个关于如何使用C语言编写飞机大战游戏的简单示例:
```
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAX_X 50
#define MAX_Y 20
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hide_cursor()
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
}
void clear_screen()
{
system("cls");
}
void draw_boundary()
{
int i;
for (i = 0; i < MAX_X; i++)
{
gotoxy(i, 0);
printf("-");
gotoxy(i, MAX_Y);
printf("-");
}
for (i = 0; i <= MAX_Y; i++)
{
gotoxy(0, i);
printf("|");
gotoxy(MAX_X, i);
printf("|");
}
}
void draw_plane(int x, int y)
{
gotoxy(x, y);
printf("^");
gotoxy(x + 1, y + 1);
printf("/ \\ ");
gotoxy(x + 1, y + 2);
printf("\\ /");
}
int main()
{
int x = MAX_X / 2, y = MAX_Y - 1;
char ch;
hide_cursor();
clear_screen();
draw_boundary();
draw_plane(x, y);
while (1)
{
ch = getch();
switch (ch)
{
case 'a':
if (x > 1)
{
x--;
clear_screen();
draw_boundary();
draw_plane(x, y);
}
break;
case 'd':
if (x < MAX_X - 3)
{
x++;
clear_screen();
draw_boundary();
draw_plane(x, y);
}
break;
case 'w':
if (y > 1)
{
y--;
clear_screen();
draw_boundary();
draw_plane(x, y);
}
break;
case 's':
if (y < MAX_Y - 2)
{
y++;
clear_screen();
draw_boundary();
draw_plane(x, y);
}
break;
case 'q
阅读全文