LXF ADVENTURE
Text adventure combat mechanics
Often found making love and not war, this month Nate Drake takes our interactive text adventure down a dark, violent path.
Credit: https://github.com/azuregate
OUR EXPERT
Nate Drake wants to dedicate this series to his dad who once told him that no one was ever going to pay him to sit around and play video games.
QUICK TIP
You can download an example of the code, including items that can change the player’s hit points, from https://github.com/azuregate/ lxfpythontext adventure/blob/main/ nextroom statscript1.py.
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 remarkable, given that nothing in the game can hurt or heal them.
We could update the Item class to include an attribute to do this but this is unwieldy, plus it makes it difficult to tell the difference between different types of items. Fortunately, Python has a neat workaround for this by enabling you to create subclasses. A subclass has the same attributes as the parent class but also allows you to add other attributes of your own.
Part Three!
Don’t miss next issue, subscribe on page 18!
In this case, we’ll use this handy Python feature to create a new object called StatItem: class StatItem(Item): def __init__(self, name, itemdesc, updroomdesc, portable, revealsitem, usedin, usedesc, removesroomitem, addsroomitem, useroomdesc, disposable, hp_change): super().__init__(name, itemdesc, updroomdesc, portable, revealsitem, usedin, usedesc, removesroomitem, addsroomitem, useroomdesc, disposable) self.hp_change = hp_change As you can see, this is based on the original Item class but introduces a new attribute, hp_change. This indicates the number by which your HP changes if the StatItem is used in-game. (Note that this can be an negative value, so StatItem covers both helpful and harmful items).