Skip to content
Advertisement

EXP system for adjusting instance attributes in Python (text-based RPG)

I have recently begun programming in Python and decided to start my journey by writing a text-based RPG. The issue that I have now stumbled upon is that I was trying to create an EXP-system to make the character’s stats dependent on their level, but it doesn’t seem to work the way I want it to. While I have achieved to change the character’s level based on their EXP, it does not seem to affect the other instance’s values (i.e. health, strenght etc.).

First, I defined the class “Player” which is a child class:

JavaScript

After that, I defined an instance method and created a list for the EXP required to reach a certain level. (the list is actually imported, but I am going to put it here):

JavaScript

The issue that now occurs is that I can say print(p.level) and it returns the correct value, but if I let it print any other attribute: print(p.health, p.power, p.accuracy, p.speed) , they remain exactly the same as defined at the beginning.

How could I fix this so that if e.g. p.level = 4, p.health would be 220?

Edit: I put self.level at the top and tried out different values. I does calculate the other attributes correctly, but I cannot change it with my method outside of the __init__.

Advertisement

Answer

Your class variables retain the value you initialized them with until you change them. You need to modify your player_level method to update the attributes on a change of level from their old values to new values.

It should look like this:

JavaScript

Additionally you could modify your method to print out the new values and verify they are as expected.

Finally you never call your player_level method. I would have expected code like

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement