#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; }C++飞机大战
张小明
前端开发工程师
基于STSW-LINK007工具的STLink升级实战案例
STLink固件升级实战:从避坑到精通的完整指南 在嵌入式开发的世界里,调试器就像医生的听诊器——看不见它的时候风平浪静,一旦“连不上”、“下不进程序”,整个项目进度立刻停摆。而当你发现原因竟是 调试器固件太老不支持新芯片…
大语言模型完整技术栈:从理论到实践的全面指南
本文全面介绍了大语言模型(LLM)的核心技术,包括Transformer架构、预训练挑战、分布式训练方法。重点讲解了参数高效微调(PEFT)技术如LoRA和QLoRA,以及提示工程策略。同时详细探讨了模型优化技术,包括量化(GPTQ、NF4、GGML)、蒸馏和剪枝&#…
1 行代码搭 Agentic 大模型应用?这场直播教你 30 分钟快速上手!
还在愁大模型应用开发门槛高?不需要复杂背景,不用重工程经验 —— 1 月 15 日(周四)17:00-18:00,AtomGit 联合 LazyLLM 带来「LazyLLM Agentic 应用开发快速上手」直播!🎙️ 直播亮点由 LazyLLM…
项目一多就混乱?试试把大目标拆成7层小动作
我见过太多这样的现场:每天早会一开,大家低头刷手机,汇报永远是“差不多完成了”、“快了快了”;群里消息满天飞,每个人都在跟进,但项目依旧卡在原地;老板问一句:“现在到底卡在哪&a…
国产力量崛起:2026十大本土HR软件厂商深度盘点
在信息技术应用创新和供应链安全的国家战略指引下,中国人力资源软件市场正经历一场深刻的国产化替代浪潮。曾经由SAP、Oracle等国际巨头主导的HR系统市场,如今已涌现出一批技术领先、服务扎实的国产优秀厂商。这些本土企业不仅深谙中国企业管理特色&…
【毕业设计】基于深度学习对狗表情训练识别基于python-AI深度学习对狗表情训练识别
博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…