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

超有趣的 C++ 太空射击小游戏代码分享

### 🌌太空射击小游戏 以下是一个简单的太空射击小游戏的 C++ 代码示例,玩家控制飞船发射子弹来射击敌人。 ```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; } ``` ### 代码解释 1. **结构体定义**:定义了 `Spaceship`、`Enemy` 和 `Bullet` 结构体,分别表示飞船、敌人和子弹。 2. **清屏函数**:`clearScreen` 函数用于清除控制台屏幕。 3. **绘制函数**:`draw` 函数将飞船、敌人和子弹绘制到屏幕上。 4. **移动和射击函数**:`moveSpaceship` 用于移动飞船,`shoot` 用于发射子弹,`moveBullets` 和 `moveEnemies` 分别用于移动子弹和敌人。 5. **碰撞检测**:`checkCollisions` 函数检测子弹和敌人是否碰撞,若碰撞则移除相应的子弹和敌人。 6. **主循环**:在主循环中,不断更新游戏状态,包括生成敌人、移动元素、检测碰撞等,同时处理玩家的输入。 ### 运行方式 将上述代码复制到 C++ 编译器中编译运行,使用 `A` 和 `D` 键控制飞船左右移动,使用 `Space` 键发射子弹。 ######[AI问答 | 1477点数解答 | 2026-06-21 11:43:43]

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