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

优化后的太空射击小游戏:支持Space键发射子弹

### 代码优化说明 在之前的太空射击小游戏代码里已经支持用空格键(Space)发射子弹啦。不过为了让代码逻辑更清晰,我们可以对输入处理部分进行优化。 ### 🌟优化后的代码 ```cpp #include <iostream> #include <vector> #include <windows.h> // 用于控制台光标控制和延时 // 定义飞船结构体 struct Spaceship { int x; int y; }; // 定义敌人结构体 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) { 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; } } // 移动飞船 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; if (it->y < 0) { it = bullets.erase(it); } else { ++it; } } } // 移动敌人 void moveEnemies(std::vector<Enemy>& enemies) { for (auto& enemy : enemies) { ++enemy.y; } // 移除超出屏幕的敌人 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) { 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; break; } else { ++bulletIt; } } if (isHit) { enemyIt = enemies.erase(enemyIt); } else { ++enemyIt; } } } int main() { Spaceship ship = {40, 19}; std::vector<Enemy> enemies; std::vector<Bullet> bullets; // 游戏主循环 while (true) { clearScreen(); draw(ship, enemies, bullets); // 随机生成敌人 if (rand() % 100 < 5) { enemies.push_back({rand() % 80, 0}); } moveEnemies(enemies); moveBullets(bullets); checkCollisions(enemies, bullets); 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(100); } return 0; } ``` ### 代码运行方式 把代码复制到 C++ 编译器中编译运行,你就可以用 `A` 和 `D` 键控制飞船左右移动,按 `Space` 键发射子弹来射击敌人。 ######[AI问答 | 1303点数解答 | 2026-06-21 11:45:27]

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