computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

🏰

🎓

⏱ 45 min

Free

Project: Text Adventure Game

Build your own choose-your-path adventure with functions, if/else, loops and a dictionary inventory.

🎯 What you will learn

  • Plan a game with rooms and choices
  • Use functions for each room
  • Track an inventory and win/lose

👋 Let’s get started!

Let’s build a real game 🏰! Players explore a castle, make choices and collect items. You’ll use variables, input, if/elif, loops, lists and functions — everything!

🪜 Step by step

1. 🗺️ Plan your map

Draw rooms on paper: start → hallway → dragon room / treasure room. Decide what happens in each.

2. 🛠️ One function per room

Each room is a function that asks a question and decides where to go next.

def hallway():
    choice = input("Go left or right? ")
    if choice == "left":
        dragon_room()
    else:
        treasure_room()
3. 🎒 Inventory

Use a list to store what the player collects.

inventory = []
inventory.append("golden key")
if "golden key" in inventory:
    print("The door unlocks! 🔓")

🧪 Try it!

Change the code and press ▶ Run to see what happens. You can’t break anything!

Castle Adventure (try left, then key)

inventory = []

def start():
    print("🏰 You stand at the castle gate.")
    choice = input("Go left or right? ")
    if choice == "left":
        garden()
    else:
        print("A sleepy dragon blocks the way. 🐉 You tiptoe back.")
        start()

def garden():
    print("🌷 You find a shiny key in the garden!")
    inventory.append("key")
    tower()

def tower():
    action = input("A locked chest! Type key to open it: ")
    if action == "key" and "key" in inventory:
        print("✨ Treasure! You win! 🏆")
    else:
        print("The chest stays locked. Try again!")
        tower()

start()

Win condition

inventory = ["map", "key"]
if "key" in inventory:
    print("You open the treasure! 🏆")
else:
    print("Find the key first!")

📖 Key words

Tap a card to flip it over!

Game loop
Code that keeps the game going 🔁
Inventory
A list of items the player has 🎒
State
What’s true in the game right now 📍

🏆 Challenge

Add a new room! Make a function library() with a choice, and call it from start(). Test every path. (Creative challenge — no single right answer.)

Challenge

def library():
    print("📚 Dusty books everywhere...")

def start():
    choice = input("Enter the library? (yes/no) ")
    if choice == "yes":
        library()
    else:
        print("You head home. The end.")

start()

Knowledge Check Quiz

  1. Why use a function for each room?
  2. How do you check if the player has a key?
  3. What prints?
← Previous lesson