用面向对象思想写出c++飞机大战

时间: 2024-04-07 10:29:38 浏览: 9
好的,下面是使用面向对象思想写出的C++飞机大战: ```c++ #include <iostream> #include <conio.h> #include <windows.h> #include <vector> using namespace std; const int width = 30; const int height = 20; class GameObject { public: int x, y; char shape; GameObject(int _x, int _y, char _shape) : x(_x), y(_y), shape(_shape) {} }; class Player : public GameObject { public: Player(int _x, int _y, char _shape) : GameObject(_x, _y, _shape) {} void Move(int dx, int dy) { x += dx; y += dy; if (x < 0) x = 0; if (x >= width) x = width - 1; if (y < 0) y = 0; if (y >= height) y = height - 1; } }; class Enemy : public GameObject { public: Enemy(int _x, int _y, char _shape) : GameObject(_x, _y, _shape) {} void Move() { y++; if (y >= height) { y = 0; x = rand() % width; } } }; class Bullet : public GameObject { public: Bullet(int _x, int _y, char _shape) : GameObject(_x, _y, _shape) {} void Move() { y--; } }; class Game { private: Player player; vector<Enemy> enemies; vector<Bullet> bullets; int score; bool gameover; public: Game() : player(width / 2, height - 1, 'A'), score(0), gameover(false) {} void Input() { if (_kbhit()) { switch (_getch()) { case 'a': player.Move(-1, 0); break; case 'd': player.Move(1, 0); break; case 'w': player.Move(0, -1); break; case 's': player.Move(0, 1); break; case ' ': bullets.push_back(Bullet(player.x, player.y - 1, '*')); break; case 'q': gameover = true; break; } } } void Logic() { // move enemies for (int i = 0; i < enemies.size(); i++) { enemies[i].Move(); // check collision with player if (enemies[i].x == player.x && enemies[i].y == player.y) { gameover = true; } } // move bullets for (int i = 0; i < bullets.size(); i++) { bullets[i].Move(); // check collision with enemies for (int j = 0; j < enemies.size(); j++) { if (bullets[i].x == enemies[j].x && bullets[i].y == enemies[j].y) { enemies.erase(enemies.begin() + j); bullets.erase(bullets.begin() + i); score++; break; } } } // add new enemy if (enemies.size() < 10) { enemies.push_back(Enemy(rand() % width, 0, 'E')); } } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == player.y && j == player.x) cout << player.shape; else { bool enemy_here = false; for (int k = 0; k < enemies.size(); k++) { if (enemies[k].x == j && enemies[k].y == i) { cout << enemies[k].shape; enemy_here = true; break; } } if (!enemy_here) { bool bullet_here = false; for (int k = 0; k < bullets.size(); k++) { if (bullets[k].x == j && bullets[k].y == i) { cout << bullets[k].shape; bullet_here = true; break; } } if (!bullet_here) cout << " "; } } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score: " << score << endl; } void Run() { while (!gameover) { Input(); Logic(); Draw(); Sleep(50); } cout << "Game over!" << endl; } }; int main() { srand(time(NULL)); Game game; game.Run(); return 0; } ``` 这个版本的飞机大战使用了类和对象,将游戏中的飞机、敌机和子弹都封装成了类,并且使用了继承和多态的概念,使得代码更加清晰、易于维护。同时,将游戏逻辑分离到Game类中,使得代码结构更加合理。

相关推荐

最新推荐

recommend-type

【长春理工大学】面向对象程序设计下期末复习浏览题.pdf

这个是我本人在大一下学期期间整理的C++题库,涵盖机考(2018级及以后的南区软件工大一下学期程面向对象程序设计期末考试题库)中几乎所有的题目,并配有解析,方便记忆,考试这个东西……最主要还是自己会,我自己...
recommend-type

C++面向对象实现五子棋小游戏

本文介绍了如何运用面向对象思想进行五子棋游戏的设计与开发,与面向过程程序设计比较,面向对象程序设计更易于实现对现实世界的描述,提高软件的扩展性和可维护性。附上最终的程序源码,推荐给大家,有需要的小伙伴...
recommend-type

面向对象程序设计(c++)课程设计实验报告

熟练掌握面向对象程序设计的编程思想,能够使用c++语法在编程中使用类、继承、重载、多态、模板等概念;熟练使用VC等集成环境进行编程。 要求: 1. 利用面向对象的方法对业务进行系统分析; 2. 对各个功能模块分析...
recommend-type

C++中异常处理的基本思想及throw语句抛出异常的使用

主要介绍了C++中异常处理的基本思想及throw类抛出异常的使用,也深入谈到了异常被抛出后的栈解旋unwinding过程,需要的朋友可以参考下
recommend-type

C++_面向对象程序设计_谭浩强 word完整版

第1篇 基 本 知 识 第1章 C++的初步知识 第2章 数据类型与表达式 第2篇 面向过程的程序设计 ... 第4篇 面向对象的程序设计 第11章 继承与派生 第12章 多态性与虚函数 第13章 输入输出流 第14章 C++工具
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。