Project Assignment: Text-Based Adventure Games
Again, just like our last project, there are many ways you can go about creating the functionality for this game. The idea is to get creative and apply the skills you have learned so far!
Note: I added some additional comments for clarity that you wouldn't necessarily need to include in your version
# Text-Based Adventure Game
import random
# Game Data
locations = {
"forest": "a dark, mysterious forest",
"mountain": "a tall, snowy mountain",
"lake": "a serene, blue lake",
"village": "a quaint, small village",
"castle": "an ancient, abandoned castle"
}
items = [ "map", "key", "coin", "sword", "shield", "potion"]
inventory = []
score = 0
# Function Definitions
def get_user_choice(options):
print("Options:", ", ".join(options))
choice = input("Enter your choice: ").lower()
if choice in options:
return choice
else:
print("Invalid choice.")
return get_user_choice(options)
def start_adventure():
print("You find yourself at the crossroads of a big adventure.")
location = get_user_choice(list(locations.keys()))
navigate_location(location)
def navigate_location(location):
global score
print(f"You enter {locations[location]}.")
score += 10
if location == "forest":
forest_event()
elif location == "mountain":
mountain_event()
elif location == "lake":
lake_event()
elif location == "village":
village_event()
elif location == "castle":
castle_event()
def forest_event():
print("In the forest, you see a chest and a path leading deeper.")
choice = get_user_choice(["chest", "path"])
if choice == "chest":
open_chest()
elif choice == "path":
encounter_bear()
def mountain_event():
print("You face a steep climb or a hidden cave.")
choice = get_user_choice(["climb", "cave"])
if choice == "climb":
climb_mountain()
elif choice == "cave":
find_hidden_treasure()
def lake_event():
print("At the lake, you find a boat and a mysterious island.")
choice = get_user_choice(["boat", "island"])
if choice == "boat":
row_boat()
elif choice == "island":
explore_island()
def village_event():
print("You meet a merchant. Choices: trade, talk, leave")
choice = get_user_choice(["trade", "talk", "leave"])
if choice == "trade":
trade_with_merchant()
elif choice == "talk":
talk_to_merchant()
elif choice == "leave":
start_adventure()
def castle_event():
print("In the castle, you face a dragon. Choices: fight, flee")
choice = get_user_choice(["fight", "flee"])
if choice == "fight":
fight_dragon()
elif choice == "flee":
print("You flee back to the crossroads.")
start_adventure()
def trade_with_merchant():
print("You trade some items and gain a mysterious amulet.")
inventory.append("amulet")
start_adventure()
def talk_to_merchant():
print("The merchant tells you a secret about the castle.")
start_adventure()
def fight_dragon():
if "sword" in inventory and "shield" in inventory:
print("You bravely fight the dragon and win!")
score += 50
inventory .append("dragon treasure")
end_game()
else:
print("You are not equipped to fight the dragon!")
print("Game Over.")
end_game()
def open_chest():
global score
item = random.choice(items)
print(f"You found a {item} in the chest!")
inventory.append(item)
score += 5
print("You continue your journey.")
start_adventure()
def climb_mountain():
print("You reach the summit and find a mysterious artifact.")
inventory.append("artifact")
print("With the artifact in hand, you continue.")
start_adventure()
def find_hidden_treasure():
print("In the cave, you find a treasure chest.")
open_chest()
def row_boat():
print("You row the boat and find a sunken ship.")
dive_ship()
def explore_island():
print("On the island, you discover ancient ruins.")
investigate_ruins()
def encounter_bear():
print("You encounter a bear! Choices: run, stay")
choice = get_user_choice(["run", "stay"])
if choice == "run":
print("You escaped safely and continue.")
start_adventure()
elif choice == "stay":
print("The bear walks away, revealing a hidden path.")
hidden_path()
def hidden_path():
print("The path leads to a secret garden.")
print("You find a rare flower.")
inventory.append("flower")
start_adventure()
def dive_ship():
print("Underwater, you find a chest.")
open_chest()
def investigate_ruins():
print("In the ruins, you solve a puzzle revealing a gemstone.")
inventory.append("gemstone")
start_adventure()
def end_game():
print("Your adventure ends here.")
print("Inventory:", inventory)
print("Score:", score)
# Start the game
print("Welcome to the Text-Based Adventure Game!")
start_adventure()
end_game()