Build Your Own Text Adventure Game¶
Just like the programmers who built Zork before you, you're going to build your very own Text Adventure Game!
Where your game takes place and what the objective of your game is is up to you. But your game should have:
- The ability to travel multiple directions from a location (for example, go through a door on the left or the right).
- The ability to travel back to a previous location.
- At least three rooms.
An Example Game¶
Here's a simple example game called "Dork":
def input_action(valid_actions):
while True:
print("You can: ")
for action in valid_actions:
print(f"- {action}")
action = input("> ")
if action in valid_actions:
return action
else:
print("You can't do that.")
def hallway():
print("You are in the hallway of Mudd, you see the door to 825 and a mysterious door at the end of the hallway.")
valid_actions = ["enter 825", "enter mystery door"]
action = input_action(valid_actions)
if action == "enter 825":
mudd_825()
elif action == "enter mystery door":
print("You entered the mystery room, you win!")
def mudd_825():
print("You are in Mudd room 825. You see a door on the left and windows to the right.")
valid_actions = ["exit door", "look out windows"]
action = input_action(valid_actions)
if action == "exit door":
hallway()
elif action == "look out windows":
print("You look out the windows, it's a beautiful summer day")
mudd_825()
print("Welcome to Dork! The Text Adventure Game!")
mudd_825()
Feel free to copy as much or as little as you'd like from this example, but your game should feel unique to you.
Bonus¶
If you feel this is easy, try the following:
- Lock at least one room with a key, make the player have to pick up a key to unlock the room.
In [2]:
Copied!
def get_action(valid_actions):
"""Given a list of valid actions, prints them out, and asks for input.
If the input is not in the list, it asks again."""
print("Actions:")
for potential_action in valid_actions:
print(f" - {potential_action}")
user_action = input("> ")
if user_action in valid_actions:
return user_action
else:
print(f"{user_action} is invalid")
get_action(valid_actions)
def hallway(have_key):
print("You're in the hallway")
if have_key:
print("...with the key")
def classroom():
have_key = False
print("You are in 825, there is a window to the right and a door to the left")
if not have_key:
print("There's a key on the desk")
action = get_action(["open window", "pickup key", "exit door"])
if action == "open window":
print("outside is a beautiful summer day")
classroom()
elif action == "pickup key":
have_key = True
classroom()
elif action == "exit door":
print("you leave the room")
hallway(have_key)
print("Welcome to Dork")
classroom()
def get_action(valid_actions):
"""Given a list of valid actions, prints them out, and asks for input.
If the input is not in the list, it asks again."""
print("Actions:")
for potential_action in valid_actions:
print(f" - {potential_action}")
user_action = input("> ")
if user_action in valid_actions:
return user_action
else:
print(f"{user_action} is invalid")
get_action(valid_actions)
def hallway(have_key):
print("You're in the hallway")
if have_key:
print("...with the key")
def classroom():
have_key = False
print("You are in 825, there is a window to the right and a door to the left")
if not have_key:
print("There's a key on the desk")
action = get_action(["open window", "pickup key", "exit door"])
if action == "open window":
print("outside is a beautiful summer day")
classroom()
elif action == "pickup key":
have_key = True
classroom()
elif action == "exit door":
print("you leave the room")
hallway(have_key)
print("Welcome to Dork")
classroom()
Welcome to Dork You are in 825, there is a window to the right and a door to the left There's a key on the desk Actions: - open window - pickup key - exit door > open window outside is a beautiful summer day You are in 825, there is a window to the right and a door to the left There's a key on the desk Actions: - open window - pickup key - exit door > pickup key You are in 825, there is a window to the right and a door to the left There's a key on the desk Actions: - open window - pickup key - exit door > exit door you leave the room You're in the hallway
In [ ]:
Copied!