11#!/bin/python3
22
33'''
4- Remix from original project published by Raspberry Pi Foundation
4+ Remix from original project published by Raspberry Pi Foundation
55https://projects.raspberrypi.org/en/projects/rpg
66Integrated code from https://github.com/xAptive
77MIT License - Copyright (c) 2019 Dave Tucker
1010
1111# NOTE: This code is in the 'master' branch
1212
13+ from map import *
14+
1315def showInstructions ():
14- # print a main menu and the commands
16+ """Print a main menu and the commands"""
17+
1518 print ('''
1619*********************
1720* Zombie House Game *
@@ -21,16 +24,19 @@ def showInstructions():
2124Get to the Garden and escape with a key and potion
2225Defend yourself against the Zombies with a sword
2326Protect yourself against the Poisons with a potion
27+ If you find a map, you can see the map using the command 'show map'
2428
2529Commands:
2630 go [direction]
2731 get [item]
32+ show map
2833 exit
29-
34+
3035''' )
3136
3237def showStatus ():
33- # print the player's current status
38+ """Print the player's current status"""
39+
3440 print ("---------------------------" )
3541 print (f"You are in the { currentRoom } " )
3642 # print the room description, if given
@@ -46,28 +52,22 @@ def showStatus():
4652 # print an item, monster or poison if there is one
4753 if "item" in rooms [currentRoom ]:
4854 print (f"You see a { rooms [currentRoom ]['item' ]} " )
55+ if "item description" in rooms [currentRoom ]:
56+ print (rooms [currentRoom ]["item description" ])
4957 if "monster" in rooms [currentRoom ]:
5058 print (f"There is a { rooms [currentRoom ]['monster' ]} in the room!" )
5159 if "poison" in rooms [currentRoom ]:
5260 print (f"There is some poison { rooms [currentRoom ]['poison' ]} in the room!" )
5361 print ("---------------------------" )
5462
55- # Create inventory, which is initially empty
56- inventory = []
57-
58- # Initial health points
59- health = 3
60-
61- # start the player in the Hall
62- currentRoom = 'Hall'
6363
6464'''
6565A dictionary is used to link a room to other rooms and
6666define what is contained inside the room.
6767The directions a player can move are in a nested dictionary.
6868Rooms can contain an item, monster or poison
6969An item is something the player can use (protect/defend)
70- whereas a monster or poison are non-playable (NPC), but can
70+ whereas a monster or poison are non-playable (NPC), but can
7171do damage to a player
7272'''
7373rooms = {
@@ -91,7 +91,7 @@ def showStatus():
9191 'item' : 'flower' ,
9292 'description' : 'A large, very clean kitchen. \n Why is that flower here?'
9393 },
94-
94+
9595 'Dining Room' : {
9696 'directions' : {
9797 'west' : 'Hall' ,
@@ -101,7 +101,7 @@ def showStatus():
101101 'item' : 'sword' ,
102102 'description' : 'A grand dining room. There is a long, majestic \n mahagony table in the center of the room'
103103 },
104-
104+
105105 'Atrium' : {
106106 'directions' : {
107107 'south' : 'Hall' ,
@@ -112,7 +112,7 @@ def showStatus():
112112 },
113113 'description' : 'An atrium with a high, vaulted ceiling. \n There are stairs leading up.'
114114 },
115-
115+
116116 'Library' : {
117117 'directions' : {
118118 'east' : 'Hall' ,
@@ -121,16 +121,19 @@ def showStatus():
121121 },
122122 'item' : 'potion' ,
123123 'description' : 'An impressive collection of very old books.'
124+
124125 },
125-
126+
126127 'Study' : {
127128 'directions' : {
128129 'north' : 'Library'
129130 },
130131 'poison' : 'hemlock' ,
131- 'description' : 'A quiet room with large, comfortable chairs.'
132+ 'item' : 'map' ,
133+ 'description' : 'A quiet room with large, comfortable chairs. You see a blank map on the table.\n ' ,
134+
132135 },
133-
136+
134137 'Parlor' : {
135138 'directions' : {
136139 'south' : 'Library' ,
@@ -139,7 +142,7 @@ def showStatus():
139142 'monster' : 'Red Zombie' ,
140143 'description' : 'A nicely decorated room with a \n number of chairs arranged in a circle.'
141144 },
142-
145+
143146 'Pantry' : {
144147 'directions' : {
145148 'west' : 'Kitchen' ,
@@ -148,7 +151,7 @@ def showStatus():
148151 'monster' : 'Green Zombie' ,
149152 'description' : 'Rows of shelves containing kitchen utensils \n and an ample supply of canned goods.'
150153 },
151-
154+
152155 'Guest Bedroom' : {
153156 'directions' : {
154157 'south' : 'Dining Room' ,
@@ -157,12 +160,12 @@ def showStatus():
157160 'poison' : 'hemlock' ,
158161 'description' : 'A small, cozy bedroom with a window looking out on the Garden'
159162 },
160-
163+
161164 'Garden' : {
162165 'directions' : {
163166 'south' : 'Atrium'
164167 },
165- 'description' : 'A magnificent, sprawling garden with every \n imaginable type of flower, shrubbery and trees.'
168+ 'description' : 'A magnificent, sprawling garden with every \n imaginable type of flower, shrubbery and trees.'
166169 },
167170
168171 'Upstairs Hall' : {
@@ -209,85 +212,115 @@ def showStatus():
209212
210213 }
211214
212- showInstructions ()
213-
214- # loop forever
215- while True :
216- try :
217-
218- showStatus ()
219-
220- # get the player's next 'move'
221- # .split() breaks it up into an list array
222- # eg typing 'go east' would give the list:
223- # ['go','east']
224- move = ''
225- while move == '' :
226- move = input ('>' )
227-
228- move = move .lower ().split ()
229-
230- # if they type 'go' first
231- if move [0 ] == 'go' :
232- # check that they are allowed wherever they want to go
233- if move [1 ] in rooms [currentRoom ]['directions' ]:
234- # set the current room to the new room
235- currentRoom = rooms [currentRoom ]['directions' ][move [1 ]]
236- # there is no door (link) to the new room
237- else :
238- print ('You can\' t go that way!' )
239-
240- # if they type 'get' first
241- if move [0 ] == 'get' :
242- # if the room contains an item, and the item is the one they want to get
243- if "item" in rooms [currentRoom ] and move [1 ] in rooms [currentRoom ]['item' ]:
244- # add the item to their inventory
245- inventory += [move [1 ]]
246- # display a helpful message
247- print (move [1 ] + ' picked up!' )
248- # delete the item from the room
249- del rooms [currentRoom ]['item' ]
250- # otherwise, if the item isn't there to get
251- else :
252- # tell player they can't get it
253- print ('Can\' t get ' + move [1 ] + '!' )
254-
255- # Allow command to exit the game
256- if move [0 ] == 'exit' :
257- break
258-
259- # Player loses health if they enter room with Zombie without a Sword
260- if 'monster' in rooms [currentRoom ] and 'Zombie' in rooms [currentRoom ]['monster' ] and 'sword' in inventory :
261- print ('You have defended yourself from the Zombie attack! ... Fantastic!' )
262- elif 'monster' in rooms [currentRoom ] and 'Zombie' in rooms [currentRoom ]['monster' ]:
263- # If monster is in room, subtract a health point
264- health -= 1
265- if health >= 1 :
266- print ("The Zombie took a bite out of you! You need to defend yourself with a sword!" )
267- else :
268- # Game ends once health reaches zero
269- print ('A Zombie has eaten your brainz ... GAME OVER!!!' )
270- break
271-
272- # Player loses health if they enter room with poison without a potion
273- if 'poison' in rooms [currentRoom ] and 'potion' in inventory :
274- print ('The potion has protected you from the Poison! ... Good Deal!' )
275- elif 'poison' in rooms [currentRoom ]:
276- # If poison is in room, subtract a health point
277- health -= 1
278- if health >= 1 :
279- print ("You are weakened by the Poison! A potion will protect you!" )
280- else :
281- # Game ends once health reaches zero
282- print ('The Poison has killed you ... GAME OVER!!!' )
215+ # start the player in the Hall
216+ currentRoom = 'Hall'
217+ rooms [currentRoom ]['position' ] = (0 ,0 ,0 )
218+ traverse_rooms (rooms )
219+
220+ if __name__ == "__main__" :
221+
222+ # Create inventory, which is initially empty
223+ inventory = []
224+ visited_rooms = []
225+
226+ # Initial health points
227+ health = 3
228+
229+ showInstructions ()
230+ traverse_rooms (rooms )
231+
232+ # loop forever
233+ while True :
234+ try :
235+
236+ showStatus ()
237+
238+ # get the player's next 'move'
239+ # .split() breaks it up into an list array
240+ # eg typing 'go east' would give the list:
241+ # ['go','east']
242+ move = ''
243+ while move == '' :
244+ move = input ('>' )
245+
246+ move = move .lower ().split ()
247+
248+ # if they type 'go' first
249+ if move [0 ] == 'go' :
250+ # check that they are allowed wherever they want to go
251+ if move [1 ] in rooms [currentRoom ]['directions' ]:
252+ # set the current room to the new room
253+ currentRoom = rooms [currentRoom ]['directions' ][move [1 ]]
254+
255+ if currentRoom not in visited_rooms and 'map' in inventory :
256+ visited_rooms .append (currentRoom )
257+
258+ # there is no door (link) to the new room
259+ else :
260+ print ('You can\' t go that way!' )
261+
262+ # if they type 'get' first
263+ if move [0 ] == 'get' :
264+ # if the room contains an item, and the item is the one they want to get
265+ if "item" in rooms [currentRoom ] and move [1 ] in rooms [currentRoom ]['item' ]:
266+ # add the item to their inventory
267+ inventory += [move [1 ]]
268+ # display a helpful message
269+ print (move [1 ] + ' picked up!' )
270+ if move [1 ] == "map" :
271+ visited_rooms .append (currentRoom )
272+ # delete the item from the room
273+ del rooms [currentRoom ]['item' ]
274+ # otherwise, if the item isn't there to get
275+ else :
276+ # tell player they can't get it
277+ print ('Can\' t get ' + move [1 ] + '!' )
278+
279+ if move [0 ] == 'show' :
280+ if "map" in inventory and move [1 ].lower () == "map" :
281+ map_rooms = {}
282+ for room in visited_rooms :
283+ map_rooms [room ] = rooms [room ]
284+ show_map (map_rooms , currentRoom )
285+ elif "map" not in inventory :
286+ print ("You do not have a map!" )
287+
288+ # Allow command to exit the game
289+ if move [0 ] == 'exit' :
290+ break
291+
292+ # Player loses health if they enter room with Zombie without a Sword
293+ if 'monster' in rooms [currentRoom ] and 'Zombie' in rooms [currentRoom ]['monster' ] and 'sword' in inventory :
294+ print ('You have defended yourself from the Zombie attack! ... Fantastic!' )
295+ elif 'monster' in rooms [currentRoom ] and 'Zombie' in rooms [currentRoom ]['monster' ]:
296+ # If monster is in room, subtract a health point
297+ health -= 1
298+ if health >= 1 :
299+ print ("The Zombie took a bite out of you! You need to defend yourself with a sword!" )
300+ else :
301+ # Game ends once health reaches zero
302+ print ('A Zombie has eaten your brainz ... GAME OVER!!!' )
303+ break
304+
305+ # Player loses health if they enter room with poison without a potion
306+ if 'poison' in rooms [currentRoom ] and 'potion' in inventory :
307+ print ('The potion has protected you from the Poison! ... Good Deal!' )
308+ elif 'poison' in rooms [currentRoom ]:
309+ # If poison is in room, subtract a health point
310+ health -= 1
311+ if health >= 1 :
312+ print ("You are weakened by the Poison! A potion will protect you!" )
313+ else :
314+ # Game ends once health reaches zero
315+ print ('The Poison has killed you ... GAME OVER!!!' )
316+ break
317+
318+ # Player wins game if they get to the Garden with the key and magic potion
319+ if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory :
320+ print ('You have escaped the house with the Key and Magic Potion ... YOU WIN!!!' )
321+ break
322+ elif currentRoom == 'Garden' :
323+ print ('To escape you need the key and potion ... Keep Looking' )
324+
325+ except KeyboardInterrupt :
283326 break
284-
285- # Player wins game if they get to the Garden with the key and magic potion
286- if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory :
287- print ('You have escaped the house with the Key and Magic Potion ... YOU WIN!!!' )
288- break
289- elif currentRoom == 'Garden' :
290- print ('To escape you need the key and potion ... Keep Looking' )
291-
292- except KeyboardInterrupt :
293- break
0 commit comments