Project 37: JSON Adventure Map 🗺️¶

Difficulty: 🔴 Advanced

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/json_adventure_map/.

← Back to index

Description¶

Model a tiny text-adventure world in JSON, walk rooms in code, and export your quest log.

Libraries & Modules¶

  • json

Python Concepts You'll Practice¶

Graph data (rooms/exits), state machines, adventure logs

🎛️ Parameter Variations¶

  • Edit rooms in WORLD
  • Change PATH of moves
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "json_adventure_map"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\json_adventure_map

Step 1 — Design the world¶

In [3]:
import json

# 🎛️ TWEAK THESE
WORLD = {
    "start": {
        "title": "Neon Alley",
        "desc": "Umbrellas drip pixel rain. A door hums to the north.",
        "exits": {"north": "cafe", "east": "market"},
    },
    "cafe": {
        "title": "CodeItAll Cafe",
        "desc": "Laptops glow. Someone whispers about recursion.",
        "exits": {"south": "start", "west": "lab"},
    },
    "market": {
        "title": "Binary Bazaar",
        "desc": "Vendors sell vintage keycaps and fresh datasets.",
        "exits": {"west": "start"},
    },
    "lab": {
        "title": "Hidden Lab",
        "desc": "A single terminal blinks: READY.",
        "exits": {"east": "cafe"},
    },
}
world_path = OUTPUT_DIR / "world.json"
world_path.write_text(json.dumps(WORLD, indent=2), encoding="utf-8")
print(f"World rooms: {list(WORLD)}")
World rooms: ['start', 'cafe', 'market', 'lab']

Step 2 — Walk a scripted path¶

In [4]:
# 🎛️ TWEAK THESE
PATH = ["north", "west", "east", "south"]

room_id = "start"
log = []
for move in PATH:
    room = WORLD[room_id]
    log.append(f"[{room_id}] {room['title']}: {room['desc']}")
    nxt = room["exits"].get(move)
    if not nxt:
        log.append(f"  x cannot go {move}")
        break
    log.append(f"  > go {move} → {nxt}")
    room_id = nxt
log.append(f"END at {room_id}: {WORLD[room_id]['title']}")
text = "\n".join(log)
print(text)
(OUTPUT_DIR / "adventure_log.txt").write_text(text, encoding="utf-8")
[start] Neon Alley: Umbrellas drip pixel rain. A door hums to the north.
  > go north → cafe
[cafe] CodeItAll Cafe: Laptops glow. Someone whispers about recursion.
  > go west → lab
[lab] Hidden Lab: A single terminal blinks: READY.
  > go east → cafe
[cafe] CodeItAll Cafe: Laptops glow. Someone whispers about recursion.
  > go south → start
END at start: Neon Alley
Out[4]:
368

✅ Project complete¶

Check your output folder: outputs/json_adventure_map/

Next: Project 38: Audio Beat Stamps · Index