0

I recently try to code a text based game. I want to change player proficiency when player level up. How should I change my code for this?

class Player:
    def __init__(self,name,_class,_race):
        self.name = name
        self.level = 1
        self.proficiency = (int(self.level/3)+1)*2
        self.inventory = 0
        self.skills = returnSkills()
        self.stats = returnStats()
        self._class = _class
        self._race = _race
        self.exp = 0

    def levelUp(self):
        self.level+=1


newPlayer = Player("Player","Barbarian","Human")

print(newPlayer.level)

for i in range(10):
    print(newPlayer.level)
    print(newPlayer.proficiency)
    newPlayer.levelUp()
1
  • You could make proficiency a property, so it is calculated from the current level each time it is referenced. Commented May 9, 2020 at 0:43

2 Answers 2

1

You can recalculate the proficiency attribute directly in the levelUp() function. Once you have updated the level attribute, that new value of level will be used to calculate the new proficiency.

    def levelUp(self):
        self.level+=1
        self.proficiency = (int(self.level/3)+1)*2
Sign up to request clarification or add additional context in comments.

Comments

0

You could make proficiency a property, so it is calculated from the current level each time it is referenced.

class Player:
    def __init__(self,name,_class,_race):
        self.name = name
        self.level = 1
        self.inventory = 0
        self.skills = returnSkills()
        self.stats = returnStats()
        self._class = _class
        self._race = _race
        self.exp = 0

    @property
    def proficiency(self):
        return (int(self.level/3)+1)*2
    ...

or you could leave it as a plain attribute, and recalculate it inside your levelUp method.

2 Comments

function decorators might be a little advanced for someone who is asking this question. in fact, i have been writing python for a few years now and you taught me something new, so thanks! :)
If you learned something new, you should probably up-vote this answer @ericstevens26101

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.