←Previous page

12GAME PROJECTS · ADVENTURE

Choose your own path.

Build a text adventure: rooms, choices, a bag of things, and a hidden treasure.

IN REAL LIFE

Stories where you choose.

You pick the path, and the story changes.

Choose-your-path books

"Open the door? Go to page 12. Run away? Go to page 30."

Adventure video games

You walk room to room, pick up keys, and open locked doors.

A treasure hunt

At a party, each clue sends you to a new place until you find the prize.

THE PLAN

Four small steps.

Each step uses something you already learned.

Rooms

A box keeps the room you are in. if and elif pick what happens there.

room = "hall" if elif

Choices

input() asks left, right or quit. An if inside another if makes a nested choice.

input() if

The bag

A list keeps what you find. append adds, and in checks.

[] append in

Keep playing

A while loop repeats until you find the treasure or quit.

while

RUN IT

Find the treasure.

Guess first. Then run real Python and check.

1

Guess the result

Read the game in the editor. Then look at PyBot's answers.

PyBot's answers: right, left, right

What is the last line Python shows?

adventure.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change PyBot's answers to: right, quit. Does the treasure line appear?

BUILD IT

Build the game, piece by piece.

Each task builds one part of an adventure. Match the goal.

0Completed
11Not tried
0Review
01

Step 1: a choice inside a choice

In the tunnel, PyBot asks about the torch but does nothing with it. Add the if and else.

Goal output
Door or tunnel? tunnel
Light the torch? yes or no? yes
You see gold!
OUTPUT

02

Step 2: the bag

PyBot finds a torch but forgets to keep it. Put it in the bag.

Goal output
You find a torch!
The cave lights up.
Bag: ['torch']
OUTPUT

03

Step 3: a way out

The loop never lets the player quit. Add the missing elif.

Goal output
Dig, walk or quit? walk
You walk deeper into the cave.
Dig, walk or quit? quit
See you next time!
OUTPUT

YOUR TURN

Questions about the game.

Try each tiny question. A wrong answer becomes something to review.

04

The room box

room = "hall"

What does the box room keep in the game?

05

Pick it up

inventory = ["map"] inventory.append("key") print(inventory)

What will Python show?

06

Is the key in the bag?

inventory = [] if "key" in inventory: print("The key opens the chest.") else: print("The chest is locked.")

What will Python show?

07

Capital letters

In the hall, the player types: LEFT

if choice == "left": room = "library"

Does the player go to the library?

08

A choice inside a choice

elif room == "kitchen": if "key" in inventory:

When does Python check the if about the key?

09

When does it end?

while room != "treasure" and room != "quit":

When does this loop stop?

10

Count the questions

Run the big game with these answers.

PyBot's answers: left, right

How many times does the game ask "left, right or quit?"

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A game is a loop of choices.

A box remembers where you are, a list remembers what you carry, and while keeps the story going.