The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 5 Programming with Python 117

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Click the Stop button, and make the game more interesting by adding a monster to avoid.
Find the Kitchen dictionary, and add a ‘monster’ item in the same way as you added the ‘key’
item – remembering to add a comma to the end of the line above:


'item' : 'monster'

To have the monster be able to attack the player, you’ll need to add some logic to the
game. Scroll to the very bottom of the program in the script area and add the following lines



  • including the comment, marked with a hash symbol, which will help you understand the
    program if you come back to it another day – and make sure to indent the lines:


player loses if they enter a room with a monster


if 'item' in rooms[currentRoom] and 'monster' in
rooms[currentRoom]['item']:
print('A monster has got you... GAME OVER!')
break


Click Run, and try going into the Kitchen room (Figure 5-20) – the monster won’t be too
impressed when you do!


5 Figure 5-20: Never mind rats, there’s a monster in the kitchen

Free download pdf