The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 5 Programming with Python 115

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


5 Figure 5-17: There are only two rooms so far


Click the Stop icon, and look in the variables area for the rooms variable. This type of
variable is known as a dictionary, and tells the game the rooms, their exits, and which room
a given exit leads to. If you scroll down to line 29 of the program in the script area, you’ll see
where this variable is created and how it’s laid out.
To make the game more interesting, add another room: a Dining Room, east of the Hall. Find
the rooms variable in the scripts area, and extend it by adding a comma symbol (,) after the }
on line 38, then typing the following (exact indentation isn't essential in a dictionary):


'Dining Room' : {
'west' : 'Hall'
}

You’ll also need a new exit in the Hall, as one isn’t automatically created for you. Go to the
end of line 33, add a comma, then add the following line:


'east' : 'Dining Room'

Click the Run icon, and try your new room: type ‘go east’ while in the Hall to enter the Dining
Room (Figure 5-18, overleaf), and type ‘go west’ while in the Dining Room to enter the Hall.
Congratulations: you’ve made a room of your own!

Free download pdf