Credit: https://github.com/azuregate
I fyou’ve been following the previous tutorials on creating your own Python text adventure you should now have a script capable of moving the player around any number of rooms and interacting with items.
This is fine but most dungeon crawlers usually involve some element of risk. Of course, as the code stands, it’s quite possible to create a room to trap a player by removing all the exit attributes, but most games usually give the hero a certain quantity of health or hit points, which they have to guard carefully.
Hit me up
In order to make our hero sufficiently mortal, we first need to update the Player class in order to introduce the hit points attribute:
class Player: def __init__(self, name, currentroom, keyring, hp, inventory): self.name = name self.currentroom = currentroom self.keyring = keyring self.hp = hp self.inventory = inventory
This also means we need to assign a certain number of hit points (hereafter known as HP) to our player at the start of the script: player = Player(player_name, room1, [], 10, [bread])
Note in this case we’ve decided to assign the player 10HP but feel free to change this according to how risky or easy you want to make your adventure.
Inheriting items
Our player now has a certain number of HP but we haven’t done anything particularly