The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

118 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


To turn this adventure into a proper game, you’re going to need more items, another room,
and the ability to ‘win’ by leaving the house with all the items safely in your inventory. Start
by adding another room, just as you did for the Dining Room – only this time, it’s a Garden.
Add an exit from the Dining Room dictionary, remembering to add a comma to the end of the
line above:

'south' : 'Garden'

Then add your new room to the main rooms dictionary, again remembering to add a comma
after the } on the line above as before:

'Garden' : {
'north' : 'Dining Room'
}

Add a ‘potion’ object to the Dining Room dictionary, again remembering to add the
necessary comma to the line above:

'item' : 'potion'

Finally, scroll to the bottom of the program and add the logic required to check if the player
has all the items and, if so, tell them they’ve won the game:

# player wins if they get to the garden with a key and a potion
if currentRoom == 'Garden' and 'key' in inventory and
'potion' in inventory:
print('You escaped the house... YOU WIN!')

Click Run, and try to finish the game by picking up the key and the potion before going to the
garden. Remember not to enter the Kitchen room, because that’s where the monster is!
As a last tweak for the game, add some instructions telling the player how to complete the
game. Scroll to the top of the program, where the function showInstructions() is defined,
and add the following:

Get to the Garden with a key and a potion
Avoid the monsters!

Run the game one last time, and you’ll see your new instructions appear at the very start
(Figure 5-21). Congratulations: you’ve made an interactive text-based maze game!
Free download pdf