C++课程设计报告(飞机大战).docx

上传人(卖家):四川天地人教育 文档编号:1500581 上传时间:2021-06-21 格式:DOCX 页数:48 大小:57.84KB
下载 相关 举报
C++课程设计报告(飞机大战).docx_第1页
第1页 / 共48页
C++课程设计报告(飞机大战).docx_第2页
第2页 / 共48页
C++课程设计报告(飞机大战).docx_第3页
第3页 / 共48页
C++课程设计报告(飞机大战).docx_第4页
第4页 / 共48页
C++课程设计报告(飞机大战).docx_第5页
第5页 / 共48页
亲,该文档总共48页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、C+程序设计 A2 课 程 设 计 报 告 课程名称C+程序设计 A2 课题名称飞机大战 专业计算机科学与技术 班级1820544 学号182054424 姓名王锐_ 指导教师韩燕丽 2019 年 6 月 15 日 (一) 、课程设计题目: 飞机大战 (二) 、目的与要求: 1、目的: (1)要求学生达到熟练掌握C+语言的基本知识和技能; (2)基本掌握面向对象程序设计的基本思路和方法; (3)能够利用所学的基本知识和技能,解决简单的面向对象程序设计问题。 2、基本要求: (1)要求利用 面向对象的方法 以及 C+的编程思路来完成系统的设计。 (2)在系统的设计中,要求运用面向对象的机制(继承

2、及多态性 ) ,来实现系统功 能,并且要建立清晰的类层次结构。 (3)在系统设计中要分析和定义各个类,每个类中要有各自的数据成员和成员数。 (4)主函数中提供菜单选项 ,并给出足够的选择信息以及提示信息。 (5)程序具有一定的 健壮性 ,不会因为用户的输入错误引起程序运行错误而中断 执行。对输入值的类型、大小范围、字符串的长度等,进行正确性检查,对不合法 的输入值给出出错信息,指出错误类型,等待重新输入。 3、创新要求: 在基本要求达到后,可进行创新设计,如根据查找结果进行修改的功能。 (三) 、设计方法和基本原理: 功能要求: 设计一个基于控制台的简洁流畅的飞机大战游戏。 问题的解决方案:

3、根据系统功能要求,可以将问题解决分为以下步骤: (1)应用系统分析,建立该系统的功能模块框图以及界面的组织和设计; (2)分析系统中的各个实体及它们之间的关系包括属性和行为; (3)根据问题描述,设计系统的类层次; (4)完成类层次中各个类的描述(包括属性和方法) ; (5)完成类中各个成员函数的定义; (6)完成系统的应用模块; (7)功能调试; (8)完成系统总结报告以及系统使用说明书 目录 1.系统需求分析 .1 . 2.总体设计 .2 . 3.详细设计及实现 .5. 4.系统调试 .5 . 5.结论 .错 误!未定义书签。 6.心得体会 .错 误!未定义书签。 7.参考书目 .4 .5

4、 . 1. 系统需求分析 1.1系统设计的目的与意义: 飞机大战游戏作为 PC 中一款桌面游戏,界面简洁流畅、游戏方式简单玩起来容 易上手。面向休闲游戏的玩家,例如:家庭、办公人员等,使用人群广泛。 1.2 系统功能需求: 1)通过键盘,方向键 WASD键可控制战机的位置,空格键发射子弹, 2)界面中敌机出现的位置是随机的。 3)对于随机产生的敌机及子弹,若超出界面,则释放该对象。 4)战机、敌机、子弹都有生命值,敌机和玩家碰撞、敌机和玩家的子弹碰撞、敌 机的子弹和玩家碰撞、敌机的子弹和玩家的子弹碰撞均需考虑。敌机的生命值为0,则 删除敌机或 BOSS ,战机生命值为 0,游戏结束。 5)战机

5、子弹:战机有两种子弹可自由选择。 6)结束功能:游戏中按下2 会结束游戏,再次按下Y/N 可继续或者退出游戏。 2. 总体设计 2.1 系统功能分析: 系统通过调用类函数来实现游戏的运行,通过面向对象类的继承、多态等思想在使 程序简洁的同时保证游戏的正常运行。 2.2 系统功能模块划分与设计: 系统定义了 Bullet()、Plane() 两个基类,通过继承得到 Bullet_1()、Bullet_2()、 Bullet_3()、 Bullet_3()、Bullet()_4、Player() 等类。Map 类用于地图的绘制, Bullet 类用于子弹的实现, Plane 类是飞机的实现,在通过

6、Plane类的继承,编写敌机和战 机类。 2.3 系统功能模块图: 飞机大战 飞子地 机弹图 类类类 敌战 BOSS 机机 机 敌战 BOSS 敌 机机 机 机 子子 子 绘 弹弹 弹 制 战 BOSS 机 机 绘 绘 制 制 2.4 类的设计 (1)Bullet类(抽象类)的设计 : bool isOut; bool isDead; int atk; int m_hp_b; Note* noo; vector* m_bull; virtual void draw_on_map(Map* p) = 0; virtual void clear(Map* m) = 0; virtual void

7、init(int x, int y) = 0; virtual void upDate(Map* m) = 0; virtual void overstep(Map* m) = 0; ( 2 )Plane 类的设计 : bool isOut; bool isDead; int atk; int m_hp; char direction; Note* noo; vector* m_data; virtual void draw_on_map(Map* p) = 0; virtual void clear(Map* m) = 0; virtual void init() = 0; virtual

8、void upDate(Map* m) = 0; virtual void is_game_over() = 0; (3) Map类的设计 : char* map; int m_row; int m_clo; void draw(); void change_map(int, int, Type); Map* m; Player* p_m; bool game_over; vector* m_enemy; void draw(); void init(); void up_date(); bool get_aircraft(); bool get_boss(); void gameStart(

9、); void gameAgain(int playerHP); int intervalEnemy_Aircraft(); int currentEnemy_Aircraft(); int intervalEnemy_Boss(); int currentEnemy_Boss(); (4) Manager类的设计 : 3. 详细设计及实现 3.1Public_using 及 stdafx 的实现 / Public_Using.h #pragmaonce / 组成飞机和子弹的节点 struct Note int m_x; int m_y; Note(int x, int y):m_x(x),

10、m_y(y) ; / 枚举类型(自己定义) enumType KONG,HEAD_F,LEFT_F,RIGHT_F,XUNL_F ,OBSTACLE,BULLET,SMALL,BOSS,MIND ,B_U_HEAD,SHENTI,B_HEAD,B_LEFT,B_RIGHT ,BOOS,PLAYER,A,B,C,D,E,F,G,H,I,J,K ,BULLET_4_HEAK,BULLET_4_SHENTI ; / stdafx.h / stdafx.h : 标准系统包含文件的包含文件, / 或是经常使用但不常更改的 / 特定于项目的包含文件 / #pragmaonce #include targe

11、tver.h #include #include #include #include #include #include #include #include usingnamespacestd; / TODO: 在此处引用程序需要的其他头文件 3.2 地图类的实现 / Map.h #pragmaonce #include Public_Using.h classMap public: Map(int x=25, int y=30); Map(void); void draw(); void change_mapi(nt x, int y ,Type t);/地图的改变 char* map;/地

12、图二维指针 int m_row;/行 int m_clo;/列 ; / Map.cpp #include stdafx.h #include Map.h #include Public_Using.h Map:Map(int x, int y) :m_row(x),m_clo(y) map=new char*m_row; for(int i = 0; i m_row; i +) mapi = new charm_clo; memset(mapi, KONG, m_clo); Map:Map() for(int i = 0; i m_row; i+ ) delete mapi; delete m

13、ap; map = NULL; void Map:draw() for (int i = 0; i m_row; i+) for (int j = 0; j m_clo; j+) if(mapij = KONG) cout; elseif(mapij = HEAD_F) cout ; elseif(mapij = LEFT_F) cout; elseif(mapij = RIGHT_F) cout; elseif(mapij = BULLET) cout ; elseif(mapij = MIND) cout; elseif(mapij = SMALL) cout ; elseif(mapij

14、 = PLAYER) cout; elseif(mapij = B_U_HEAD) cout; elseif(mapij = SHENTI) cout ; elseif(mapij = B_HEAD) cout ; elseif(mapij = B_LEFT) cout; elseif(mapij = B_RIGHT) cout ; elseif(mapij = B) cout; elseif(mapij = C) cout; elseif(mapij = D) cout; elseif(mapij = E) cout; elseif(mapij = F) cout; elseif(mapij

15、 = G) cout; elseif(mapij = H) cout; elseif(mapij = I) cout; elseif(mapij = J) cout; elseif(mapij = BULLET_4_HEAK) cout; elseif(mapij = BULLET_4_SHENTI) cout; coutendl; void Map:change_map(int x, int y ,Type t) mapxy = t; 3.3 子弹类的实现 1)/ Bullet.h #pragmaonce #include Public_Using.h classMap; classBull

16、et public: Bullet(int atk=0 ,int m_hp_b=0 ,bool isOut=false ,bool isDead=false);/构造函数 Bullet( void);/析构函数 virtual void draw_on_map(Map* m)=0;/在地图上绘制 virtual void clearMe(Map* m)=0;/清空自己的轨迹 virtual void init(int x,int y)=0;/初始化自己 virtual void upDate(Map* m)=0;/位置更新 virtual void overstep(Map* m)=0;/越界

17、判定 Note* noo;/节点的指针 vector* m_bull;/存放自己包含的节点的容器(这 里是指针) bool isOut;/是否越界 bool isDead;/是否死亡 int atk;/攻击力 int m_hp_b;/血量 ; / Bullet.cpp #include stdafx.h #include Bullet.h #include Map.h Bullet:Bullet( int atk, int m_hp_b, bool isOut, bool isDead):atk(atk),isOut(isOut),isDead(isDead),m_hp_b(m_hp_b) /

18、 开空间 m_bull = new vector(); Bullet:Bullet() / 释放内存 delete m_bull; m_bull = NULL; 2)/ Bullet_1.h #pragmaonce #include Bullet.h classBullet_1:public Bullet public: Bullet_1(int x, int y,int atk = 1,int m_hp_b = 1); Bullet_1(); virtual void draw_on_map(Map* m); virtual void clearMe(Map* m); virtual voi

19、d init( int x, int y); virtual void upDate(Map* m); virtual void overstep(Map* m); ; / Bullet_1.cpp #include stdafx.h #include Bullet_1.h #include Map.h Bullet_1:Bullet_1( int x, int y,int atk, int m_hp_b):Bullet(atk, m_hp_b) init(x, y); Bullet_1:Bullet_1() / 释放内存 delete noo; noo = NULL; void Bullet

20、_1:draw_on_map(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i);/ 循环返回容器元素的位置 m-change_map(n-m_x, n-m_y, BULLET); void Bullet_1:clearMe(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); m-change_map(n-m_x, n-m_y, KONG); void Bullet_1:init( int x, int y) noo = new Note(x, y);

21、m_bull-push_back(noo); void Bullet_1:upDate(Map* m) / 清空自己的轨迹 clearMe(m); / 自己更新 for(int i = 0; i size(); i +) m_bull-at(i)-m_x-; / 判断是否越界 overstep(m); void Bullet_1:overstep(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); if(n-m_x m_y m_x m-m_row - 1 | n-m_y m-m_clo - 1) isOut = true;

22、 3)/ Bullet_2.h #pragmaonce #include Bullet.h classBullet_2:public Bullet public: Bullet_2(int x, int y,int atk = 1); Bullet_2(); virtual void draw_on_map(Map* m); virtual void clearMe(Map* m); virtual void init(int x, int y); virtual void upDate(Map* m); virtual void overstep(Map* m); ; / Bullet_2.

23、cpp #include stdafx.h #include Bullet_2.h #include Map.h #include Public_Using.h Bullet_2:Bullet_2( int x, int y,int atk):Bullet(atk) init(x, y); Bullet_2:Bullet_2() / 释放内存 delete noo; noo = NULL; void Bullet_2:draw_on_map(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i);/ 循环返回容器元素的位置 m-

24、change_map(n-m_x, n-m_y, BULLET); void Bullet_2:clearMe(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); m-change_map(n-m_x, n-m_y, KONG); void Bullet_2:init( int x, int y) / 内存泄漏 noo = new Note(x, y - 1); m_bull-push_back(noo); noo = new Note(x, y + 1); m_bull-push_back(noo); void Bull

25、et_2:upDate(Map* m) clearMe(m); for(int i = 0; i size(); i +) m_bull-at(i)-m_x-; overstep(m); void Bullet_2:overstep(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); if(n-m_x m_y m_x m-m_row - 1 | n-m_y m-m_clo - 1) isOut = true; 4)/ Bullet_3.h #pragmaonce #include Bullet.h classBullet_

26、3:public Bullet public: Bullet_3(int x, int y,int atk = 1); Bullet_3(); virtual void draw_on_map(Map* m); virtual void clearMe(Map* m); virtual void init(int x, int y); virtual void upDate(Map* m); virtual void overstep(Map* m); ; / Bullet_3.cpp #include stdafx.h #include Bullet_3.h #include Map.h #

27、include Public_Using.h Bullet_3:Bullet_3( int x, int y,int atk):Bullet(atk) init(x, y); Bullet_3:Bullet_3() / 释放内存 delete noo; noo = NULL; void Bullet_3:draw_on_map(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); m-change_map(n-m_x, n-m_y, BULLET); void Bullet_3:clearMe(Map* m) for(int

28、 i = 0; i size(); i +) Note* n = m_bull-at(i); m-change_map(n-m_x, n-m_y, KONG); void Bullet_3:init( int x, int y) noo = new Note(x - 1, y - 1); m_bull-push_back(noo); noo = new Note(x - 1, y + 1); m_bull-push_back(noo); noo = new Note(x - 1, y - 1); m_bull-push_back(noo); noo = new Note(x - 1, y +

29、1); m_bull-push_back(noo); noo = new Note(x + 1, y - 1); m_bull-push_back(noo); noo = new Note(x + 1, y + 1); m_bull-push_back(noo); void Bullet_3:upDate(Map* m) clearMe(m); for(int i = 0; i size(); i +) if(i = 0) m_bull-at(i)-m_x-; if(i = 1) m_bull-at(i)-m_x-; if(i = 2) m_bull-at(i)-m_x-; m_bull-at

30、(i)-m_y-; if(i = 3) m_bull-at(i)-m_x-; m_bull-at(i)-m_y+; if(i = 4) m_bull-at(i)-m_x-; m_bull-at(i)-m_y-; if(i = 5) m_bull-at(i)-m_x-; m_bull-at(i)-m_y+; overstep(m); void Bullet_3:overstep(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); if(n-m_x m_y m_x m-m_row - 1 | n-m_y m-m_clo - 1

31、) isOut = true; 5)/ Bullet_4.h #pragmaonce #include Bullet.h classBullet_4:public Bullet public: Bullet_4(int x, int y,int atk = 3,int m_hp_b = 5); Bullet_4(); virtual void draw_on_map(Map* m); virtual void clearMe(Map* m); virtual void init(int x, int y); virtual void upDate(Map* m); virtual void o

32、verstep(Map* m); ; / Bullet_4.cpp #include stdafx.h #include Bullet_4.h #include Map.h #include Public_Using.h Bullet_4:Bullet_4( int x, int y,int atk, int m_hp_b):Bullet(atk, m_hp_b) init(x, y); Bullet_4:Bullet_4() / 释放内存 delete noo; noo = NULL; void Bullet_4:draw_on_map(Map* m) m-change_map(m_bull

33、-at(0)-m_x, m_bull-at(0)-m_y, BULLET_4_SHENTI); m-change_map(m_bull-at(1)-m_x, m_bull-at(1)-m_y, BULLET_4_HEAK); m-change_map(m_bull-at(2)-m_x, m_bull-at(2)-m_y, B_LEFT); m-change_map(m_bull-at(3)-m_x, m_bull-at(3)-m_y, B_RIGHT); void Bullet_4:clearMe(Map* m) for(int i = 0; i size(); i +) Note* n =

34、m_bull-at(i); m-change_map(n-m_x, n-m_y, KONG); void Bullet_4:init( int x, int y) noo = new Note(x + 1, y); m_bull-push_back(noo); noo = new Note(x + 2, y); m_bull-push_back(noo); noo = new Note(x + 1, y - 1); m_bull-push_back(noo); noo = new Note(x + 1, y + 1); m_bull-push_back(noo); void Bullet_4:

35、upDate(Map* m) clearMe(m); for(int i = 0; i size(); i +) m_bull-at(i)-m_x+; overstep(m); void Bullet_4:overstep(Map* m) for(int i = 0; i size(); i +) Note* n = m_bull-at(i); if(n-m_x m_y m_x m-m_row - 1 | n-m_y m-m_clo - 1) isOut = true; 3.4 飞机类及玩家类的实现 1)/ Plane.h #pragmaonce classMap; #include Publ

36、ic_Using.h classPlane public: Plane(int m_h = 0, int _atk = 0,bool isD = false, bool isO = false); Plane(); virtual void init() = 0;/ 纯虚函数 virtual void draw_on_map(Map* m) = 0; virtual void clear(Map* m) = 0; virtual void up_date(Map* m) = 0; virtual void is_game_over();/ 判断玩家是否死亡 char direction;/ 飞

37、机的方向 Note* noo;/ 节点指针 vector* m_data;/ 存储飞机包含的节点的容器(指针) int m_hp;/ 血量 int atk;/ 攻击力 bool isDead;/ 死亡判定 bool isOut;/ 越界判定 ; / Plane.cpp #include stdafx.h #include Plane.h #include Public_Using.h #include Map.h Plane:Plane(int m_h,int _atk,bool isD,bool isOut) :m_hp(m_h),isDead(isD),atk(_atk),isOut(is

38、Out) m_data=new vector(); Plane:Plane(void) delete m_data; m_data=NULL; void Plane:is_game_over() if (m_hp=0) m_hp=0; isDead=true; 2)/ Player.h #pragmaonce #include Plane.h classBullet; classBullet_1; classMap; classPlayer:public Plane public: Player(int hp = 5,int count = 0,int bullettype = 1,int a

39、tk = 1); Player(); virtual void init(); virtual void draw_on_map(Map* m); virtual void up_date(Map* m); virtual void clear(Map* m); void init_bullet_1();/ 子弹 1 void init_bullet_2();/ 子弹 2 void init_bullet_3();/ 子弹 3(有问题) bool overstep(int x, int y, Map* m); void up(Map* m);/ 上 void dowm(Map* m);/ 下

40、void left(Map* m);/ 左 void right(Map* m);/ 右 int count;/ 玩家的分数 int bullettype;/ 子弹类型 vector* m_bullet;/ 存放玩家子弹的容器(指针) ; / Player.cpp #include stdafx.h #include Player.h #include Bullet.h #include Bullet_1.h #include Bullet_2.h #include Bullet_3.h #include Map.h Player:Player(int hp, int count, int b

41、ullettype, int atk) :Plane(hp, atk), count(count), bullettype(bullettype) init(); m_bullet = new vector(); Player:Player() delete m_bullet; m_bullet = NULL; deletenoo; noo = NULL; void Player:init() noo = new Note(24, 15); m_data-push_back(noo); noo = new Note(24, 14); m_data-push_back(noo); noo = n

42、ew Note(24, 16); m_data-push_back(noo); noo = new Note(23, 15); m_data-push_back(noo); void Player:draw_on_map(Map* m) / 绘制自己 m-mapm_data-at(0)-m_xm_data-at(0)-m_y = PLAYER; m-mapm_data-at(1)-m_xm_data-at(1)-m_y = LEFT_F; m-mapm_data-at(2)-m_xm_data-at(2)-m_y = RIGHT_F; m-mapm_data-at(3)-m_xm_data-a

43、t(3)-m_y = HEAD_F; / 玩家子弹的绘制 for(int i = 0; i size(); i +) if(!m_bullet-at(i)-isOut) for(int i = 0; i size(); i +) m_bullet-at(i)-draw_on_map(m); void Player:up_date(Map* m) clear(m); if(_kbhit()/ 检测按键 direction = _getch(); else direction = -100; switch(direction) casew: up(m); break; casea: left(m)

44、; break; cases: dowm(m); break; cased: right(m); break; case : if (bullettype = 1) init_bullet_1(); elseif(bullettype = 2) init_bullet_2(); elseif(bullettype = 3) init_bullet_3(); break; casep: system(pause); break; case3: bullettype = 1;/ 切换子弹 break; case4: bullettype = 2; break; case5: bullettype

45、= 3; break; case2: this-isDead =true; break; default: break; for(int i = 0; i size(); i +) m_bullet-at(i)-upDate(m); for(int i = 0; i size();) if(m_bullet-at(i)-isOut = true | m_bullet-at(i)-isDead = true) / 用一个新的指针指向越界或者已经打到敌人身上需要删除的子弹 Bullet* b = m_bullet-at(i); vector:iterator vit = m_bullet-begi

46、n();/ 定义一个迭代器 m_bullet-erase(vit + i); / 释放已经被删除的子弹 deleteb; else i+; void Player:clear(Map* m) / 清除飞机 for(int i = 0; i size(); i +) Note* n = m_data-at(i); m-change_map(n-m_x, n-m_y, KONG); / 清除子弹 for(int i = 0; i size(); i +) Bullet* b = m_bullet-at(i); for(int j = 0; j m_bull-size(); j +) int x =

47、 b-m_bull-at(j)-m_x; int y = b-m_bull-at(j)-m_y; m-change_map(x, y, KONG); void Player:init_bullet_1() int x = m_data-at(3)-m_x; int y = m_data-at(3)-m_y; Bullet* b = new Bullet_1(x - 1, y); m_bullet-push_back(b); void Player:init_bullet_2() int x = m_data-at(3)-m_x; int y = m_data-at(3)-m_y; Bullet

48、* b = new Bullet_2(x - 1, y); m_bullet-push_back(b); void Player:init_bullet_3() int x = m_data-at(3)-m_x; int y = m_data-at(3)-m_y; Bullet* b = new Bullet_3(x - 1, y); m_bullet-push_back(b); bool Player:overstep(int offx, int offy, Map* m) int x, y; for(int i = 0; i size(); i +) x = m_data-at(i)-m_

49、x + offx; y = m_data-at(i)-m_y + offy; if(x 0 | y m-m_row - 1 | y m-m_clo - 1) return true; return false; void Player:up(Map* m) if(overstep(-1, 0, m) return ; for(int i = 0; i size(); i +) m_data-at(i)-m_x-; void Player:dowm(Map* m) if(overstep(1, 0, m) return ; for(int i = 0; i size(); i +) m_data

50、-at(i)-m_x+; void Player:left(Map* m) if(overstep(0, -1, m) return ; for(int i = 0; i size(); i +) m_data-at(i)-m_y-; void Player:right(Map* m) if(overstep(0, 1, m) return ; for(int i = 0; i size(); i +) m_data-at(i)-m_y+; 3)/ Enemy.h #pragmaonce #include Plane.h #include Public_Using.h classBullet;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 办公、行业 > 待归类文档
版权提示 | 免责声明

1,本文(C++课程设计报告(飞机大战).docx)为本站会员(四川天地人教育)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|