第八周上机实践项目4(2):游戏中的角色类增强版
来源:互联网 发布:考普通话的软件 编辑:程序博客网 时间:2024/05/18 00:28
<pre class="cpp" name="code">/*All rights reserved. *文件名称:ftg.cpp *作 者:赵子琳 *完成日期:2016年4月28日 *版 本 号:v1.0 * *问题描述:游戏中的角色类增强版 */
1.game.h:类声明#ifndef GAME_H_INCLUDED#define GAME_H_INCLUDED#include <string>using namespace std;const int N=10; //每个角色最多拥有的武器const int NOWEAPON=-1; //表示手中无武器class Point //Point类声明{public: //外部接口 Point(int x=0, int y=0); int getX(); int getY(); double distance(const Point &p); //返回与另外一点p之间的距离 void moveTo(int x, int y); //移到另外一点 void move(int dx, int dy); //从当前位置移动private: int x, y; //座标};class Weapon{public: Weapon(){}; Weapon(string wnam, int f, double k); Weapon(const Weapon&); string getWname(); int getForce(); //返回杀伤力 double getKillRange(); //返回杀伤距离private: string wname; //名称 int force; //杀伤力 double killRange; //杀伤距离};class Role{public: Role(string nam, int b, Point l, Weapon w[], int n); //构造函数 ~Role(); //析构函数 void eat(int d); //吃东西,涨d血(死了后吃上东西可以复活) void attack(Role &r); //攻击别人,自己涨血,同时对方被攻击失血。血量取决于当前用的武器 void beAttack(int f); //被别人攻击,参数f是承受的攻击力 double distance(Role &r); //返回与另一角色的距离 bool isAlived(); //是否活着 void moveTo(int x, int y); //移到另外一点 void move(int dx, int dy); //从当前位置移动 void changeWeapon(int wno); //换手中的武器 void show(); //显示private: string name; //角色名称 int blood; //当前血量 bool life; //是否活着 Point location; //位置 Weapon weapons[N]; //武器 int weaponNum; //武器数目 int holdWeapon; //现在手持哪一件武器(空手为NOWEAPON,初始时空手)};#endif // GAME_H_INCLUDED
2.point.cpp,定义点类,表示位置#include "game.h"#include <cmath>Point::Point(int x, int y): x(x), y(y) { }int Point::getX(){ return x;}int Point::getY(){ return y;}//移到另外一点void Point::moveTo(int x, int y){ this->x=x; this->y=y;}//从当前位置移动void Point::move(int dx, int dy){ this->x+=dx; this->y+=dy;}double Point::distance(const Point& p){ double dx = this->x - p.x; double dy = this->y - p.y; return (sqrt(dx * dx + dy * dy));}
3.weapon.cpp,定义武器类#include "game.h"Weapon::Weapon(string wnam, int f, double k):wname(wnam),force(f),killRange(k) {}Weapon::Weapon(const Weapon &w):wname(w.wname),force(w.force),killRange(w.killRange) {}string Weapon::getWname(){ return wname;}//返回杀伤力int Weapon::getForce(){ return force;}//返回杀伤距离double Weapon::getKillRange(){ return killRange;}
4.role.cpp,定义角色类,表示参与游戏的角色#include <iostream>#include "game.h"using namespace std;Role::Role(string nam, int b, Point l, Weapon w[], int n) :name(nam),blood(b),location(l),weaponNum(n),holdWeapon(NOWEAPON){ if(blood>0) life=true; else life=false; for(int i=0; i<n; i++) weapons[i]=w[i];}Role::~Role(){ cout<<name<<"退出江湖..."<<endl;}//吃东西,涨d血(死了后吃上东西可以复活)void Role::eat(int d) //吃东西,涨d血(死了也能吃,别人喂的,以使能复活){ blood+=d; if(blood>0) life=true;}//攻击别人,自己涨血,同时对方被攻击失血,血量取决于当前用的武器//在武器的攻击范围内才可以攻击void Role::attack(Role &r){ if(isAlived()&&holdWeapon>NOWEAPON&&weapons[holdWeapon].getKillRange()>this->distance(r)) //活着且在杀伤范围内 { blood+=weapons[holdWeapon].getForce(); r.beAttack(weapons[holdWeapon].getForce()); }}//被别人攻击,参数f是承受的攻击力void Role::beAttack(int f){ blood-=f; if(blood<=0) life=false;}//返回与另一角色的距离double Role::distance(Role &r){ return location.distance(r.location);}//换手中的武器void Role::changeWeapon(int wno){ if(wno<weaponNum) holdWeapon=wno;}//是否活着bool Role::isAlived(){ return life;}//移到另外一点void Role::moveTo(int x, int y){ if(isAlived()) //死了就不能动了 location.moveTo(x,y);}//从当前位置移动void Role::move(int dx, int dy){ if(isAlived()) location.move(dx,dy);}//显示void Role::show(){ cout<<name<<" has "<<blood<<" blood, hold "; if(holdWeapon==NOWEAPON) cout<<"no weapon"; else cout<<weapons[holdWeapon].getWname(); cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and "; if(isAlived()) cout<<"alived."; else cout<<"dead."; cout<<endl;}
5.main.cpp,测试函数,表示位置#include <iostream>#include "game.h"using namespace std;int main( ){ Weapon w1[1]= {Weapon("Gold stick",200, 100)}; //金箍棒 Weapon w2[3]= {Weapon("Fire-Tip Lance",180,300), //火尖枪 Weapon("Universal Ring",100,500), //乾坤圈 Weapon("Sky Muddling Damask",50,1000) //混天绫 }; Role wuKong("WuKong", 500, Point(0, 0), w1, 1); Role neZha("NeZha", 210, Point(30,30), w2, 3); wuKong.changeWeapon(0); neZha.changeWeapon(0); cout<<"---begin---"<<endl; wuKong.show(); neZha.show(); cout<<"---1st round---"<<endl; wuKong.attack(neZha); wuKong.show(); neZha.show(); cout<<"---2nd round---"<<endl; neZha.changeWeapon(2); neZha.attack(wuKong); wuKong.show(); neZha.show(); cout<<"---3rd round---"<<endl; neZha.moveTo(100,100); wuKong.attack(neZha); wuKong.show(); neZha.show(); cout<<"---4th round---"<<endl; neZha.attack(wuKong); wuKong.show(); neZha.show(); cout<<"---then---"<<endl; neZha.attack(wuKong); neZha.attack(wuKong); wuKong.attack(neZha); wuKong.show(); neZha.show(); cout<<"---end---"<<endl; return 0;}
0 0
- 第八周上机实践项目4(2):游戏中的角色类增强版
- 第八周上机实践项目——项目4-游戏中的角色类增强版-问题(2)
- 第八周上机实践项目——项目4-游戏中的角色类增强版-问题(3)
- 第八周上机实践项目4(1):游戏中角色类增强版
- 第八周项目4游戏中的角色类增强版
- 第八周项目四 游戏中的角色类增强版
- 第八周项目(2)-游戏中的角色类增强版
- 第五周上机实践项目——项目2-游戏中的角色类-(2)
- 第五周上机实践项目——项目2-游戏中的角色类(2)
- 第五周上机实践项目 项目2--游戏中的角色类(2)
- 第五周上机实践项目——项目2-游戏中的角色类-(1)
- 第五周上机实践项目——项目2-游戏中的角色类(1)
- 第五周上机实践项目 项目2--游戏中的角色类(1)
- 第八周项目四(2)-游戏中角色类的增强版
- 第五周上机实践项目2(2):游戏中的角色类
- 第五周上机实践项目2(1):游戏中的角色类
- 第四次上机实践项目-项目2-游戏中的角色类-(1)
- 第八周项目四(1)-游戏中角色类的增强版
- 【代码笔记】iOS-判断是否是模拟机
- 设置content type
- XML 入门学习笔记(一)
- 在linux下安装禅道
- 110.Balanced Binary Tree从8ms到2ms到1ms
- 第八周上机实践项目4(2):游戏中的角色类增强版
- Android Studio 使用之一(Eclipse 快捷键!快捷键大法好)
- 链串的基本操作
- mysql的下载到安装使用
- <html>a
- js实现99乘法表
- 指向学生类的指针
- Web测试要点(功能、性能、可用性、兼容、安全)
- 第九周-深复制体验