news 2026/2/17 10:33:33

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/2/12 20:11:16

【计算机毕业设计案例】基于python_CNN卷积神经网络对猫狗识别基于python_CNN深度学习卷积神经网络对猫狗识别

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

作者头像 李华
网站建设 2026/2/16 14:08:04

电商api实战解析:1688.item_get_company 获取公司档案信息

一、接口定位item_get_company 不是“商品级”接口&#xff0c;而是“供应商级”接口。 输入&#xff1a;1688 商品 offerId 或 companyId&#xff08;二选一&#xff09; 输出&#xff1a;公司档案 60 字段&#xff0c;包括工商信息、深度认证、工厂能力、贸易能力、在线表现 …

作者头像 李华
网站建设 2026/2/13 1:38:37

【课程设计/毕业设计】基于python_CNN深度学习卷积神经网络对猫狗识别基于深度学习卷积神经网络对猫狗识别

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

作者头像 李华
网站建设 2026/2/6 0:04:47

【课程设计/毕业设计】基于python人工智能深度学习对狗表情训练识别基于深度学习对狗表情训练识别

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

作者头像 李华