The idea is to create an attack class and a hero class and a monster class in Python. The Hero and the Monster will get a specific attack from the attack class and I want to define who is attacked by passing the object in the method. My problem is how I can get access in this method the attributes of the new class.
Let me give an example:
class Hero:
def __init__(self,health,strength,attack):
self.health = health
self.strength = strength
self.attack = attack
class Monster:
def __init__(self,health,strength,attack):
self.health = health
self.strength = strength
self.attack = attack
class Attacks:
def bite(self,who):
print('I bite')
who.health -= self.strength #This should be the strength of the one attacking
def hit(self,who):
print('I hit')
who.health -= self.strength #This should be the strength of the one attacking
attacks = Attacks()
my_hero = Hero(100,10,attack = attacks.hit)
my_monster = Monster(100,10,attack = attacks.bite)
print(my_monster.health)
my_hero.attack(my_monster)
print(my_monster.health)
This is not working as it doesn't know in the class attacks self.strength. if I use a number instead then it works:
class Attacks:
def bite(self,who):
print('I bite')
who.health -= 10
def hit(self,who):
print('I hit')
who.health -= 10
This is working fine but then I lose the flexibility to access the strength of the one attacking.