带武器的格斗游戏,武器带回血

来源:互联网 发布:暴走大事件 豫章 知乎 编辑:程序博客网 时间:2024/04/24 06:37


#include <iostream>
using namespace std;
class Weapon
{
public:
    Weapon(string wnam, int f);
    int getForce();
private:
    string wname;   //名称
    int force;       //威力
};
Weapon::Weapon(string wnam, int f):wname(wnam),force(f){}
int Weapon::getForce()
{
    return force;
}
class Role
{
public:
    Role(string nam, int b, string wnam, int f); //构造函数
    ~Role(); //析构函数
    void eat(int d); //吃东西,涨d血
    void attack(Role &r); //攻击别人,自己涨一半血,别人同时失血
    bool isAlived(); //是否活着
    void show(); //显示
private:
    string name;
    int blood;//血量
    Weapon weapon;
    bool life;
};

 Role::Role(string nam, int b, string wnam, int f):name(nam),blood(b),weapon(wnam,f)
 {
     if(blood<=0)
        life=false;
        else
        life=true;
 }

Role::~Role()
{
    cout<<name<<"玩家退出"<<endl;
}
 void Role::eat(int d)
 {
     blood+=d;
 }
 void Role::attack(Role &r)
 {
     r.blood-=weapon.getForce();
     blood+=weapon.getForce()/2;
     if(r.blood<=0)
     {
         r.~Role();
         cout<<"游戏结束"<<endl;
     }

 }
 bool  Role::isAlived()
 {
     if(blood<=0)
     {
         life=false;
         cout<<"玩家"<<name<<"死亡";
     }
        else
        {
        life=true;
        cout<<"玩家"<<name<<"还活着";
        }
 }
 void Role::show()
 {
     cout<<"玩家:"<<name<<"  血量:"<<blood<<"  攻击力:"<<weapon.getForce()<<endl;
 }

int main( )
{
    Role mary("Mary", 500, "TuLong",200);
    Role jack("Jack", 100, "YiTian", 180);
    cout<<"---begin---"<<endl;
    mary.show();
    jack.show();
    cout<<"---1st round---"<<endl;
    jack.attack(mary);
    mary.show();
    jack.show();
    cout<<"---2nd round---"<<endl;
    mary.attack(jack);
    mary.show();
    jack.show();
    cout<<"---end---"<<endl;
    return 0;
}

0 0