news 2026/3/16 11:15:24

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/3/5 12:35:29

基于STSW-LINK007工具的STLink升级实战案例

STLink固件升级实战&#xff1a;从避坑到精通的完整指南 在嵌入式开发的世界里&#xff0c;调试器就像医生的听诊器——看不见它的时候风平浪静&#xff0c;一旦“连不上”、“下不进程序”&#xff0c;整个项目进度立刻停摆。而当你发现原因竟是 调试器固件太老不支持新芯片…

作者头像 李华
网站建设 2026/3/16 2:35:29

大语言模型完整技术栈:从理论到实践的全面指南

本文全面介绍了大语言模型(LLM)的核心技术&#xff0c;包括Transformer架构、预训练挑战、分布式训练方法。重点讲解了参数高效微调(PEFT)技术如LoRA和QLoRA&#xff0c;以及提示工程策略。同时详细探讨了模型优化技术&#xff0c;包括量化(GPTQ、NF4、GGML)、蒸馏和剪枝&#…

作者头像 李华
网站建设 2026/3/12 5:35:06

项目一多就混乱?试试把大目标拆成7层小动作

我见过太多这样的现场&#xff1a;每天早会一开&#xff0c;大家低头刷手机&#xff0c;汇报永远是“差不多完成了”、“快了快了”&#xff1b;群里消息满天飞&#xff0c;每个人都在跟进&#xff0c;但项目依旧卡在原地&#xff1b;老板问一句&#xff1a;“现在到底卡在哪&a…

作者头像 李华
网站建设 2026/3/4 7:10:04

国产力量崛起:2026十大本土HR软件厂商深度盘点

在信息技术应用创新和供应链安全的国家战略指引下&#xff0c;中国人力资源软件市场正经历一场深刻的国产化替代浪潮。曾经由SAP、Oracle等国际巨头主导的HR系统市场&#xff0c;如今已涌现出一批技术领先、服务扎实的国产优秀厂商。这些本土企业不仅深谙中国企业管理特色&…

作者头像 李华