Create a text-based adventure game where the player explores, collects items and makes choices to find the treasure.
Long before games had fancy graphics, people played text adventures. The computer describes where you are, you type what you want to do, and the story changes depending on your choices. In this project you will write your own adventure where a hero explores a forest, a river and a mysterious cave, searching for the legendary Python Gem.
Each place in the game will be its own function. When the player finishes in one place, the function returns the name of the next place to go. A loop at the bottom of the program keeps calling the right function until the adventure is over. This is a powerful pattern that real game programmers use!
You will also give the hero an inventory, which is a list of items they are carrying. Finding a key at the river changes what happens in the cave. That means the player has to explore and think, which makes the game much more fun than just picking a path.
Every adventure needs a hero! We ask the player to name their character and store it in a variable called hero. We will use that name again at the end of the game.
print("đ Welcome to Python Adventure!")
hero = input("What is your hero's name? ")
print("Good luck, " + hero + "! Find the treasure hidden in the forest.")
đ Welcome to Python Adventure! What is your hero's name? Zara Good luck, Zara! Find the treasure hidden in the forest.
The forest is a function that describes the scene and asks which way to go. Instead of printing where to go next, it returns the name of the next place. If the player types something unexpected, we return "forest" so they get another chance.
def forest():
print()
print("đ˛ You are in a dark, whispering forest.")
print("A path goes left to a river. Another goes right to a cave.")
choice = input("Which way? (left/right) ").lower()
if choice == "left":
return "river"
elif choice == "right":
return "cave"
else:
print("You spin in a circle. Let's try that again!")
return "forest"
next_place = forest()
print("Next you go to the", next_place)
đ˛ You are in a dark, whispering forest. A path goes left to a river. Another goes right to a cave. Which way? (left/right) left Next you go to the river
The inventory is a list that starts empty. At the river, the player can pick up a golden key, and .append() adds it to the list. We check if "key" in inventory first so the player can't collect the same key twice.
inventory = []
def river():
print()
print("đ You reach a sparkling river.")
if "key" in inventory:
print("The water is calm. There is nothing else here.")
else:
print("Something shiny is glinting in the water!")
choice = input("Do you pick it up? (yes/no) ").lower()
if choice == "yes":
print("You found a golden key! đ")
inventory.append("key")
else:
print("You leave it where it is.")
print("You walk back to the forest.")
return "forest"
river()
print("Inventory:", inventory)
đ You reach a sparkling river. Something shiny is glinting in the water! Do you pick it up? (yes/no) yes You found a golden key! đ You walk back to the forest. Inventory: ['key']
In the cave there is a locked chest. If the key is in the inventory, the chest opens and the function returns "end" to finish the game. If not, the player is sent back to the forest to keep exploring.
inventory = ["key"]
def cave():
print()
print("đĻ You enter a cave. A huge locked chest sits in the middle.")
if "key" in inventory:
print("You use the golden key... the chest creaks open!")
print("đ Inside is the legendary Python Gem. You win!")
return "end"
else:
print("The chest is locked. Maybe something nearby can open it?")
return "forest"
print(cave())
đĻ You enter a cave. A huge locked chest sits in the middle. You use the golden key... the chest creaks open! đ Inside is the legendary Python Gem. You win! end
The variable place remembers where the hero is right now. The while loop keeps running until place becomes "end". Each time round, we call the function for the current place and store the place it returns, which moves the hero around the map.
place = "forest"
while place != "end":
if place == "forest":
place = forest()
elif place == "river":
place = river()
elif place == "cave":
place = cave()
Let's add a moves counter that goes up by 1 every time the loop runs. When the hero wins, we print a victory message with their name and the number of moves. Can you find the treasure in fewer moves next time?
moves = 0
# ...inside the while loop:
moves = moves + 1
# ...after the loop:
print("đ Well done, " + hero + "! You finished in", moves, "moves.")
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
inventory = []
def forest():
print()
print("đ˛ You are in a dark, whispering forest.")
print("A path goes left to a river. Another goes right to a cave.")
choice = input("Which way? (left/right) ").lower()
if choice == "left":
return "river"
elif choice == "right":
return "cave"
else:
print("You spin in a circle. Let's try that again!")
return "forest"
def river():
print()
print("đ You reach a sparkling river.")
if "key" in inventory:
print("The water is calm. There is nothing else here.")
else:
print("Something shiny is glinting in the water!")
choice = input("Do you pick it up? (yes/no) ").lower()
if choice == "yes":
print("You found a golden key! đ")
inventory.append("key")
else:
print("You leave it where it is.")
print("You walk back to the forest.")
return "forest"
def cave():
print()
print("đĻ You enter a cave. A huge locked chest sits in the middle.")
if "key" in inventory:
print("You use the golden key... the chest creaks open!")
print("đ Inside is the legendary Python Gem. You win!")
return "end"
else:
print("The chest is locked. Maybe something nearby can open it?")
return "forest"
print("đ Welcome to Python Adventure!")
hero = input("What is your hero's name? ")
print("Good luck, " + hero + "! Find the treasure hidden in the forest.")
place = "forest"
moves = 0
while place != "end":
moves = moves + 1
if place == "forest":
place = forest()
elif place == "river":
place = river()
elif place == "cave":
place = cave()
print()
print("đ Well done, " + hero + "! You finished in", moves, "moves.")
This is what it looks like when someone plays. The words after each question are what the player typed.
đ Welcome to Python Adventure! What is your hero's name? Zara Good luck, Zara! Find the treasure hidden in the forest. đ˛ You are in a dark, whispering forest. A path goes left to a river. Another goes right to a cave. Which way? (left/right) right đĻ You enter a cave. A huge locked chest sits in the middle. The chest is locked. Maybe something nearby can open it? đ˛ You are in a dark, whispering forest. A path goes left to a river. Another goes right to a cave. Which way? (left/right) left đ You reach a sparkling river. Something shiny is glinting in the water! Do you pick it up? (yes/no) yes You found a golden key! đ You walk back to the forest. đ˛ You are in a dark, whispering forest. A path goes left to a river. Another goes right to a cave. Which way? (left/right) right đĻ You enter a cave. A huge locked chest sits in the middle. You use the golden key... the chest creaks open! đ Inside is the legendary Python Gem. You win! đ Well done, Zara! You finished in 6 moves.
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.