发一段关于飞机大战的代码吧!
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <chrono>
#include <thread>
#include <Windows.h>
#include <GL/glut.h>
using namespace std;
int playerHealth = 100; // 添加玩家生命值
int enemyHealth = 50; // 添加敌机生命值
// 定义常量
const int kScreenWidth = 80;
const int kScreenHeight = 25;
const int kPlayerStartX = kScreenWidth / 2;
const int kPlayerStartY = kScreenHeight - 5;
const int kNumEnemies = 5;
const int kCollisionThreshold = 2;
const int kOutputFrequency = 100;
const int kGameSpeed = 50;
enum Direction {
LEFT,
RIGHT
};
enum ObjectType {
PLAYER,
ENEMY
};
class Bullet {
private:
int x, y; // 子弹坐标
bool active; // 子弹是否激活
public:
Bullet() : x(0), y(0), active(false) {}
void activate(int startX, int startY) {
x = startX;
y = startY;
active = true;
}
void deactivate() {
active = false;
}
bool isActive() const {
return active;
}
int getX() const {
return x;
}
int getY() const {
return y;
}
void move() {
// 子弹向上移动
if (active && y > 0) {
y--;
} else {
deactivate();
}
}
};
class Aircraft {
private:
int x, y; // 飞机坐标
int health; // 飞机生命值
Bullet bullet; // 飞机射击的子弹
public:
Aircraft(int startX, int startY) : x(startX), y(startY), health(100) {}
int getX() const {
return x;
}
int getY() const {
return y;
}
int getHealth() const {
return health;
}
void move(Direction direction) {
if (direction == LEFT && x > 0) {
x--;
} else if (direction == RIGHT && x < kScreenWidth - 1) {
x++;
}
}
void shoot() {
if (!bullet.isActive()) {
bullet.activate(x, y);
}
}
void updateBullet() {
bullet.move();
}
const Bullet& getBullet() const {
return bullet;
}
void deactivateBullet() {
bullet.deactivate();
}
};
class EnemyAircraft {
private:
int x, y; // 敌机坐标
int health; // 敌机生命值
Bullet bullet; // 敌机射击的子弹
public:
EnemyAircraft(int startX, int startY) : x(startX), y(startY), health(50) {}
int getX() const {
return x;
}
int getY() const {
return y;
}
int getHealth() const {
return health;
}
void move() {
if (y < kScreenHeight - 1) {
y++;
} else {
y = 0;
x = rand() % kScreenWidth;
}
}
void shoot() {
if (!bullet.isActive()) {
bullet.activate(x, y);
}
}
void updateBullet() {
bullet.move();
}
const Bullet& getBullet() const {
return bullet;
}
void deactivateBullet() {
bullet.deactivate();
}
};
class Game {
private:
int playerHealth;
int enemyHealth;
int playerScore; // 新增:玩家得分
int highestScore; // 新增:最高分
int outputCounter;
Aircraft player;
vector<EnemyAircraft> enemies;
public:
Game() : playerHealth(100), enemyHealth(50), playerScore(0), highestScore(0), outputCounter(0), player(40, 20) {
for (int i = 0; i < kNumEnemies; ++i) {
enemies.emplace_back(rand() % kScreenWidth, rand() % (kScreenHeight - 1));
}
// 输出控制说明
cout << "=== Aircraft Game ===" << endl;
cout << "Controls:" << endl;
cout << " - Move left: 'a'" << endl;
cout << " - Move right: 'd'" << endl;
cout << " - Shoot: 'Spacebar'" << endl;
cout << "=====================" << endl;
}
// 处理用户输入
void handleInput() {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'a':
player.move(LEFT);
break;
case 'd':
player.move(RIGHT);
break;
case ' ':
player.shoot();
break;
default:
break;
}
}
}
// 更新游戏状态
void updateGame() {
handleInput();
player.updateBullet();
// 每次循环重新初始化outputCounter
outputCounter = 0;
// 更新敌机状态
for (auto& enemy : enemies) {
enemy.move();
enemy.shoot();
enemy.updateBullet();
// 如果玩家的子弹击中敌机,增加得分
if (player.getBullet().isActive()) {
for (auto& enemy : enemies) {
if (abs(player.getBullet().getX() - enemy.getX()) < 2 &&
abs(player.getBullet().getY() - enemy.getY()) < 2) {
playerScore += 10; // 增加得分
enemyHealth -= 10;
player.deactivateBullet();
}
}
} else {
// Deduct 5 points for each enemy aircraft not hit
playerScore -= 5;
}
// 碰撞检测,如果玩家子弹和敌机坐标相近,则减少敌机生命值
if (player.getBullet().isActive() &&
abs(player.getBullet().getX() - enemy.getX()) < 2 &&
abs(player.getBullet().getY() - enemy.getY()) < 2) {
enemyHealth -= 10;
player.deactivateBullet(); // 玩家的子弹失效
}
// 碰撞检测,如果敌机子弹和玩家飞机坐标相近,则减少玩家飞机生命值
if (enemy.getBullet().isActive() &&
abs(enemy.getBullet().getX() - player.getX()) < kCollisionThreshold&&
abs(enemy.getBullet().getY() - player.getY()) < kCollisionThreshold) {
playerHealth -= 10;
enemy.deactivateBullet(); // 敌机的子弹失效
}
}
// 更新最高分
highestScore = max(highestScore, playerScore);
//outputCounter 控制了生命值的输出频率,每10次循环输出一次
if (outputCounter % kOutputFrequency == 0) {
cout << "Player Health: " << playerHealth << endl;
cout << "Enemy Health: " << enemyHealth << endl;
// 输出得分和最高分
cout << "Player Score: " << playerScore << endl;
cout << "Highest Score: " << highestScore << endl;
}
outputCounter++;
}
// 渲染游戏界面
void render() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// 绘制玩家
glColor3f(1.0f, 1.0f, 1.0f); // 设置颜色为白色
glRectf(static_cast<float>(player.getX()), static_cast<float>(player.getY()),
static_cast<float>(player.getX() + 1), static_cast<float>(player.getY() + 1));
// 绘制玩家子弹
if (player.getBullet().isActive()) {
glColor3f(1.0f, 0.0f, 0.0f); // 设置颜色为红色
glRectf(static_cast<float>(player.getBullet().getX()), static_cast<float>(player.getBullet().getY()),
static_cast<float>(player.getBullet().getX() + 1), static_cast<float>(player.getBullet().getY() + 1));
}
// 绘制敌机
for (const auto& enemy : enemies) {
glColor3f(0.0f, 0.0f, 1.0f); // 设置颜色为蓝色
glRectf(static_cast<float>(enemy.getX()), static_cast<float>(enemy.getY()),
static_cast<float>(enemy.getX() + 1), static_cast<float>(enemy.getY() + 1));
// 绘制敌机子弹
if (enemy.getBullet().isActive()) {
glColor3f(0.0f, 1.0f, 0.0f); // 设置颜色为绿色
glRectf(static_cast<float>(enemy.getBullet().getX()), static_cast<float>(enemy.getBullet().getY()),
static_cast<float>(enemy.getBullet().getX() + 1), static_cast<float>(enemy.getBullet().getY() + 1));
}
}
glutSwapBuffers();
}
// 运行游戏主循环
void run() {
glutDisplayFunc([]() { instance->render(); }); // 注册渲染函数
while (playerHealth > 0) {
updateGame();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
glutPostRedisplay(); // 触发重新绘制
}
// 游戏结束画面
system("cls");
cout << "=====================" << endl;
cout << " Game Over " << endl;
cout << "=====================" << endl;
cout << "Final Score: " << playerScore << endl;
cout << "Highest Score: " << highestScore << endl;
}
};
Game* Game::instance;
int main(int argc, char** argv) {
srand(static_cast<unsigned>(time(nullptr)));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(kScreenWidth, kScreenHeight);
glutCreateWindow("Aircraft Game");
Game game;
Game::instance = &game; // 设置Game类的静态成员变量
glutDisplayFunc([]() { instance->render(); }); // 注册渲染函数
glutMainLoop(); // 进入GLUT主循环
return 0;
}