Search code examples
pythonmethodsconstructorupdating

Why does my player object update score but doesn't keep track of whether it's alive or not?


Here is the player class.

class Character():
    def __init__(self, name, territory):
        self.waterScore = 1
        self.airScore = 1
        self.landScore = 1
        self.tribe = ""
        if territory == 1: #Water tribe
            self.airScore = 0
            self.landScore = 1
            self.waterScore = 2
            self.tribe = "Water"
        if territory == 2: #Air tribe
            self.landScore = 0
            self.waterScore = 1
            self.airScore = 2
            self.tribe = "Air"
        if territory == 3: #Land tribe
            self.waterScore = 0
            self.airScore = 1
            self.landScore = 2
            self.tribe = "Land"
        self.name = name
        self.tribeScore = 0
        if self.tribe == "Land":
            self.tribeScore = self.landScore
        elif self.tribe == "Water":
            self.tribeScore = self.waterScore
        elif self.tribe == "Air":
            self.tribeScore = self.airScore

class Player(Character):
    def __init__(self, territory):
        super().__init__("Player", territory)
        self.dead = False
        if self.dead:
            print("You have died.")
            time.sleep(2)
            print("So long...")
            time.sleep(2)
            sys.exit(0)
    def setTribeScore(self): #This is the problem area
        if self.tribe == "Water":
            self.tribeScore = self.waterScore #These three are working
        elif self.tribe == "Air":
          self.tribeScore = self.airScore
        elif self.tribe == "Land":
          self.tribeScore = self.landScore
        if self.landScore <= -2 or self.airScore <= -2 or self.landScore <= -2: #this one isn't doing anything
            self.dead = True

Here is where "setTribeScore()" is called:

"""
An enemy and the player are passed into the following function, as well as which tribe skill is at stake. The function checks whether the winner or the loser is the player in order to set their tribe score.
"""
def winFight(winner, loser, stake):
    x = getattr(winner, stake)
    setattr(winner, stake, x + 1) #the attack that the winner used gets increased
    y = getattr(loser, stake)
    setattr(loser, stake, y - 1)
    if not isinstance(winner, Player):
        del winner
        loser.setTribeScore()
        print("You lost! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(loser, "landScore"), getattr(loser, "waterScore"), getattr(loser, "airScore")))
    elif not isinstance(loser, Player):
        del loser
        winner.setTribeScore()
        print("You won! Your attributes are {} for land, {} for water, and {} for air.".format(getattr(winner, "landScore"), getattr(winner, "waterScore"), getattr(winner, "airScore")))

When I run through my game loop, the player can never die, even when a tribe score falls below -1. However, the tribe score does update, as print debugging shows, so I know that the function itself is doing something. What could be the problem?


Solution

  • These lines of code in your Player.__init__:

    if self.dead:
        print("You have died.")
        time.sleep(2)
        print("So long...")
        time.sleep(2)
    

    When exactly do you expect this to execute? What I'm getting at here is that it seems like from your line of questioning you expect this to execute whenever you set self.dead to True (when the landScore, airScore or waterScore is less than or equal to -2).

    This is not the case. The body of the if-statement in your Player.__init__ will only execute when a Player object is created (it is in the __init__ method after all) - and only then when self.dead is True, which you've hardcoded to be False by default.