news 2026/4/25 15:01:15

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/4/22 21:41:58

大模型微调技术详解:从全参数微调到RLHF的演进与应用

文章系统介绍了大模型微调技术的发展历程&#xff0c;从2018年全参数微调到2023年的偏好对齐技术&#xff0c;包括特征提取、Adapter、LoRA、提示微调、指令微调等方法。分析了各种微调技术的原理、特点和适用场景&#xff0c;解释了微调为何在大模型时代取代从零训练&#xff…

作者头像 李华
网站建设 2026/4/19 21:20:55

CC-Switch深度解析与Mac安装全指南:一键搞定AI编程工具配置切换

文章目录目录一、CC-Switch核心解析&#xff1a;为什么它能成为开发者必备&#xff1f;1. 工具定位与核心价值2. 核心功能亮点3. 与传统配置方式对比二、Mac安装全流程&#xff1a;从下载到启动零踩坑1. 安装前提准备2. 两种安装方式&#xff08;任选其一&#xff09;方式一&am…

作者头像 李华
网站建设 2026/4/24 0:21:05

兴趣岛1元试课靠谱吗?如何运作?拆解其在线教育系统逻辑

兴趣岛靠谱吗&#xff1f;在信息爆炸的时代&#xff0c;消费者面对任何商业模式时&#xff0c;往往容易陷入“非黑即白”的极端判断。近期&#xff0c;围绕兴趣岛的“一元试课”模式&#xff0c;社交媒体上出现不少争议声音&#xff0c;部分不好怀疑的声音将其标签化为“营销套…

作者头像 李华
网站建设 2026/4/20 12:06:03

【计算机毕业设计案例】机器学习基于python-AI深度学习对狗表情训练识别基于python-AI深度学习对狗表情训练识别

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

作者头像 李华
网站建设 2026/4/19 18:34:24

深度学习毕设项目推荐-基于python-AI深度学习对狗表情训练识别

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

作者头像 李华