Talk:Roguelike Tutorial, using python3+tdl, part 6 code

From RogueBasin
Revision as of 16:35, 22 April 2018 by Zebra3 (talk | contribs) (Bugfix for player death code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


There seems to be an error in the "Untimely deaths" section that means you never die. Usually a good thing in life but not here in the game:)


Code example says:

        class Fighter:
            #combat-related properties and methods (monster, player, NPC).
            def __init__(self, hp, defense, power, death_function=None):
                self.max_hp = hp
                self.hp = hp
                self.defense = defense
                self.power = power
                self.death_function = death_function
            def take_damage(self, damage):
                #apply damage if possible
                if damage > 0:
                    self.hp -= damage
                    #check for death. if there's a death function, call it
                    if self.hp <= 0:
                        function = self.death_function
                        if function is not None:
                            function(self.owner)

I think the line in the initiation section is wrong

       self.death_function = death_function


And should in fact be

        self.death_function = player_death

This is because the code you want to call is

   def player_death(player):
        #the game ended!
        global game_state
        print('You died!')
        game_state = 'dead' 
        #for added effect, transform the player into a corpse!
        player.char = '%'
        player.color = colors.dark_red


Worked fine for me once swapped but I'm just getting into this so didn't want to change the code on the wiki directly.