第八周项目四(1)-游戏中角色类的增强版
来源:互联网 发布:淘宝怎么用微信支付吗 编辑:程序博客网 时间:2024/04/29 11:54
问题及代码:
game.h
#ifndef GAME_H_INCLUDED#define GAME_H_INCLUDED#include <string>using namespace std;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(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); //构造函数 ~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 show(); //显示private: string name; //角色名称 int blood; //当前血量 bool life; //是否活着 Point location; //位置 Weapon weapon; //武器};#endif // GAME_H_INCLUDED
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));}
role.cpp
#include <iostream>#include "game.h"using namespace std;Role::Role(string nam, int b, Point l, Weapon w):name(nam),blood(b),location(l),weapon(w){ if(blood>0) life=true; else life=false;}Role::~Role(){ cout<<name<<"退出江湖..."<<endl;}//吃东西,涨d血(死了后吃上东西可以复活)void Role::eat(int d) //吃东西,涨d血(死了也能吃,别人喂的,以使能复活){ blood+=d; if(blood>0) life=true;}//攻击别人,自己涨血,同时对方被攻击失血,血量取决于当前用的武器//在武器的攻击范围内才可以攻击void Role::attack(Role &r) //攻击别人,涨1血{ if(isAlived()&&weapon.getKillRange()>this->distance(r)) //活着且在杀伤范围内 { blood+=weapon.getForce(); r.beAttack(weapon.getForce()); }}//被别人攻击,参数f是承受的攻击力void Role::beAttack(int f){ blood-=f; if(blood<=0) life=false;}//返回与另一角色的距离double Role::distance(Role &r){ return location.distance(r.location);}//是否活着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 "<<weapon.getWname(); cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and "; if(isAlived()) cout<<"alived."; else cout<<"dead."; cout<<endl;}
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;}
main.cpp
/**烟台大学计算机学院*文件名称:xiangmu2(1).cpp*作 者:闫安*完成日期:2016年5月5日*版 本 号:codeblocks 16.01**问题描述:改进:(1)武器增加了杀伤距离,角色增加了位置,这样,当两个角色对象离得太远,距离超出 攻击者武器的杀伤范围,攻击行为无效;(2)其他一些规则更加合理*输入描述:无*程序输出:任务的各种状态*/#include <iostream>#include "game.h"using namespace std;int main( ){ Weapon w1("Gold stick",200, 100), w2("Fire point gun",180,300); Role wuKong("WuKong", 500, Point(0, 0), w1); Role neZha("NeZha", 210, Point(30,30), w2); cout<<"---begin---"<<endl; wuKong.show(); neZha.show(); cout<<"---1st round---"<<endl; wuKong.attack(neZha); wuKong.show(); neZha.show(); cout<<"---2nd round---"<<endl; 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;}
运行结果:
1.Role里的数据成员有Point类的对象和Weapon类的对象
2.若某类的“数据成员为别的类的对象”,其构造函数定义时构造函数的形参列表中必须包含别的类的构造函数
3.攻击条件为:任务必须活着并且可攻击距离大于当前两个人物的距离。攻击时,攻击者血量增加量为攻击力,别攻击者血量减少量为攻击力
0 0
- 第八周项目四(1)-游戏中角色类的增强版
- 第八周项目四(2)-游戏中角色类的增强版
- 第八周项目四(3)-游戏中角色类的增强版
- 第八周项目四 游戏中的角色类增强版
- 第八周上机实践项目4(1):游戏中角色类增强版
- 第八周项目4游戏中的角色类增强版
- 第八周上机实践项目4(2):游戏中的角色类增强版
- 第八周项目(2)-游戏中的角色类增强版
- 第八周上机实践项目——项目4-游戏中的角色类增强版-问题(2)
- 第八周上机实践项目——项目4-游戏中的角色类增强版-问题(3)
- 第八周项目四—游戏中的角色类
- 第八周项目四—游戏中的角色类
- 第8周项目4——游戏中的角色类增强版
- 第8周项目4:游戏中的角色类增强版(2)
- 第8周【项目4-游戏中的角色类增强版】
- 第五周项目2 游戏中角色类的设计(1)
- 补充程序之游戏系列— 4 游戏中的角色类增强版 (1)
- 游戏中的角色类增强版
- javaBean
- [C++]using语义使用说明
- 【连载】关系型数据库是如何工作的?(19) - 查询管理器之贪婪算法
- GridView+下拉刷新+上拉加载
- [随笔]示波器"触发",爱它无法自拔
- 第八周项目四(1)-游戏中角色类的增强版
- bzoj 1119: [POI2009]SLO(置换)
- Android正则
- 适配器模式
- HDU 5249 KPI STL
- BSG白山极客挑战赛——数数字(递推)
- leetcode #16 in cpp
- 洛谷 P1352 TYVJ P1052 CODEVS 1380 没有上司的舞会
- 斗地主