使用c++写一个打飞机小游戏要求结束显示得分
时间: 2024-01-01 09:06:50 浏览: 116
好的,下面是使用C++语言编写的一个简单的打飞机小游戏,可以在控制台上运行,游戏结束后会显示得分。
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WIDTH = 40; // 游戏界面的宽度
const int HEIGHT = 20; // 游戏界面的高度
const int MAX_ENEMY_NUM = 10; // 敌机的最大数量
int score = 0; // 玩家的得分
// 定义游戏界面上的各种对象的结构体
struct Point {
int x, y;
};
struct Player {
Point pos;
};
struct Bullet {
Point pos;
bool active;
};
struct Enemy {
Point pos;
bool active;
};
// 初始化玩家
void initPlayer(Player& player) {
player.pos.x = WIDTH / 2;
player.pos.y = HEIGHT - 2;
}
// 初始化子弹
void initBullet(Bullet bullets[], int size) {
for (int i = 0; i < size; i++) {
bullets[i].active = false;
}
}
// 初始化敌机
void initEnemy(Enemy enemies[], int size) {
for (int i = 0; i < size; i++) {
enemies[i].active = false;
}
}
// 绘制游戏界面上的各种对象
void draw(Player& player, Bullet bullets[], int bulletSize, Enemy enemies[], int enemySize) {
// 清空屏幕
system("cls");
// 绘制玩家
cout << " ";
for (int i = 0; i < WIDTH - 2; i++) {
cout << "_";
}
cout << " " << endl;
for (int i = 0; i < HEIGHT - 1; i++) {
cout << "|";
for (int j = 0; j < WIDTH - 2; j++) {
if (player.pos.x == j && player.pos.y == i) {
cout << "O";
} else {
bool isBullet = false;
for (int k = 0; k < bulletSize; k++) {
if (bullets[k].active && bullets[k].pos.x == j && bullets[k].pos.y == i) {
cout << "|";
isBullet = true;
break;
}
}
if (!isBullet) {
bool isEnemy = false;
for (int k = 0; k < enemySize; k++) {
if (enemies[k].active && enemies[k].pos.x == j && enemies[k].pos.y == i) {
cout << "V";
isEnemy = true;
break;
}
}
if (!isEnemy) {
cout << " ";
}
}
}
}
cout << "|" << endl;
}
cout << " ";
for (int i = 0; i < WIDTH - 2; i++) {
cout << "-";
}
cout << " " << endl;
// 显示得分
cout << "Score: " << score << endl;
}
// 更新玩家的位置
void updatePlayer(Player& player) {
if (_kbhit()) {
switch (_getch()) {
case 'a': player.pos.x--; break;
case 'd': player.pos.x++; break;
}
}
if (player.pos.x < 0) {
player.pos.x = 0;
}
if (player.pos.x >= WIDTH - 2) {
player.pos.x = WIDTH - 3;
}
}
// 更新子弹的位置
void updateBullet(Bullet bullets[], int bulletSize) {
for (int i = 0; i < bulletSize; i++) {
if (bullets[i].active) {
bullets[i].pos.y--;
if (bullets[i].pos.y < 0) {
bullets[i].active = false;
}
}
}
}
// 更新敌机的位置
void updateEnemy(Enemy enemies[], int enemySize) {
for (int i = 0; i < enemySize; i++) {
if (enemies[i].active) {
enemies[i].pos.y++;
if (enemies[i].pos.y >= HEIGHT - 1) {
enemies[i].active = false;
}
}
}
}
// 发射子弹
void shoot(Bullet bullets[], int bulletSize, Player& player) {
for (int i = 0; i < bulletSize; i++) {
if (!bullets[i].active) {
bullets[i].active = true;
bullets[i].pos.x = player.pos.x;
bullets[i].pos.y = player.pos.y - 1;
break;
}
}
}
// 生成敌机
void generateEnemy(Enemy enemies[], int enemySize) {
int count = 0;
for (int i = 0; i < enemySize; i++) {
if (enemies[i].active) {
count++;
}
}
if (count >= MAX_ENEMY_NUM) {
return;
}
int x = rand() % (WIDTH - 2);
for (int i = 0; i < enemySize; i++) {
if (!enemies[i].active) {
enemies[i].active = true;
enemies[i].pos.x = x;
enemies[i].pos.y = 0;
break;
}
}
}
// 检测碰撞
void checkCollision(Player& player, Bullet bullets[], int bulletSize, Enemy enemies[], int enemySize) {
for (int i = 0; i < bulletSize; i++) {
if (bullets[i].active) {
for (int j = 0; j < enemySize; j++) {
if (enemies[j].active && bullets[i].pos.x == enemies[j].pos.x && bullets[i].pos.y == enemies[j].pos.y) {
bullets[i].active = false;
enemies[j].active = false;
score++;
}
}
}
}
for (int i = 0; i < enemySize; i++) {
if (enemies[i].active && player.pos.x == enemies[i].pos.x && player.pos.y == enemies[i].pos.y) {
cout << "Game Over!" << endl;
cout << "Your score is " << score << "." << endl;
exit(0);
}
}
}
int main() {
srand(time(NULL));
Player player;
Bullet bullets[100];
Enemy enemies[MAX_ENEMY_NUM];
initPlayer(player);
initBullet(bullets, 100);
initEnemy(enemies, MAX_ENEMY_NUM);
while (true) {
draw(player, bullets, 100, enemies, MAX_ENEMY_NUM);
updatePlayer(player);
updateBullet(bullets, 100);
updateEnemy(enemies, MAX_ENEMY_NUM);
generateEnemy(enemies, MAX_ENEMY_NUM);
checkCollision(player, bullets, 100, enemies, MAX_ENEMY_NUM);
Sleep(50);
}
return 0;
}
```
在游戏结束时,程序会输出玩家的得分,例如:
```
Game Over!
Your score is 10.
```
其中,10就是玩家的得分。
阅读全文