酷代码 AI
菜单
服务商

帮我写个c++枪战游戏代码,要有颜色

以下是一个简单的C++控制台枪战游戏代码,使用Windows API实现颜色显示。游戏包含玩家移动、射击和敌人随机生成功能。 ```cpp #include <iostream> #include <conio.h> #include <windows.h> #include <vector> #include <cstdlib> #include <ctime> using namespace std; // 设置控制台颜色函数 void SetColor(int color) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); } // 游戏常量 const int WIDTH = 20; const int HEIGHT = 10; const char PLAYER = 'P'; const char ENEMY = 'E'; const char BULLET = '*'; const char EMPTY = ' '; // 玩家结构 struct Player { int x, y; Player(int startX, int startY) : x(startX), y(startY) {} }; // 子弹结构 struct Bullet { int x, y; bool active; Bullet(int startX, int startY) : x(startX), y(startY), active(true) {} }; // 敌人结构 struct Enemy { int x, y; bool active; Enemy(int startX, int startY) : x(startX), y(startY), active(true) {} }; vector<Bullet> bullets; vector<Enemy> enemies; Player player(WIDTH / 2, HEIGHT - 1); // 初始化游戏 void InitGame() { srand(time(0)); player.x = WIDTH / 2; player.y = HEIGHT - 1; bullets.clear(); enemies.clear(); } // 绘制游戏界面 void Draw() { system("cls"); // 清屏 for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (x == player.x && y == player.y) { SetColor(10); // 绿色玩家 cout << PLAYER; } else { bool drawn = false; // 绘制子弹 for (const auto& bullet : bullets) { if (bullet.active && bullet.x == x && bullet.y == y) { SetColor(14); // 黄色子弹 cout << BULLET; drawn = true; break; } } if (drawn) continue; // 绘制敌人 for (const auto& enemy : enemies) { if (enemy.active && enemy.x == x && enemy.y == y) { SetColor(12); // 红色敌人 cout << ENEMY; drawn = true; break; } } if (!drawn) { SetColor(7); // 默认灰色 cout << EMPTY; } } } cout << endl; } SetColor(7); // 恢复默认颜色 cout << "移动: A(左) D(右) 射击: Space 退出: Q" << endl; } // 更新游戏状态 void Update() { // 移动子弹 for (auto& bullet : bullets) { if (bullet.active) { bullet.y--; if (bullet.y < 0) bullet.active = false; } } // 移除无效子弹 bullets.erase(remove_if(bullets.begin(), bullets.end(), [](const Bullet& b) { return !b.active; }), bullets.end()); // 生成敌人 if (rand() % 10 == 0) { enemies.push_back(Enemy(rand() % WIDTH, 0)); } // 移动敌人 for (auto& enemy : enemies) { if (enemy.active) { enemy.y++; if (enemy.y >= HEIGHT) enemy.active = false; } } // 移除无效敌人 enemies.erase(remove_if(enemies.begin(), enemies.end(), [](const Enemy& e) { return !e.active; }), enemies.end()); // 检测子弹与敌人碰撞 for (auto& bullet : bullets) { for (auto& enemy : enemies) { if (bullet.active && enemy.active && bullet.x == enemy.x && bullet.y == enemy.y) { bullet.active = false; enemy.active = false; break; } } } // 检测玩家与敌人碰撞 for (const auto& enemy : enemies) { if (enemy.active && enemy.x == player.x && enemy.y == player.y) { cout << "游戏结束!被敌人击中。" << endl; exit(0); } } } // 处理输入 void ProcessInput() { if (_kbhit()) { char key = _getch(); switch (key) { case 'a': // 左移 if (player.x > 0) player.x--; break; case 'd': // 右移 if (player.x < WIDTH - 1) player.x++; break; case ' ': // 射击 bullets.push_back(Bullet(player.x, player.y - 1)); break; case 'q': // 退出 exit(0); break; } } } // 主游戏循环 int main() { InitGame(); while (true) { Draw(); ProcessInput(); Update(); Sleep(100); // 控制游戏速度 } return 0; } ``` 这个代码实现了一个基础的控制台枪战游戏: - 玩家使用'A'和'D'键左右移动,空格键射击。 - 敌人从顶部随机生成并向下移动。 - 子弹向上飞行,击中敌人后双方消失。 - 玩家与敌人碰撞会导致游戏结束。 - 使用Windows API设置颜色,玩家为绿色,敌人为红色,子弹为黄色。 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: - 内存管理:使用vector动态管理子弹和敌人对象,避免内存泄漏。 - 碰撞检测逻辑:确保子弹和敌人碰撞后正确标记为无效,防止重复检测。 - 跨平台兼容性:此代码依赖Windows API,在其他操作系统上可能需要调整颜色和输入处理部分。 [2025-10-03 15:06:13 | AI写代码神器 | 1355点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]
数独游戏 [娱乐类]
经典推箱子 [娱乐类]