Developing a text-based adventure (part 5)

Dictionaries

Dictionaries are a data structure that allow us to store related information in our game. For example we might use a dictionary to represent the attributes of the player character. Or a dictionary might be used to store the different monsters that could be encountered in the game.

Character creation

To demonstrate the use of dictionaries in a text-based adventure game we will do a walkthrough of character creation code.

The first step in character creation is to give the character a name. This name is entered with an input statement. The character name is then stored in the playerAttributes dictionary.

import random

playerAttributes={}

response=input("What is your name brave adventurer? ")
playerAttributes["name"]=response

After the player selects their character name, they need to select the character class, which can either be a fighter, wizard or healer. The character class is stored in the playerAttributes dictionary using the “class” key.
To ensure that the player selects a valid option the code is enclosed in a loop. The loop will continue until a valid response is entered as input. A Boolean-valued variable, called validResp, is used to keep track of whether or not a valid response has been entered.

validResp=False
while (not validResp):
    response=input("You may play as a [F]ighter, [W]izard or [H]ealer > ")

    if (response.upper()=="F"):
        playerAttributes["class"]="fighter"
        validResp=True
    elif (response.upper()=="W"):
        playerAttributes["class"]="wizard"
        validResp=True
    elif (response.upper()=="H"):
        playerAttributes["class"]="healer"
        validResp=True
    else:
        print("This is not a valid option")

Next the program sets the character’s health points which are initialised using a random number generator. The random module is imported at the beginning of the code listing. In this case we use the randint function, which generates a random integer (whole number) between the given lower and upper bounds. Two new attributes are added to the player attributes dictionary, the first representing the maximum number of health points (i.e. when the character is in full health), the second representing the current number of health points.

playerAttributes["hpsMax"]=random.randint(20,30)
playerAttributes["hpsCurrent"]=playerAttributes["hpsMax"]

The next part of the code listing sets up class specific attributes. Fighters have might points which can be used to perform special attacks in close combat. Wizards have power points which can be used to cast spells. Healers have salvation points which can be used to heal the player or to create protective wards. For each of the class specific attributes attributes are initialised for the maximum number of points and the current number of points.

if (playerAttributes["class"]=="fighter"):
    playerAttributes["mightMax"]=random.randint(20,40)
    playerAttributes["mightCurrent"]=playerAttributes["mightMax"]
elif (playerAttributes["class"]=="wizard"):
    playerAttributes["powerMax"]=random.randint(20,40)
    playerAttributes["powerCurrent"]=playerAttributes["powerMax"]
elif (playerAttributes["class"]=="healer"):
    playerAttributes["salvationMax"]=random.randint(20,40)
    playerAttributes["salvationCurrent"]=playerAttributes["salvationMax"]

The final part of the code listing defines a function for printing out the player attributes. The attributes are printed within a box, which is created using special box drawing Unicode characters. Note that the box characters will not display correctly in the IDLE environment – you will need to run your program within a Python shell – you can double click on the file in Windows Explorer to run the program. A for loop is used to print out each of the individual key/value pairs in the dictionary. This will print the pairs in the order they have been added to the dictionary. The upper function has been used to convert the attribute keys to all upper case characters.

def printPlayerAttributes():
    print("\u250F"+"\u2501"*18+"\u2533"+"\u2501"*20+"\u2513")
    for k,v in playerAttributes.items():
        print("\u2503{0:<18}\u2503{1:<20}\u2503".format(k.upper(),v))
    print("\u2517"+"\u2501"*18+"\u253B"+"\u2501"*20+"\u251B")  

printPlayerAttributes()

Output from the final code is shown in the image below.

Presenting dictionary attributes inside a box

print

Leave a Reply

Your email address will not be published. Required fields are marked *