酷代码 AI
菜单
服务商
DeepSeek字节豆包
更多选项

太空射击游戏:添加分数和血量显示,让游戏更刺激

### 💥添加分数和血量显示 为了在游戏中显示分数和飞船血量,我们需要对代码进行以下几个方面的修改: 1. 定义分数和血量变量。 2. 在绘制界面时显示分数和血量。 3. 在敌人与飞船碰撞时减少血量,当子弹击中敌人时增加分数。 4. 当血量为 0 时结束游戏。 以下是修改后的代码: ```cpp #include <iostream> #include <vector> #include <windows.h> // 用于控制台光标控制和延时 // 定义飞船结构体 struct Spaceship { int x; int y; int health; }; // 定义敌人结构体 struct Enemy { int x; int y; }; // 定义子弹结构体 struct Bullet { int x; int y; }; // 清屏函数 void clearScreen() { HANDLE hOut; COORD Position; hOut = GetStdHandle(STD_OUTPUT_HANDLE); Position.X = 0; Position.Y = 0; SetConsoleCursorPosition(hOut, Position); } // 绘制游戏界面 void draw(const Spaceship& ship, const std::vector<Enemy>& enemies, const std::vector<Bullet>& bullets, int score) { const int width = 80; const int height = 20; char screen[height][width]; // 初始化屏幕 for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { screen[y][x] = ' '; } } // 绘制飞船 screen[ship.y][ship.x] = '^'; // 绘制敌人 for (const auto& enemy : enemies) { screen[enemy.y][enemy.x] = 'V'; } // 绘制子弹 for (const auto& bullet : bullets) { screen[bullet.y][bullet.x] = '*'; } // 输出屏幕 for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { std::cout << screen[y][x]; } std::cout << std::endl; } // 显示分数和血量 std::cout << "Score: " << score << " Health: " << ship.health << std::endl; } // 移动飞船 void moveSpaceship(Spaceship& ship, char input) { if (input == 'a' && ship.x > 0) { --ship.x; } else if (input == 'd' && ship.x < 79) { ++ship.x; } } // 发射子弹 void shoot(std::vector<Bullet>& bullets, const Spaceship& ship) { bullets.push_back({ship.x, ship.y - 1}); } // 移动子弹 void moveBullets(std::vector<Bullet>& bullets) { for (auto it = bullets.begin(); it != bullets.end();) { // 增加子弹移动步长 it->y -= 2; if (it->y < 0) { it = bullets.erase(it); } else { ++it; } } } // 移动敌人 void moveEnemies(std::vector<Enemy>& enemies) { for (auto& enemy : enemies) { // 增加敌人移动步长 enemy.y += 2; } // 移除超出屏幕的敌人 for (auto it = enemies.begin(); it != enemies.end();) { if (it->y >= 20) { it = enemies.erase(it); } else { ++it; } } } // 检测碰撞 void checkCollisions(std::vector<Enemy>& enemies, std::vector<Bullet>& bullets, Spaceship& ship, int& score) { for (auto enemyIt = enemies.begin(); enemyIt != enemies.end();) { bool isHit = false; for (auto bulletIt = bullets.begin(); bulletIt != bullets.end();) { if (enemyIt->x == bulletIt->x && enemyIt->y == bulletIt->y) { bulletIt = bullets.erase(bulletIt); isHit = true; score += 10; // 击中敌人增加分数 break; } else { ++bulletIt; } } if (isHit) { enemyIt = enemies.erase(enemyIt); } else { // 检查敌人与飞船碰撞 if (enemyIt->x == ship.x && enemyIt->y == ship.y) { ship.health--; // 减少飞船血量 enemyIt = enemies.erase(enemyIt); } else { ++enemyIt; } } } } int main() { Spaceship ship = {40, 19, 3}; // 初始血量为 3 std::vector<Enemy> enemies; std::vector<Bullet> bullets; int score = 0; // 游戏主循环 while (ship.health > 0) { clearScreen(); draw(ship, enemies, bullets, score); // 随机生成敌人 if (rand() % 100 < 5) { enemies.push_back({rand() % 80, 0}); } moveEnemies(enemies); moveBullets(bullets); checkCollisions(enemies, bullets, ship, score); if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')) { moveSpaceship(ship, 'a'); } else if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')) { moveSpaceship(ship, 'd'); } if (GetAsyncKeyState(VK_SPACE)) { shoot(bullets, ship); } // 减少延迟时间 Sleep(50); } std::cout << "Game Over! Your score is: " << score << std::endl; return 0; } ``` ### 代码修改说明 1. **结构体修改**:在 `Spaceship` 结构体中添加了 `health` 成员来表示飞船的血量。 2. **绘制函数修改**:在 `draw` 函数中增加了分数和血量的显示。 3. **碰撞检测函数修改**:在 `checkCollisions` 函数中,当子弹击中敌人时增加分数,当敌人与飞船碰撞时减少飞船的血量。 4. **主函数修改**:在主函数中初始化飞船的血量为 3,并在血量为 0 时结束游戏,输出最终分数。 ### 运行方式 将上述代码复制到 C++ 编译器中编译运行,使用 `A` 和 `D` 键控制飞船左右移动,使用 `Space` 键发射子弹。在游戏过程中,屏幕下方会显示当前的分数和飞船的血量。当血量为 0 时,游戏结束并显示最终分数。 ######[AI问答 | 1714点数解答 | 2026-06-21 11:49:22]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]