c++实现飞机大战游戏 面向过程
时间: 2023-07-30 16:07:23 浏览: 158
基于C++实现简易飞机大战小游戏【100012171】
好的,下面是一个简单的 C++ 面向过程实现飞机大战游戏的示例代码:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 25;
int score = 0;
struct Position {
int x;
int y;
};
struct Bullet {
Position pos;
bool active;
};
struct Enemy {
Position pos;
bool active;
};
struct Player {
Position pos;
int lives;
};
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void drawPlayer(Player player) {
gotoxy(player.pos.x, player.pos.y);
cout << " ^ ";
gotoxy(player.pos.x, player.pos.y + 1);
cout << " ||| ";
}
void erasePlayer(Player player) {
gotoxy(player.pos.x, player.pos.y);
cout << " ";
gotoxy(player.pos.x, player.pos.y + 1);
cout << " ";
}
void movePlayer(Player& player, int direction) {
erasePlayer(player);
switch (direction) {
case 0: // Up
if (player.pos.y > 0) {
player.pos.y--;
}
break;
case 1: // Down
if (player.pos.y < SCREEN_HEIGHT - 2) {
player.pos.y++;
}
break;
case 2: // Left
if (player.pos.x > 0) {
player.pos.x--;
}
break;
case 3: // Right
if (player.pos.x < SCREEN_WIDTH - 4) {
player.pos.x++;
}
break;
}
drawPlayer(player);
}
void drawBullet(Bullet bullet) {
gotoxy(bullet.pos.x, bullet.pos.y);
cout << "^";
}
void eraseBullet(Bullet bullet) {
gotoxy(bullet.pos.x, bullet.pos.y);
cout << " ";
}
void moveBullet(Bullet& bullet) {
eraseBullet(bullet);
bullet.pos.y--;
drawBullet(bullet);
}
void drawEnemy(Enemy enemy) {
gotoxy(enemy.pos.x, enemy.pos.y);
cout << "v";
}
void eraseEnemy(Enemy enemy) {
gotoxy(enemy.pos.x, enemy.pos.y);
cout << " ";
}
void moveEnemy(Enemy& enemy) {
eraseEnemy(enemy);
enemy.pos.y++;
drawEnemy(enemy);
}
bool isCollision(Bullet bullet, Enemy enemy) {
if (bullet.active && enemy.active) {
return bullet.pos.x == enemy.pos.x && bullet.pos.y == enemy.pos.y;
}
return false;
}
void updateScore() {
gotoxy(SCREEN_WIDTH - 10, 0);
cout << "Score: " << score;
}
int main() {
srand(time(NULL));
// Initialize player
Player player;
player.pos.x = SCREEN_WIDTH / 2 - 1;
player.pos.y = SCREEN_HEIGHT - 2;
player.lives = 3;
// Initialize bullets
const int MAX_BULLETS = 5;
Bullet bullets[MAX_BULLETS];
for (int i = 0; i < MAX_BULLETS; i++) {
bullets[i].active = false;
}
// Initialize enemies
const int MAX_ENEMIES = 5;
Enemy enemies[MAX_ENEMIES];
for (int i = 0; i < MAX_ENEMIES; i++) {
enemies[i].active = false;
}
// Game loop
bool gameOver = false;
while (!gameOver) {
// Handle user input
if (_kbhit()) {
char ch = _getch();
switch (ch) {
case 'w':
movePlayer(player, 0);
break;
case 's':
movePlayer(player, 1);
break;
case 'a':
movePlayer(player, 2);
break;
case 'd':
movePlayer(player, 3);
break;
case ' ':
// Find inactive bullet
for (int i = 0; i < MAX_BULLETS; i++) {
if (!bullets[i].active) {
bullets[i].active = true;
bullets[i].pos.x = player.pos.x + 1;
bullets[i].pos.y = player.pos.y - 1;
break;
}
}
break;
default:
break;
}
}
// Move bullets
for (int i = 0; i < MAX_BULLETS; i++) {
if (bullets[i].active) {
moveBullet(bullets[i]);
// Check for collisions with enemies
for (int j = 0; j < MAX_ENEMIES; j++) {
if (isCollision(bullets[i], enemies[j])) {
bullets[i].active = false;
enemies[j].active = false;
score++;
updateScore();
}
}
// Check if bullet is out of screen
if (bullets[i].pos.y <= 0) {
bullets[i].active = false;
}
}
}
// Move enemies and spawn new ones
for (int i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i].active) {
moveEnemy(enemies[i]);
// Check for collisions with player
if (enemies[i].pos.y >= SCREEN_HEIGHT - 2) {
enemies[i].active = false;
player.lives--;
if (player.lives == 0) {
gameOver = true;
}
}
}
else {
// Spawn new enemy
enemies[i].active = true;
enemies[i].pos.x = rand() % (SCREEN_WIDTH - 2) + 1;
enemies[i].pos.y = 1;
}
}
// Draw player, bullets, and enemies
drawPlayer(player);
for (int i = 0; i < MAX_BULLETS; i++) {
if (bullets[i].active) {
drawBullet(bullets[i]);
}
}
for (int i = 0; i < MAX_ENEMIES; i++) {
if (enemies[i].active) {
drawEnemy(enemies[i]);
}
}
// Sleep to control game speed
Sleep(50);
}
// Game over
gotoxy(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2);
cout << "Game Over" << endl;
gotoxy(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2 + 1);
cout << "Score: " << score << endl;
gotoxy(SCREEN_WIDTH / 2 - 7, SCREEN_HEIGHT / 2 + 3);
cout << "Press any key to exit" << endl;
// Wait for user input before exiting
_getch();
return 0;
}
```
这是一个简单的实现,仅供参考。在实际开发中,你需要根据自己的需求和技能水平进行适当的修改和调整。
阅读全文