In this exercise, you will build a text adventure game using functions, conditions, and loops.
Your player starts in one location. They can type commands to:
The game ends when they reach the final location with the right items.
I encourage you to work with a partner for this assignment. Two brains are better than one. But if you'd like to work alone, that's OK too.
First, think of a story you want to tell. Your story needs:
Examples:
Be creative! Make your own story.
Here's a simple game with three locations:
def location_1():
print("You're in a beautiful field.")
print("ACTION MENU:")
print("- walk east")
print("- walk west")
action = input("INPUT ACTION: ")
if action == "walk east":
print("You walk east towards a house.")
location_2()
elif action == "walk west":
location_1()
else:
print(f"Invalid action: {action}")
location_1()
def location_2():
print("You see a house with a locked door.")
print("ACTION MENU:")
print("- walk west")
action = input("INPUT ACTION: ")
if action == "walk west":
location_1()
else:
print(f"Invalid action: {action}")
location_2()
def location_3():
print("You enter the house. You won!")
# Start the game
location_1()
Your task:
Your game needs a way to lose. Add a game_over()
function:
def game_over():
print("GAME OVER")
def location_1():
# ... other code ...
elif action == "walk west":
print("You fall into a hole.")
game_over()
Let's track what items the player has. We use function arguments to pass inventory between rooms:
def location_1(has_key):
print("You're in a beautiful field.")
if not has_key:
print("A key is on the ground.")
print("ACTION MENU:")
print("- walk east")
print("- walk west")
if not has_key:
print("- pickup key")
action = input("INPUT ACTION: ")
if action == "walk east":
print("You walk east towards a house.")
location_2(has_key)
elif action == "walk west":
print("You walk west. The field continues.")
location_1(has_key)
elif action == "pickup key" and not has_key:
print("You pick up the key.")
has_key = True
location_1(has_key)
else:
print(f"Invalid action: {action}")
location_1(has_key)
def location_2(has_key):
print("You see a house with a locked door.")
print("ACTION MENU:")
print("- walk west")
if has_key:
print("- use key")
action = input("INPUT ACTION: ")
if action == "walk west":
location_1(has_key)
elif action == "use key" and has_key:
location_3(has_key)
else:
print(f"Invalid action: {action}")
location_2(has_key)
def location_3(has_key):
print("You enter the house. You won!")
# Start the game
has_key = False
location_1(has_key)
Multiple items: Add more arguments
def location_1(has_key, has_sword, has_map):
# ... your code ...
Different types: Use numbers and strings too
def location_1(health, gold_coins, player_name):
# health = 100 (number)
# gold_coins = 5 (number)
# player_name = "Alex" (string)
Track events: Use inventory to remember what happened
def location_1(talked_to_wizard, door_is_open):
# talked_to_wizard = True/False
# door_is_open = True/False
Want to avoid repeating code? Consider writing a function to handle your menus:
def three_action_menu(action_1, action_2, action_3):
print("ACTION MENU:")
print(f"1. {action_1}")
print(f"2. {action_2}")
print(f"3. {action_3}")
while True:
action = input("INPUT ACTION: ")
if action == "1" or action == action_1:
return 1
elif action == "2" or action == action_2:
return 2
elif action == "3" or action == action_3:
return 3
else:
print(f"INVALID ACTION '{action}'")
def location_1():
print("You're in a beautiful field.")
action = three_action_menu("walk east", "walk west", "sit")
if action == 1:
print("You walk east.")
location_2()
elif action == 2:
print("You walk west.")
location_1()
elif action == 3:
print("You sit and rest.")
location_1()
Note: In the above example, three_action_menu
works only when you have exactly three options. There are ways for functions to have a variable number of arguments, but we haven't learned these yet.
Make your game more interesting with randomness:
from random import randint
# Random chance to find items
def location_1():
chance = randint(0, 10)
if chance > 7:
print("You notice a key on the ground!")
# Dice rolling mini-game
def roll_dice():
result = randint(1, 6)
print(f"You rolled a {result}")
return result
# Combat system
from random import randint
def attack(target_hp, has_sword):
if has_sword:
damage = randint(3, 10)
else:
damage = randint(0, 4)
target_hp = target_hp - damage
return target_hp
def combat(player_hp, enemy_hp, has_sword):
while True:
print(f"PLAYER HP: {player_hp} / ENEMY HP: {enemy_hp}")
action = input("INPUT ACTION (attack, run): ")
if action == "attack":
print("PLAYER ATTACKS")
enemy_hp = attack(enemy_hp, has_sword)
if enemy_hp <= 0:
return True # You won!
elif action == "run":
print("PLAYER RAN")
return False
# Enemy attacks with a 50% chance
if randint(0, 1) == 1:
print("ENEMY ATTACKS")
player_hp = attack(player_hp, False)
if player_hp <= 0:
return False # You were defeated
def location_1(hp, has_sword, slayed_the_dragon):
if slayed_the_dragon:
print("No dragon here. You win!")
else:
print("Hark! There goes a dragon!")
slayed_the_dragon = combat(hp, 10, has_sword)
location_1(hp, has_sword, slayed_the_dragon)
hp = 10
has_sword = True
slayed_the_dragon = False
location_1(hp, has_sword, slayed_the_dragon)
This is your chance to be creative with programming. Make a fun game and challenge yourself!
Requirements:
When you're done with the assignment, click the "Share" button in the top-right hand corner of your notebook and add dp3305@columbia.edu
(please give me "commenter" or "editor" permissions so I can leave feedback!).
Next week, I will select my favorite (most elaborate) game to be "The Best Game" and to show the class how to put it on a website for your friends to be able to come and play.
Rules:
If you're stuck and your neighbor can't help you, try adding a post to the Ask for Help board!
https://bigd103.link/ask-for-help
I will be responding to posts from 3pm to 5pm and throughout the weekend.
For inspiration, you can play a version of Zork on the web! https://bigd103.link/play-zork
# Write you adventure here!