Categories We Write About

Create text-based adventure games

Here’s a complete, playable text-based adventure game written in Python. You can copy and run this code in any Python environment (such as IDLE, VS Code, or online interpreters like Replit or Trinket).


python
import time def pause(): time.sleep(1) def intro(): print("nYou wake up in a dark forest. The air is cold and the moonlight flickers through the trees.") pause() print("You can barely see a narrow path to the north and a cave entrance to the east.") pause() first_choice() def first_choice(): print("nWhat do you want to do?") print("1. Go north along the path") print("2. Enter the cave") choice = input("> ") if choice == "1": path_scene() elif choice == "2": cave_scene() else: print("Invalid choice.") first_choice() def path_scene(): print("nYou walk carefully along the narrow path.") pause() print("Soon, you arrive at an old wooden bridge crossing a deep chasm.") pause() print("The bridge looks unstable.") pause() print("nWhat do you want to do?") print("1. Cross the bridge") print("2. Go back") choice = input("> ") if choice == "1": bridge_scene() elif choice == "2": first_choice() else: print("Invalid choice.") path_scene() def bridge_scene(): print("nYou take a deep breath and step onto the bridge...") pause() print("Halfway across, the wood creaks loudly under your feet!") pause() print("You sprint the rest of the way and make it across safely.") pause() print("On the other side, you find a small village. You've escaped the forest!") pause() print("Congratulations, you win!n") def cave_scene(): print("nYou step into the dark cave. It's damp and echoes with distant sounds.") pause() print("You see a torch on the wall and a tunnel leading deeper into the cave.") pause() print("nWhat do you want to do?") print("1. Take the torch and go deeper") print("2. Leave the cave") choice = input("> ") if choice == "1": deep_cave_scene() elif choice == "2": first_choice() else: print("Invalid choice.") cave_scene() def deep_cave_scene(): print("nWith the torch in hand, you walk deeper into the cave.") pause() print("Suddenly, you hear a growl. A giant bear is blocking your path!") pause() print("nWhat do you want to do?") print("1. Fight the bear") print("2. Run back") choice = input("> ") if choice == "1": fight_scene() elif choice == "2": cave_scene() else: print("Invalid choice.") deep_cave_scene() def fight_scene(): print("nYou raise the torch and charge at the bear.") pause() print("The flames scare the bear, and it backs off into a side tunnel.") pause() print("You proceed and find a hidden treasure chest filled with gold!") pause() print("Congratulations, you win!n") # Start the game intro()

How it works:

  • Each function represents a scene or choice point in the game.

  • The input() function allows player decisions.

  • time.sleep() pauses add drama.

  • The player can win by finding the village or the treasure.

You can expand this game by:

  • Adding inventory (e.g., sword, map)

  • Adding random encounters

  • Using a class for player stats

If you’d like a more complex version, such as saving progress, using files, or building with a GUI or web front-end, I can provide that too.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About