Zombie Text Adventure!



This week we’ll be looking at a different sort of game – the typed text adventure. I’ve tidied up and simplified  ‘Zombies in Kenilworth’ (for those who have seen it previously) so that you will be able to reuse the core code to create your own text adventures.

My version has just 6 locations and 3 in-game objects that must be collected and used to escape from the zombie apocalypse that engulfed the computer lab. I am sure that you will be able to come up with some significant enhancements – if not entirely your own take on the text adventure genre.

For those of you who like to do the typing, here it is in all its glory. I’ve marked the section CUSTOMISE THE CODE HERE where you can replace my Zombie adventure with one of your own. Simply plan our a map of locations and their connections (exits). Then decide which exits will block progress by being locked, and add in a selection of cryptic objects which must be collected, taken to the locked locations and used in order for the player to progress through your map.

See you later.

 

def 
AddGameLocation(id, name, description):
global locations_map
locations_map[id] = {
"Name" : name, "Description" : description}
locations_map[id][
"Exits"] = {}
return
 

def AddGameObject(name, description, location_id, use_location_id, use_target, use_message):
global object_list
object_list[name] = {
"Description" : description , "Location" : location_id, "UseLocation" : use_location_id, "UseTarget" : use_target, "UseMessage" : use_message}
return
 

def AddUnlockableGameLocationExit(location_id, exit_name, location_exit_id, unlocked_description, locked_description = "none"):
global locations_map

if exit_name not in locations_map[location_id]["Exits"]:
if locked_description != "none":
locations_map[location_id][
"Exits"][exit_name] = {"ExitLocation" : location_exit_id, "Status" : "locked"}
else:
locations_map[location_id][
"Exits"][exit_name] = {"ExitLocation" : location_exit_id, "Status" : "unlocked"}
locations_map[location_id][
"Exits"][exit_name]["Descriptions"] = {}


locations_map[location_id][
"Exits"][exit_name]["Descriptions"]["unlocked"] = unlocked_description
if 
locked_description !=
"none"
:
locations_map[location_id][
"Exits"
][exit_name][
"Descriptions"
][
"locked"
] = locked_description
return
 

def UseExit(exit_name):
global current_location_name
global locations_map

exit = locations_map[current_location_name][
"Exits"][exit_name]

if exit["Status"] == "unlocked":
# if this exit is unlocked then we allow the change of location
current_location_name = exit["ExitLocation"]
print("You move to " + locations_map[current_location_name]["Name"])
return True
else
:
# otherwise print this exit's description according to its current status
print("")
print(exit["Descriptions"][exit["Status"]])
print("")
return False
 


def UseObject(object_name):
global object_list
global current_location_name
global locations_map

# Return and message and error if the object is
# not in our object dictionary
if object_name not in object_list:
print("I don't know about this object")
return False
 

# Check if the player has the object (its location is 'player'
object = object_list[object_name]
if object["Location"] != "player":
print("You do not have that")
return False
 

# Check if the object is intended to be used here
if object["UseLocation"] != current_location_name:
print("You cannot use that here")
return False
 

# Get the current game location dictionary entry
game_location = locations_map[current_location_name]

# go through all the items in this location's exit dictionary
for exit_key in game_location["Exits"]:
# Check if one of the exits matches the objects UseTarget
if game_location["Exits"][exit_key]["ExitLocation"] == object["UseTarget"]:
# Unlock the exit
game_location["Exits"][exit_key]["Status"] = "unlocked"
# Print the Object's UseMessage
print(object["UseMessage"])
# Call the UseExit function to move the player to the new location
UseExit(exit_key)
return True


# if we get here we couldn't find an exit to match the use_target
# specified in this object - probably a mistake!
print
(
"This object appears to have no purpose"
)
return False
 

def Move(direction):
global current_location_name
if direction in locations_map[current_location_name]["Exits"]:
# location_exit = locations_map[current_location_name]["Exits"][direction]
if UseExit(direction):
Look()
else:
print("You cannot go that way")


def
Look():
global 
current_location_name
global 
object_list

print
(
"You are in " 
+ locations_map[current_location_name][
"Description"
])
print
(
""
)

first_item =
True
for
object_key
in 
object_list:
if 
object_list[object_key][
"Location"
] == current_location_name:
if 
first_item ==
True
:
print
(
"You can see:"
)
first_item =
False
print
(object_key +
" - " 
+ object_list[object_key][
"Description"
])

print
(
""
)

first_item =
True
game_location = locations_map[current_location_name]
for 
exit_key
in 
game_location[
"Exits"
]:
if 
first_item :
print
(
"There are exits to the:"
)
first_item =
False
print
(exit_key +
" - " 
+ game_location[
"Exits"
][exit_key][
"ExitLocation"
])
print
(
""
)


def
Take(object_name):
for 
object_key
in 
object_list:
object = object_list[object_key]
if 
object[
"Location"
] == current_location_name
and 
object_key == object_name:
object[
"Location"
] = "player"
print
(
"You take the " 
+ object_name)
return

print(
"You cannot see that here"
)


def
ShowHelp():
print
(
"Commands available:"
)
print
(
"look                      - Describes where you are"
)
print
(
"north, south, east, west  - Moves you in a direction"
)
print
(
"help                      - Prints this help"
)
print
(
"take [Object]             - Take an object with you"
)
print
(
"use  [Object]             - Use an object at this location"
)
print
(
""
)


# ============ CUSTOMISE THE CODE HERE ==========
 

# Edit this function to set the start location and intro text
def ResetGame():
global current_location_name
current_location_name =
'lab'
print(" ______________________________ ")
print("| == Zombies in Kenilworth == |")
print("| A GCC Text Adventure |")
print("|______________________________|")
print("")
print("You were at the Computer Club when suddenly someone shouted 'Zombies!!'")
print("There was panic. You hit your head...")
print("and that was the last thing you can remember")
print("")



# Your game map is made up of game locations.
# Add each one using AddGameLocation(id, name, description):
# id = unique name used by the game to identify this location
# name = printable name shown to the player
# description = long description used as part of the reponse to a 'look' by the player
 
AddGameLocation("lab", "the computer lab", "a familiar room where GCC is usually held. It is still full of computers, but they have all been smashed; As have the tables and chairs.")
AddGameLocation(
"cupboard", "the stock cupboard", "a small cupboard where the teacher normally keeps stationary and lesson equipment.")
AddGameLocation(
"shelf", "the dusty shelf", "a small shelf, covered in dust. Evidently nobody has looked up here for quite a while.")
AddGameLocation(
"corridor", "the corridor", "a normally bustling thoroughfare, but it is eerily silent.")
AddGameLocation(
"foyer", "the foyer", "the entrance to lower school and your way out!")


# This location - 'escape' is special and triggers the end of the game
AddGameLocation(
"escape"
,
"the outside world!"
,
"the outside world again. You breathe the fresh air... but then you hear the sound of moaning. Time to run!"
)

# Now connect up your game locations with exits - if you add an extra 'locked' description then the exit
# AddUnlockableGameLocationExit(location_id, exit_name, location_exit_id, unlocked_description, locked_description = "none"):
# location_id = the unique location name to which this exit belongs
# exit_name = the name used by the player to 'Use' this exit - usually a direction, so could be 'up' or 'down' etc.
# location_exit_id = the unique location name of the location to which this exit leads
# unlocked_description = the description used when the location is unlocked (the default)
# (optional) locked_description = can be left off, the description used when the location is locked. Specifying this will also lock the location
AddUnlockableGameLocationExit(
"lab"
,
"north"
,
"corridor"
,
"you go through to the corridor"
,
"the door is locked."
)
AddUnlockableGameLocationExit(
"lab"
,
"east"
,
"cupboard"
,
"you go into the cupboard"
)
AddUnlockableGameLocationExit(
"cupboard"
,
"west"
,
"lab"
,
"you go through to the computer lab"
)
AddUnlockableGameLocationExit(
"cupboard"
,
"east"
,
"shelf"
,
"you climb up to the shelf"
,
"You can see a shiny object, but the shelf is too high to reach"
)
AddUnlockableGameLocationExit(
"shelf"
,
"west"
,
"cupboard"
,
"you climb back down to the stock cupboard"
)
AddUnlockableGameLocationExit(
"corridor"
,
"south"
,
"lab"
,
"you return to the computer lab"
)
AddUnlockableGameLocationExit(
"corridor"
,
"north"
,
"foyer"
,
"you go through to the foyer"
)
AddUnlockableGameLocationExit(
"foyer"
,
"south"
,
"corridor"
,
"you return to the corridor"
)
AddUnlockableGameLocationExit(
"foyer"
,
"east"
,
"escape"
,
"you walk passed the zombie, who is too busy eating to notice you"
,
"You can see your way out, but a zombie blocks the way"
)
AddUnlockableGameLocationExit(
"escape"
,
"west"
,
"foyer"
,
"you return to the foyer"
)

# Finally add the game objects that will be used to unlock the matching exits
# AddGameObject(name, description, location_id, use_location_id, use_target, use_message)
# name = the object's name (must be unique within the game)
# description = used to describe the object to the player. Should *hint* at its potential use
# location_id = where this object will be found
# use_location_id = where this object should be used
# use_target = the name of the location exit id that it will unlock at the use location
# use_message = the printed message given when the object is successfully used
AddGameObject(
"box"
,
"a small but sturdy box"
,
"lab"
,
"cupboard"
,
"shelf"
,
"You stand on the wooden box. Now you can reach the high shelf"
)
AddGameObject(
"key"
,
"a small key like the ones the teachers carry"
,
"shelf"
,
"lab"
,
"corridor"
,
"The key fits the locked door perfectly. The lock clicks and the door swings open..."
)
AddGameObject(
"brains"
,
"a glob of partially consumed brains. You think that they might once have been Edward's..."
,
"lab"
,
"foyer"
,
"escape"
,
"The zombie greedily grabs the brains and starts munching. He seems entirely distracted."
)
 
# =================================================
 

# Here are the global variables which
# can be used by all the functions and objects
# we have created
current_location_name = ''
game_running = True
locations_map = {}
object_list = {}


# This prints the title block when the program first runs
 
ResetGame()
ShowHelp()


# Main game loop
while
game_running:
# wait for input and store it in 'command'
command =
input
(locations_map[current_location_name][
"Name"
] +
" >>"
)


# Use lower() to make everything lower case before
# doing a match test, otherwise 'Help' would not be recognised as 'help'
command = command.lower()


# split 'command' wherever there is a space to make a list of 'commands'
commands = command.split(" ")


# Call a function if the first command (position [0] in the list)
# is one we know.
if commands[0] == "look":
Look()

elif commands[0] == "help":
ShowHelp()

elif commands[0] == "north" or commands[0] == "south" or commands[0] == "east" or commands[0] == "west":
Move(commands[
0])

elif commands[0] == "take":
if len(commands) > 1:
Take(commands[
1])
else:
print("Take what?")

elif commands[0] == "use":
if len(commands) > 1:
UseObject(commands[
1])
else:
print("Use what?")

else:
print("I don't know that word. Try again.")
ShowHelp()


if 
current_location_name ==
"escape"
:
again =
input
(
"Play Again? (Y/N) >>"
)
again = again.lower()
if 
again !=
"y"
:
game_running =
False
else
:
ResetGame()

Comments


Comments powered by Disqus