0

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.

Richard
  • 703
  • 3
  • 11
  • 33
  • 1
    Do the answers to this [question](https://stackoverflow.com/questions/1015307/python-bind-an-unbound-method) help at all? – quamrana Aug 30 '22 at 16:49
  • Why do you want the attacks to be in a separate class. – wwii Aug 30 '22 at 16:55
  • I saw this in an exercise in a training course online but it was only writing the text. I wanted to investigate a bit further but it seems much more complex. I liked the idea that you could reuse the different attacks for different type of monsters and it would then be separated but maybe it makes it more complex in the end... – Richard Aug 30 '22 at 16:57
  • An `Attack` is a thing that operates on two players. Thus, the attach functions (which don't have to be classes) should be passed the two players. It uses the attributes of the two and adjusts them appropriately. `Hero` and `Monster` should both be subclasses of a common base, like `Player`. – Tim Roberts Aug 30 '22 at 16:57
  • ok, I think maybe it is in the end more confusing the approach I saw in the tutorial – Richard Aug 30 '22 at 16:59

1 Answers1

0

Consider a design like this, which does work. Both player types derive from a common subclass, since their behavior is identical. I have called the member "attacks", since you will probably want to have multiple attacks eventually. The attack method can choose one at random.

class Player:
    def __init__(self, health, strength, attacks):
        self.health = health
        self.strength = strength
        self.attacks = attacks

    def attack(self,other):
        self.attacks( self, other )

class Hero(Player):
    pass

class Monster(Player):
    pass

def biteattack(attacker, victim):
    print('I bite')
    victim.health -= attacker.strength

def hitattack(attacker, victim):
    print('I hit')
    victim.health -= attacker.strength

my_hero = Hero(100,10,biteattack)
my_monster = Monster(100,10,biteattack)

print(my_monster.health)
my_hero.attack(my_monster)
print(my_monster.health)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30