Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Define shared attributes on class not on object solution in Clear category for The Warriors by ayubutrym
class Warrior:
attack = 5
def __init__(self, health=50):
self.health = health
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.accept_damage(self.attack)
def accept_damage(self, damage):
self.health -= damage
class Knight(Warrior):
attack = 7
def fight(unit1, unit2):
attacker, defender = unit1, unit2
while attacker.is_alive:
attacker.hit(defender)
attacker, defender = defender, attacker
return unit1.is_alive
July 6, 2022
Comments: