news 2026/9/16 17:56:02

C++飞机大战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++飞机大战
#include <iostream> #include <vector> #include <conio.h> // 用于_kbhit和_getch #include <windows.h> // 用于Sleep和光标控制 #include <ctime> #include <cstdlib> using namespace std; // 全局常量 const int WIDTH = 40; const int HEIGHT = 20; const char PLAYER = 'A'; const char ENEMY = 'X'; const char BULLET = '|'; // 玩家类 class Player { public: int x, y; Player() : x(WIDTH / 2), y(HEIGHT - 2) {} void moveLeft() { if (x > 1) x--; } void moveRight() { if (x < WIDTH - 2) x++; } void moveUp() { if (y > 1) y--; } void moveDown() { if (y < HEIGHT - 2) y++; } }; // 子弹类 class Bullet { public: int x, y; bool active; Bullet(int x, int y) : x(x), y(y), active(true) {} void move() { y--; if (y <= 0) active = false; } }; // 敌人类 class Enemy { public: int x, y; bool active; Enemy(int x, int y) : x(x), y(y), active(true) {} void move() { y++; if (y >= HEIGHT - 1) active = false; } }; // 游戏类 class Game { private: Player player; vector<Bullet> bullets; vector<Enemy> enemies; int score; bool gameOver; public: Game() : score(0), gameOver(false) { srand(time(0)); } void hideCursor() { HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO info; info.dwSize = 100; info.bVisible = FALSE; SetConsoleCursorInfo(consoleHandle, &info); } void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void drawBorder() { for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < HEIGHT; i++) { cout << "#"; for (int j = 0; j < WIDTH; j++) { if (j == WIDTH - 1) cout << "#"; else cout << " "; } cout << endl; } for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; } void draw() { gotoxy(0, 0); drawBorder(); // 绘制玩家 gotoxy(player.x + 1, player.y + 1); cout << PLAYER; // 绘制子弹 for (int i = 0; i < bullets.size(); i++) { if (bullets[i].active) { gotoxy(bullets[i].x + 1, bullets[i].y + 1); cout << BULLET; } } // 绘制敌人 for (int i = 0; i < enemies.size(); i++) { if (enemies[i].active) { gotoxy(enemies[i].x + 1, enemies[i].y + 1); cout << ENEMY; } } // 绘制分数 gotoxy(WIDTH + 3, 1); cout << "Score: " << score; // 绘制控制说明 gotoxy(WIDTH + 3, 3); cout << "Controls:"; gotoxy(WIDTH + 3, 4); cout << "A/D - Move Left/Right"; gotoxy(WIDTH + 3, 5); cout << "W/S - Move Up/Down"; gotoxy(WIDTH + 3, 6); cout << "Space - Shoot"; gotoxy(WIDTH + 3, 7); cout << "X - Quit"; } void input() { if (_kbhit()) { char ch = _getch(); switch (ch) { case 'a': case 'A': player.moveLeft(); break; case 'd': case 'D': player.moveRight(); break; case 'w': case 'W': player.moveUp(); break; case 's': case 'S': player.moveDown(); break; case ' ': bullets.push_back(Bullet(player.x, player.y)); break; case 'x': case 'X': gameOver = true; break; } } } void logic() { // 移动子弹 for (int i = 0; i < bullets.size(); i++) { if (bullets[i].active) { bullets[i].move(); if (!bullets[i].active) { bullets.erase(bullets.begin() + i); i--; } } } // 生成敌人 if (rand() % 10 == 0) { int x = rand() % (WIDTH - 2) + 1; enemies.push_back(Enemy(x, 1)); } // 移动敌人 for (int i = 0; i < enemies.size(); i++) { if (enemies[i].active) { enemies[i].move(); if (!enemies[i].active) { enemies.erase(enemies.begin() + i); i--; } } } // 碰撞检测:子弹与敌人 for (int i = 0; i < bullets.size(); i++) { for (int j = 0; j < enemies.size(); j++) { if (bullets[i].active && enemies[j].active) { if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) { bullets[i].active = false; enemies[j].active = false; score += 10; } } } } // 移除不活跃的子弹 for (int i = 0; i < bullets.size(); i++) { if (!bullets[i].active) { bullets.erase(bullets.begin() + i); i--; } } // 移除不活跃的敌人 for (int i = 0; i < enemies.size(); i++) { if (!enemies[i].active) { enemies.erase(enemies.begin() + i); i--; } } // 检查玩家与敌人碰撞 for (int i = 0; i < enemies.size(); i++) { if (enemies[i].active) { if (enemies[i].x == player.x && enemies[i].y == player.y) { gameOver = true; } } } } void run() { hideCursor(); while (!gameOver) { draw(); input(); logic(); Sleep(100); // 控制游戏速度 } gotoxy(WIDTH / 2 - 5, HEIGHT / 2); cout << "GAME OVER!"; gotoxy(WIDTH / 2 - 5, HEIGHT / 2 + 1); cout << "Final Score: " << score; gotoxy(0, HEIGHT + 3); } }; int main() { Game game; game.run(); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/3 1:31:42

基于Simulink的混合PO与INC切换MPPT策略仿真

目录 手把手教你学Simulink 一、引言&#xff1a;为什么需要“混合P&O与INC”&#xff1f; 二、系统整体架构 控制流程&#xff1a; 三、核心算法详解 1. P&O 算法&#xff08;扰动观察法&#xff09; 2. INC 算法&#xff08;电导增量法&#xff09; 3. 光照突…

作者头像 李华
网站建设 2026/9/5 11:11:26

工业场景中弧形导轨的安装要点

弧形导轨作为工业自动化中实现弧线运动的核心部件&#xff0c;常用于机械臂关节、旋转工作台、自动化生产线转弯部位&#xff0c;医疗CT机的旋转扫描部件也依赖高精度弧形导轨实现平滑运动。其安装质量直接影响设备运行精度与寿命&#xff0c;从材料准备到定位调试&#xff0c;…

作者头像 李华
网站建设 2026/9/14 1:54:39

2026年你应该掌握的进阶版 Gemini CLI 实用指南

现在用AI&#xff0c;只会在网页版当聊天机器人用吗&#xff1f;那你就out了。 Gemini CLI 是 Google 推出的终端 AI 助手。相比于网页版&#xff0c;命令行工具在处理本地文件、读取项目上下文方面有着天然优势。对于开发者而言&#xff0c;它不仅仅是一个聊天机器人&#xf…

作者头像 李华
网站建设 2026/9/15 9:36:17

ppo 找出口模型 训练 笔记 26/1/13

cnn模型大小我给他控制在训练耗时30s左右&#xff08;4060ti&#xff09; 动作空间6个&#xff1a;4个移动2个转头&#xff0c;因为一开始都要跑一遍&#xff0c;动作太多需要跑更多步才能吃到正反馈 我现在设置是60步一episode&#xff0c;5 episode训练一次 转头70度左右&…

作者头像 李华
网站建设 2026/9/13 5:34:18

深度学习计算机毕设之基于python-AI机器学习对狗表情训练识别基于python-AI深度学习对狗表情训练识别

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华