帮我写一个飞机游戏代码
时间: 2023-03-29 17:01:38 浏览: 271
好的,以下是一个简单的飞机游戏代码,供您参考:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 30
#define HEIGHT 20
int x, y; // 飞机的坐标
int score; // 得分
int speed; // 飞机速度
int bullet_x, bullet_y; // 子弹的坐标
int enemy_x, enemy_y; // 敌机的坐标
void gotoxy(int x, int y) // 光标移动函数
{
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void draw_plane() // 绘制飞机
{
gotoxy(x, y);
printf(" /\\");
gotoxy(x, y+1);
printf(" | |");
gotoxy(x, y+2);
printf("/----\\");
}
void draw_bullet() // 绘制子弹
{
gotoxy(bullet_x, bullet_y);
printf("|");
}
void draw_enemy() // 绘制敌机
{
gotoxy(enemy_x, enemy_y);
printf(" /\\");
gotoxy(enemy_x, enemy_y+1);
printf(" | |");
gotoxy(enemy_x, enemy_y+2);
printf("/----\\");
}
void update_bullet() // 更新子弹位置
{
bullet_y--;
if (bullet_y <= ) // 子弹飞出屏幕外
{
bullet_x = ;
bullet_y = ;
}
}
void update_enemy() // 更新敌机位置
{
enemy_y++;
if (enemy_y >= HEIGHT) // 敌机飞出屏幕外
{
enemy_x = rand() % (WIDTH-6) + 3; // 随机生成敌机位置
enemy_y = ;
}
}
void check_collision() // 检测碰撞
{
if (bullet_x == enemy_x && bullet_y == enemy_y+2) // 子弹打中敌机
{
score++;
bullet_x = ;
bullet_y = ;
enemy_x = rand() % (WIDTH-6) + 3; // 随机生成敌机位置
enemy_y = ;
}
if ((x == enemy_x && y == enemy_y) || (x == enemy_x+5 && y == enemy_y)) // 飞机撞上敌机
{
printf("\a"); // 播放声音
printf("\nGame Over! Your score is %d\n", score);
exit();
}
}
int main()
{
system("mode con cols=32 lines=22"); // 设置窗口大小
system("title Plane Game"); // 设置窗口标题
system("color f"); // 设置窗口颜色
x = WIDTH / 2 - 1; // 初始化飞机位置
y = HEIGHT - 3;
score = ;
speed = 100;
bullet_x = ;
bullet_y = ;
enemy_x = rand() % (WIDTH-6) + 3; // 随机生成敌机位置
enemy_y = ;
while (1)
{
if (_kbhit()) // 检测键盘输入
{
char ch = _getch();
if (ch == 'a' && x > ) // 左移
x--;
if (ch == 'd' && x < WIDTH-3) // 右移
x++;
if (ch == 'w' && y > ) // 上移
y--;
if (ch == 's' && y < HEIGHT-3) // 下移
y++;
if (ch == ' ') // 发射子弹
{
bullet_x = x+2;
bullet_y = y-1;
}
}
system("cls"); // 清屏
draw_plane(); // 绘制飞机
draw_enemy(); // 绘制敌机
if (bullet_y > ) // 子弹在屏幕内才绘制
draw_bullet(); // 绘制子弹
update_bullet(); // 更新子弹位置
update_enemy(); // 更新敌机位置
check_collision(); // 检测碰撞
Sleep(speed); // 控制游戏速度
}
return ;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)