我希望能够制作一个战斗场景并将其放入一个函数中,但每次我尝试时我都必须在不同的头文件中创建对象,然后我无法在 main.js 中调用它。为了解决这个问题,我在主函数中创建了循环,但它说调用该方法的对象必须是可修改的。
//main.cpp
int main()
{
Characters h;//Created using normal constructor
cout << "Character: \n";
h.setAttack(5);
h.getAttack();
h.setDefense(15);
h.getDefense();
Hero Me;
cout << "Hero: \n";
Me.setAttack(10);
Me.getAttack();
Me.setHp(5);
Me.getHp();
Hero::Hero(1,2,3,4);//Created using overloaded constructor
Monsters m;
cout << "Monster: \n";
m.setAttack(20);
m.getAttack();
Monsters::Monsters(5,6,7,8);
//Problem is this this loop! i cant access the member functions for my objects.
//And i want to be able to put this in a function and call it from another file!
do
{
cout << "Attacking!\n";
cout << "Your hp is: " << Me.getHp() << endl;
cout << "The enemy's hp is: "<< m.getHp << endl;
cout << "\nThe monster has attacked you!\n";
cout << "You received " << m.getStrength() << " damage;" << endl;
Me.setHp() -= m.getStrength() ;//It compiles an error, saying its not modifiable
cout << "\nYour hp is now: " << Me.getHp() << endl;
cout << "Enemy hp is: "<< m.getHp << endl;
cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
m.setHp() -= Me.getAttack();
cout << "Enemy hp is now: " << m.getHp() - Me.getAttack() << endl;
}while ((Me.getHp() >= 0) && (m.getHp() >= 0));
if ((Me.getHp > 0) && (m.getHp < 0))
cout <<"Congratulations! You killed the enemy!" << endl;
else if ((Me.getHp < 0) && (m.getHp > 0))
cout << "You have died!" << endl;
cin.sync();
cin.get();
return 0;
}
//Here's the rest of my code.
//Hero.h
class Hero:
public Characters
{
public:
Hero();
Hero(int, int, int, int);
~Hero(void);
};
//Hero.cpp
int Herolevel;
int HeroHp;
int HeroStrength;
int HeroAttack;
int HeroDefense;
Hero::Hero()
{
cout << "HOLA! Hero Created using normal constructor\n";
}
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
{
cout << "Hero created using Overloaded function!\n";
HeroHp = newHp;
cout << "Hp is: "<< HeroHp << endl;
Herolevel = newLevel;
cout << "level is: " << Herolevel << endl;
HeroAttack = newAttack;
cout << "Attack is: " << HeroAttack << endl;
HeroDefense = newDef;
cout << "Defense is: " << HeroDefense << endl;
}
Hero::~Hero(void)
{
cout << "Hero destroyed!\n";
}
//Monsters.h
#pragma once
#include "Hero.h"
#include "Characters.h"
class Monsters:
public Characters //Hero
{
public:
Monsters(void);
Monsters(int, int, int, int);
//Monsters(int);
~Monsters(void);
};
//Monsters.cpp
int Monsterlevel;
int MonsterHp;
int MonsterStrength;
int MonsterAttack;
int MonsterDefense;
Monsters::Monsters(void)
{
"Monster Created";
}
Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
{
cout << "Monster created using Overloaded function!\n";
MonsterHp = newHp;
cout << "Hp is: "<< MonsterHp << endl;
Monsterlevel = newLevel;
cout << "level is: " << Monsterlevel << endl;
MonsterAttack = newAttack;
cout << "Attack is: " << MonsterAttack << endl;
MonsterDefense = newDef;
cout << "Defense is: " << MonsterDefense << endl;
}
Monsters::~Monsters(void)
{
cout << "\nMonster Destroyed";
}
//Characters.h
#pragma once
class Characters
{
private:
int level;
int Hp;
int Strength;
int Attack;
int Defense;
public:
Characters(void);
Characters(int);
Characters(int, int, int, int);
~Characters(void);
int getAttack();
int getDefense();
int getStrength();
int getHp();
int getLevel();
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setlevel(int);
};
//Characters.cpp
Characters::Characters(void)
{
cout << "\nCharacter has been created!\n";
}
Characters::Characters(int random)//How can i make this work?
{
cout << "Level " << level << " character created with: \n";
/*srand ((unsigned)time(0));
random = rand() % 10 + 1;
setlevel(int random);*/
level = random;
}
Characters::~Characters(void)
{
cout << "Character has been destroyed!\n";
}
void Characters::setAttack(int att)//get Character left over hp
{
Attack = att;
}
void Characters::setDefense(int def)//get Character left over hp
{
Defense = def;
}
void Characters::setStrength(int str)//get Character left over hp
{
Strength = str;
}
void Characters::setHp(int damage)//get Character left over hp
{
Hp -= damage;
}
void Characters::setlevel(int lvl)//get Character left over hp
{
level = lvl;
}
int Characters::getAttack()
{
cout << "Your attack is: " << Attack << endl;
return Attack;
}
int Characters::getDefense()
{
cout << "Your defense is: " << Defense << endl;
return Defense;
}
int Characters::getStrength()
{
cout << "Your strength is: " << Strength << endl;
return Strength;
}
int Characters::getHp()
{
cout << "Your hp is: " << Hp << endl;
return Hp;
}
int Characters::getLevel()
{
cout << "Your level is: " << level << endl;
return level;
}