Python Series track

Project 12 · Practice Projects

Project 12 — Text Adventure Game

Build a text adventure game with rooms, items, NPCs, and a simple command parser. Load the world map from a JSON file.

Practice Projects1 min readFree lesson

Difficulty: Lv ★★★ | Prerequisites: Intermediate, Week 6-7

Goal

Branching-choice adventure game (5 scenes+, multiple endings)

Requirements

  • At least 5 scenes, at least 3 branches
  • State management like HP/Item
  • At least 2 endings
  • Scenario in external JSON

Challenge Task

  • Progress Save/Load
  • Scenario editor

Assignment

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution.pyPython
"""Text Adventure — Branch + HP"""
import json
from pathlib import Path

#Scenario inline (actually from a JSON file)
SCENES = {
    "start": {
        "text": "Entered a dark forest. The path splits into two.",
        "choices": {
            "1": {"label": "To the left", "next": "left", "effect": {"hp": 0}},
            "2": {"label": "To the right", "next": "right", "effect": {"hp": 0}},
        },
    },
    "left": {
        "text": "Encountered a wolf! Fight or run?",
        "choices": {
            "1": {"label": "Fight", "next": "fight", "effect": {"hp": -30}},
            "2": {"label": "Run away", "next": "escape", "effect": {"hp": -10}},
        },
    },
    "right": {
        "text": "Found an old cabin. Go inside?",
        "choices": {
            "1": {"label": "Go inside", "next": "treasure", "effect": {"hp": 0, "item": "Sword"}},
            "2": {"label": "Pass by", "next": "lost", "effect": {"hp": 0}},
        },
    },
    "fight": {"text": "Defeated the wolf and found treasure! 🏆", "ending": True, "result": "GOOD"},
    "escape": {"text": "Safely to the village. Ordinary ending.", "ending": True, "result": "NEUTRAL"},
    "treasure": {"text": "Got a sword in the cabin and became a hero! 🏆", "ending": True, "result": "GOOD"},
    "lost": {"text": "Lost in the forest and disappeared...", "ending": True, "result": "BAD"},
}

def main():
    state = {"hp": 100, "items": []}
    current = "start"
    while True:
        scene = SCENES[current]
        print(f"\n{scene['text']}")
        print(f"[HP: {state['hp']}, Items: {state['items']}]")

        if scene.get("ending"):
            print(f"\n=== Ending: {scene['result']} ===")
            break

        for k, ch in scene["choices"].items():
            print(f"  {k}. {ch['label']}")
        pick = input("> ").strip()
        if pick not in scene["choices"]:
            print("Invalid selection"); continue

        choice = scene["choices"][pick]
        effect = choice.get("effect", {})
        state["hp"] += effect.get("hp", 0)
        if "item" in effect:
            state["items"].append(effect["item"])
        if state["hp"] <= 0:
            print("\n=== HP 0! Game Over ===")
            break
        current = choice["next"]

if __name__ == "__main__":
    main()

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now