用c++编写一个打飞机小游戏
时间: 2023-08-20 19:07:18 浏览: 43
好的,以下是一个简单的打飞机小游戏的C++代码示例:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 50; // 游戏窗口宽度
const int HEIGHT = 20; // 游戏窗口高度
const int PLAYER_X = WIDTH / 2; // 玩家飞机初始位置
const int PLAYER_Y = HEIGHT - 3;
const int ENEMY_COUNT = 10; // 敌机数量
const int BULLET_SPEED = 2; // 子弹速度
const int ENEMY_SPEED = 1; // 敌机速度
int score = 0; // 得分
bool gameOver = false; // 游戏结束标识
// 游戏窗口绘制函数
void drawWindow(char window[][WIDTH+1])
{
system("cls"); // 清屏
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
cout << window[i][j];
}
cout << endl;
}
}
// 玩家飞机绘制函数
void drawPlayer(char window[][WIDTH+1], int x, int y)
{
window[y][x-1] = '<';
window[y][x] = 'A';
window[y][x+1] = '>';
}
// 玩家飞机移动函数
void movePlayer(char window[][WIDTH+1], int& x, int& y, char input)
{
switch (input)
{
case 'a':
if (x > 1) x--;
break;
case 'd':
if (x < WIDTH - 2) x++;
break;
}
drawPlayer(window, x, y);
}
// 子弹绘制函数
void drawBullet(char window[][WIDTH+1], int x, int y)
{
window[y][x] = '|';
}
// 子弹移动函数
void moveBullet(char window[][WIDTH+1], int& x, int& y)
{
if (y > 0)
{
window[y][x] = ' ';
y -= BULLET_SPEED;
drawBullet(window, x, y);
}
else
{
window[y][x] = ' ';
}
}
// 敌机绘制函数
void drawEnemy(char window[][WIDTH+1], int x, int y)
{
window[y][x] = '+';
}
// 敌机移动函数
void moveEnemy(char window[][WIDTH+1], int& x, int& y)
{
if (y < HEIGHT - 1)
{
window[y][x] = ' ';
y += ENEMY_SPEED;
drawEnemy(window, x, y);
}
else
{
window[y][x] = ' ';
x = rand() % (WIDTH - 2) + 1;
y = rand() % 3 - 3;
drawEnemy(window, x, y);
}
}
// 碰撞检测函数
bool checkCollision(int bx, int by, int ex, int ey)
{
if (bx == ex && by == ey) return true;
else return false;
}
// 游戏结束函数
void gameover(char window[][WIDTH+1])
{
system("cls");
cout << "Game Over!" << endl;
cout << "Your Score is: " << score << endl;
drawWindow(window);
gameOver = true;
}
int main()
{
srand(time(NULL)); // 初始化随机数种子
char window[HEIGHT][WIDTH+1]; // 游戏窗口数组
memset(window, ' ', sizeof(window)); // 初始化为' '
// 绘制游戏窗口边框
for (int i = 0; i < HEIGHT; i++)
{
window[i][0] = '|';
window[i][WIDTH-1] = '|';
}
for (int j = 0; j < WIDTH; j++)
{
window[0][j] = '-';
window[HEIGHT-2][j] = '-';
}
// 绘制玩家飞机
drawPlayer(window, PLAYER_X, PLAYER_Y);
// 绘制敌机
int enemyX[ENEMY_COUNT], enemyY[ENEMY_COUNT];
for (int i = 0; i < ENEMY_COUNT; i++)
{
enemyX[i] = rand() % (WIDTH - 2) + 1;
enemyY[i] = rand() % 3 - 3;
drawEnemy(window, enemyX[i], enemyY[i]);
}
// 游戏主循环
while (!gameOver)
{
// 绘制游戏窗口
drawWindow(window);
// 接收玩家输入
if (_kbhit())
{
char input = _getch();
movePlayer(window, PLAYER_X, PLAYER_Y, input);
if (input == ' ')
{
for (int i = 0; i < ENEMY_COUNT; i++)
{
if (checkCollision(PLAYER_X, PLAYER_Y-1, enemyX[i], enemyY[i]))
{
score++;
enemyX[i] = rand() % (WIDTH - 2) + 1;
enemyY[i] = rand() % 3 - 3;
}
}
drawPlayer(window, PLAYER_X, PLAYER_Y);
drawBullet(window, PLAYER_X, PLAYER_Y-1);
}
}
// 移动子弹
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
if (window[i][j] == '|')
{
moveBullet(window, j, i);
for (int k = 0; k < ENEMY_COUNT; k++)
{
if (checkCollision(j, i, enemyX[k], enemyY[k]))
{
score++;
enemyX[k] = rand() % (WIDTH - 2) + 1;
enemyY[k] = rand() % 3 - 3;
window[i][j] = ' ';
}
}
}
}
}
// 移动敌机
for (int i = 0; i < ENEMY_COUNT; i++)
{
moveEnemy(window, enemyX[i], enemyY[i]);
if (checkCollision(PLAYER_X, PLAYER_Y, enemyX[i], enemyY[i]))
{
gameover(window);
}
}
// 延时
Sleep(50);
}
return 0;
}
```
以上代码实现了一个简单的打飞机小游戏,包括玩家飞机的移动、发射子弹、击落敌机、敌机的移动、游戏得分、游戏结束等功能。可以在Windows平台上编译运行。