I'm making a simple text-based game. My full code is 150 lines, so I'll include what I think is relevant. Let me know if you need more.
The problem is that this line:
print("You deal " + str(hero.damage) + " damage to the monster")
returns only 5 instead of 5 + level as wanted.
class Hero:
def __init__(self):
self.level = 0
self.damage = 5 + self.level #This is the problem line. Self.level seems to be treated as equal to 0 even when it is higher than 0.
self.experience = 0
def level_up(self): #triggered on monster death
xp_required = 15
if self.experience >= xp_required:
self.level += 1
hero = Hero()
I know hero.level_up()
is successful because:
print(hero.level)
returns a level that gets correctly updated as appropriate.
I'm guessing either:
self.damage
only gets calculated once, then stores that value even after components of it have changed.
or:
There is some kind of issue with calling __init__
values within __init__
.
or:
The calculation is done on the class Hero, not the object Hero()