Python Project No.2: Text-based Adventure Game
This idea of text-based game immediately brought to my attention because I’m a fan of Zork. For those of you don’t know, Zork it’s one of the earliest interactive fiction computer games, and it’s text-based.
This is only the beginning of the game. The source code is at the bottom.
Input/Output
I’m using Python3 so no need to struggle with input()
and raw_input()
as the latter doesn’t exist in Python3 anymore.
For output, print()
is good enough to catch everything I need.
print("\nYou are in front of an old white house.")
= input("Would you like to step in?(yes/no)\n") response
If Statements
Ah, the magic statement in every language.
Note that Python’s If
result cannot be empty. If there’s no action, put pass
in the statement instead.
if response == "yes":
print("\nThere's a wooden door in front of you. It's locked from inside.")
elif response == "no":
print("\nGoodbye.")
quit()else:
pass
Loops
I used while
loops here. The logic is that the loop will continue until the input matches certain conditions.
= ["yes", "no"]
yes_no
while response not in yes_no:
= input("\nYou are in front of an old white house. Would you like to step in?(yes/no)\n")
response if response == "yes":
print("\nThere's a wooden door in front of you. It's locked from inside.")
elif response == "no":
print("\nGoodbye.")
quit()else:
print("\nI didn't understand that.\n")
Dedent
Print Multiple Lines without Indent
Here comes the little challenge. Enclosed with triple quotes is the way to print multiple lines, however, it will also display indent like below.
You find yourself standing in a kitchen. There’s some food on a table. On the corner, you can see a brass lantern and a knife on the counter. A hallway is in the west of the kitchen.
To avoid the extra indent, use textwrap.dedent
to remove unnecessary indent.
from textwrap import dedent
= dedent("""
desc You find yourself standing in a kitchen. There's some food on a table.
In the corner, you can see a brass lantern and a knife on the counter.
A hallway is in the west of the kitchen.
""")
print(desc)
Output: >You find yourself standing in a kitchen. There’s some food on a table. >In the corner, you can see a brass lantern and a knife on the counter. >A hallway is in the west of the kitchen.
Source Code
# Setup
from textwrap import dedent
= ["yes", "no"]
yes_no = ["west", "east", "south", "north"]
directions
# Start of game
= ""
response while response not in yes_no:
= input("\nYou are in front of an old white house. Would you like to step in?(yes/no)\n")
response if response == "yes":
print("\nThere's a wooden door in front of you. It's locked from inside.")
elif response == "no":
print("\nGoodbye.")
quit()else:
print("\nI didn't understand that.\n")
#Old House
= ""
response while response not in directions:
= input("What direction do you want to move? (west/east/north/south)\n")
response if response == "west":
print("\nYou're in the west of house. You see a window but it's locked.")
= ""
response elif response == "east":
print("\nYou're in the east of house. There's an open, small window.")
elif response == "south":
print("\nYou're already in the south of house. You can see a locked door in front of you.")
= ""
response elif response == "north":
print("\nYou're now in the north of house. There's nothing in here.")
= ""
response elif response == "exit":
print("\nYou leave the house. Goodbye,")
quit()else:
print("\nI didn't understand that.\n")
#Kitchen
= ""
response while response not in yes_no:
= input("Do you want to open the window? (yes/no) \n")
response if response == "yes":
= dedent("""
desc You find yourself standing in a kitchen. There's some food on a table.
On the corner, you can see a brass lantern and a knife on the counter.
A hallway is in the west of the kitchen.
""")
print(desc)
elif response == "no":
print("\nGoodbye.")
quit()