我正在开发一个基于文本的游戏以获得乐趣,并且我正在努力从基类继承。我有我的基类Being
,它包含我创建的任何角色的所有统计数据。然后我有一个班级Combat
,我想从Being
(或从一个存在并将其传递到战斗中?)的所有统计数据并做所有的战斗工作。但我不太了解继承,或者至少如何声明函数Main
以使其工作。如果我将攻击函数保留在 中Being
,我可以在 main 中写下这一行:
human.attack(monster);
但是在将战斗分成另一个类之后,我不确定如何在 main 中编写它以便它工作。请并感谢您的帮助!
class Being // Base Class
{
public:
string name, nameSpecialAttack; // Implement a special attack feature,
// that has a specific mutliplier #
// unique to each being
int attackBonus, attackMod; // was a float attackMod method for casting
// spells that double or halve a Being’s
// attack power.
int baseDamage; // base damage
int health, healthMax; // current health and max health
int mp, mpMax; // current mp and max mp
int arrows; // change to [ranged?] ammo
Being(); // Default Constructor
Being(string name, string nameSpecialAttack, int attackBonus,
int attackMod, int baseDamage, int health, int healthMax,
int mp, int mpMax, int arrows); // Constructor 2
// All my set and get functions
}
然后我的派生类:
class Combat : public Being
{
private:
public:
void attack(Being& target);
};
战斗.cpp:
void Combat::attack(Being& target)
{
//unsigned seed = time(0);
//srand(seed);
// Rand # 0-99, * damage+1, /100, * attackMod, parse to int.
int damage = (int)(attackMod*( ( (rand()%100)*(baseDamage+1) /100) + attackBonus + (rand()%2)));
target.health -=damage;
cout << name << " attacks " << target.name << " doing " << damage << " damage!" << endl;
cout << target.name << "'s health: " << target.health << endl;
// Use getHealth() instead and put this function there
if(target.health <= 0)
{
cout << endl << name << " killed " << target.name << "! You have won the game! " << endl << endl;
cout << "Terminating AMnew World, Good Bye.\n" << endl << endl;
exit(0);
}
}
主要的:
Being human("", "FirstofFurry", 2, 1, 2, 50, 50, 20, 30, 7); // A thing of
// class Being
Being monster("Armored Goblin", "Rawr", 2, 1, 2, 65, 59, 20, 20, 6);
int main()
{
human.attack(monster); // No longer works since attack is in
// combat class now
Sleep(3000);
cout << endl << endl;
monster.attack(human);
}