第六周项目二 游戏中角色类的设计(类的组合)

本文详细介绍了游戏开发中角色类与武器类的设计与实现,包括构造函数、属性访问和基本战斗操作。通过实例展示了类的组合和成员变量的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="cpp">/*  
 *Copyright(c) 2016,烟台大学计算机学院  
 *All rights reserved.  
 *作    者:刘金石  
 *完成日期:2016年4月10日  
 *版本  号:v1.0  
 *问题描述:设计游戏中的角色类,并把武器作为一个单独的类  
 *输入描述:角色。  
 *输出描述:输出模拟战斗过程。  
*/    
#include<iostream>
using namespace std;
class Weapon
{
    string name;//武器名字
    int force;//武器威力
public:
    Weapon(string mname,int mforce):name(mname),force(mforce){}//构造函数
    int getcomForce();//获取普通攻击数值
    int getbigForce();//获取大招攻击数值
};
int  Weapon::getcomForce()
{
    return force;
}
int  Weapon::getbigForce()
{
    return (force*2);
}
class Role
{
public:
    Role(string jsname,int xblood,string wqname,int wqf);//角色类的构造函数
    ~Role();//析构函数
    void show();
    bool isAlived();
    void comattack(Role &r);//普通攻击
    void finalhit(Role &r);//大招攻击
    void eat(int bloo);//吃东西,获取血量
private:
    string name;//角色名字
    int blood;
    bool life;//角色状态
    Weapon weapon;
};
bool Role::isAlived() //是否活着
{
    return life;
}
Role::Role(string jsname,int xblood,string wqname,int wqf):name(jsname),blood(xblood),weapon(wqname,wqf)
{
    if(blood>0)
        life=true;
    else
        life=false;
}
Role::~Role()
{
    cout<<name<<"退出江湖..."<<endl;
}
void Role::comattack(Role &r)
{
    cout<<r.name<<" "<<"was"<<" "<<"be"<<" "<<"attack"<<endl;
    if(isAlived())
    {
        //blood+=weapon.getcomForce();
        r.blood-=weapon.getcomForce();
        if(r.blood<=0)
            r.life=false;
    }
}
void Role::finalhit(Role &r)
{
    cout<<r.name<<" "<<"was"<<" "<<"be"<<" "<<"attack"<<endl;
    if(isAlived())
    {
        //blood+=weapon.getbigForce();
        r.blood-=weapon.getbigForce();
        if(r.blood<=0)
            r.life=false;
    }
}
    void Role::show() //显示
    {
    cout<<name<<" has "<<blood<<" blood, it is ";
    if(isAlived())
        cout<<"alived.";
    else
        cout<<"dead.";
    cout<<endl;
    }
void Role::eat(int blo)
{
    blood=blood+blo;
    cout<<name<<" "<<"eat"<<" "<<"something,"<<"blood"<<" "<<"is"<<" "<<blood<<endl;
}
int main()
{
    cout<<"---begin---"<<endl;
    Role mary("Mary",4,"yitian",2);
    Role jack("Jack",6,"tulong",3);
    mary.show();
    jack.show();
    cout<<"---1st round---"<<endl;
    mary.comattack(jack);
    mary.eat(2);
    mary.show();
    jack.show();
    cout<<"---2nd round---"<<endl;
    jack.finalhit(mary);
    mary.show();
    jack.show();
    cout<<"---end---"<<endl;
    return 0;
}
运行结果:

<img src="https://img-blog.youkuaiyun.com/20160410184718169?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
学习心的:
      通过这次练习,对类的组合有了更深的认识,尤其是组合类的构造函数如何去写。即先写最大的那个类的构造函数,然后写大类中的小类。然后就是在大的那个类中如何使用小类中的数据成员,即在小类中写一个函数得到那个数据,然后用小类的对象调用那个函数。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值