增加游戏角色的功能
来源:互联网 发布:如何申请成为淘宝达人 编辑:程序博客网 时间:2024/05/16 07:01
/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:胡庆龙 *完成日期:2016年4月26日 *版 本 号:v1.0 * *问题描述:在之前的游戏类的基础上添加人物坐标,武器杀伤范围,武器库。 */<pre name="code" class="cpp">//game.h#ifndef GAME_H_INCLUDED#define GAME_H_INCLUDED#include <vector>#include <string>using namespace std;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() = default; 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, vector<Weapon> w); //构造函数 ~Role(); //析构函数 void eat(int d); //吃东西,涨d血(死了后吃上东西可以复活) void attack(Role &r); //攻击别人,自己涨血,同时对方被攻击失血。血量取决于当前用的武器 void beAttack(Role &r); //被别人攻击,r是攻击者 double distance(Role &r); //返回与另一角色的距离 bool isAlived(); //是否活着 void moveTo(int x, int y); //移到另外一点 void move(int dx, int dy); //从当前位置移动 void changeWeapon(int wno); //换手中的武器 void vAddWeapon(); void vDelWeapon(); void show(); //显示 void vShowWeapons();//显示武器库private: string name; //角色名称 int blood; //当前血量 bool life; //是否活着 Point location; //位置 vector<Weapon> weapons; //武器库 int holdWeapon; //现在手持哪一件武器(空手为NOWEAPON,初始时空手)};#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));}//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;}//role.cpp#include <iostream>#include "game.h"using namespace std;Role::Role(string nam, int b, Point l, vector<Weapon> w) :name(nam),blood(b),location(l),weapons(w),holdWeapon(NOWEAPON){ 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){ if(isAlived()&&holdWeapon>NOWEAPON&&weapons[holdWeapon].getKillRange()>this->distance(r)) //活着且在杀伤范围内 { blood += weapons[holdWeapon].getForce(); r.blood -= weapons[holdWeapon].getForce(); }}//被别人攻击,r是攻击者void Role::beAttack(Role &r){ if(r.isAlived() && r.holdWeapon>NOWEAPON && r.weapons[holdWeapon].getKillRange()>this->distance(r)) { blood -= r.weapons[holdWeapon].getForce(); r.blood += r.weapons[holdWeapon].getForce(); if(blood<=0) life=false; }}//返回与另一角色的距离double Role::distance(Role &r){ return location.distance(r.location);}//换手中的武器void Role::changeWeapon(int wno){ if(wno<(int)weapons.size()) 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::vAddWeapon(){ string name; int force; double range; cout << "输入要添加武器的名字:"; cin >> name; cout << "威力:"; cin >> force; cout << "杀伤范围:"; cin >> range; weapons.push_back(Weapon(name,force,range));}//删除武器void Role::vDelWeapon(){ string wnam; cout <<"输入要删除武器的名称:"; cin.clear(); cin.sync(); getline(cin,wnam); for(auto beg = weapons.begin(); beg != weapons.end(); ++beg) { if(beg->getWname() == wnam) { weapons.erase(beg); return ; } } cout << "找不到武器" << endl;}//显示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;}//显示武器库信息void Role::vShowWeapons(){ cout << name << "'s arsenal:" << endl; for(auto &temp : weapons) cout << "name:" << temp.getWname() << " force:" << temp.getForce() <<" kill range:" << temp.getKillRange() << endl;}//测试函数#include <iostream>#include "game.h"using namespace std;int main(){ vector<Weapon> w1 = {Weapon("Gold stick",200, 100)}; vector<Weapon> w2 = {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); Role neZha("NeZha", 210, Point(30,30), w2); wuKong.changeWeapon(0); neZha.changeWeapon(0); cout<<"---begin---"<<endl; wuKong.show(); neZha.show(); cout<<"---1st round---"<<endl; neZha.beAttack(wuKong); 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; wuKong.vAddWeapon(); neZha.vDelWeapon(); neZha.vShowWeapons(); wuKong.vShowWeapons(); return 0;}
0 0
- 增加游戏角色的功能
- 人生角色的增加
- 飞机游戏增加BOSS功能
- 游戏角色创建的看法
- 游戏制作人的角色属性
- 带武器的游戏角色
- 带武器的游戏角色
- 带武器的游戏角色
- RPG角色游戏的创建
- 增加类的功能
- 游戏角色
- 游戏角色
- 游戏角色
- 游戏角色
- 游戏角色
- sudoku游戏1.1版(增加了生成初始棋局的功能)
- 使用视图取出用户,功能,角色,用户角色,角色功能5张表的关系
- 游戏历史上十大最成功的人工智能角色
- 【codevs2421】【BZOJ1858】序列操作,线段树
- ios7设计思想(界面坐标上移的原因)
- 简单的
- 实践2
- 第九周项目1——深复制体验
- 增加游戏角色的功能
- 第九周项目3-人数不定的工资类
- 项目3——指向学生类的指针
- 解决Volley接收数据、中文显示乱码问题。
- 项目24-深复制的体验
- 第十周第十一周-阅读程序
- 第九周项目一 深复制体验
- Hibernate OneToOne注解
- 第九周项目1阅读程序(1)