敌人攻击玩家的方法

时间:2012-10-07 17:14:17

标签: c++ text-based

嘿,我的文本冒险游戏。我有这门课:

class wolves
{
    public:
    string name;
    int health;
private:
    string attack;
    //wolves(string name,int health); // Constructor
    //hitPoints() +1;
};

我有一种randomaly计算生命值的方法。我不知道如何使用类创建函数。或者如何在班级中使用命中点功能。

有人可以帮忙吗?

hitpoints()

int hitPoints()
{
    srand(time(0)); // seed random number generator based on current time
int randomNumber= rand(); // generate random number
int hitPoints = (randomNumber% 20) + 1; // get a number between 1 and 20
return hitPoints;
}
int fatigue()
{
    srand(time(0)); // seed random number generator based on current time
int randomNumber= rand(); // generate random number
int fatigue = (randomNumber% 5) + 1; // get a number between 1 and 5
return fatigue;
}
int Encounter()
{


    srand(time(0)); // seed random number generator based on current time
    int randomNumber= rand(); // generate random number
    int encounter = (randomNumber% 3); // get a number between 1 and 5
    cin >> encounter;
        switch(encounter)
        {
        case 1:
            Wolves();
            menu = false;
            break;
        case 2:
            Soldier();
            break;
        case 3:
            CaveBear();
            break;
        }
        cout << encounter;

    return encounter;
}

1 个答案:

答案 0 :(得分:2)

要定义类成员函数,请使用范围运算符::来使用类名限定名称:

int wolves::hitpoints()
{
    return health;
}

这也适用于结构和命名空间内的函数。

当然,函数需要首先在类体内声明:

class wolves
{
public:
    string name;
    //wolves(string name,int health); // Constructor
    int hitPoints();
private:
    int health;
    string attack;
};

此外,对于非常短的函数,在类定义中内联定义它们通常更有意义:

class wolves
{
public:
    string name;
    //wolves(string name,int health); // Constructor
    int hitPoints() { return health; }
private:
    int health;
    string attack;
};