Text adventure
A small game in one page: rooms, choices and items, made of links.
What it shows
Build a small game in one page, where each choice is a link that jumps to a room.
What you learn
A block for the cover, headings that take their id from their own line, links with a label, a die to roll, a block that picks for you, a table of things to tick.
Try this
- Change
nightin the palette, and the cover (a gradient made from it) follows.
You fell asleep on a bench during the school trip. It is 11:40 at night, the lights are off, and the front doors are chained shut.
Not sure who you are tonight? Let the museum decide.
The Hall of Clocks
A hundred clocks tick out of step. A green exit sign glows at the far end, and a corridor leads off towards the gift shop. Under the bench you find your torch, its battery at half.
Follow the exit sign Try the gift shop
The gift shop
The shutter is down, but it stops a hand's width above the floor. You reach under it to a rack of leaflets and pull one out: a floor plan, with the staff door marked in red behind the guard's office.
Go back and follow the exit sign
The Dinosaur Hall
Your torch finds ribs, then teeth, then the whole skeleton of a Diplodocus, as long as two buses. The exit sign points at a fire door. It is alarmed, and a notice says so in three languages.
Take the stairs to the staff corridor Go back to the clocks
The guard's office
A radio plays quietly to nobody. A key card hangs on a hook by the door. Then footsteps come down the corridor: the guard is on his rounds. Roll 1d6 sneak. On a 1 or a 2 his torch sweeps the room, and you wait under the desk until he has gone.
Take the card to the staff door Back down to the dinosaurs
The staff door
You hold the card to the reader. A small light turns green, the lock clicks, and cold air comes in, smelling of rain and of chips from the van across the road.
Behind you, a hundred clocks strike midnight, nearly together.
Your bag
| item | found | where |
|---|---|---|
| torch | the Hall of Clocks | |
| floor plan | the gift shop | |
| key card | the guard's office |
How it's made
- Each room is an
@ name: Titleline. Its rule turns the line into a heading, with the name as the heading's id. - Each choice is
[[name|what you do]], a link to the room with that name. A line starting with>holds a room's choices. {torch}marks a thing to pick up. The bag is a data block, and itsfoundcolumn is a switch to tick.[roll 1d6 sneak]is a die to click, and thepickblock chooses one line for you. Both are plugins.
Try it yourself
- Add a room
@ roof: The roofwith a line of story under it, then add[[roof|Climb the fire escape]]to the choices at the staff door. The new choice takes you there. - Change
[roll 1d6 sneak]to[roll 2d6 sneak]. The die rolls two dice and adds them up. - Add
- Ingrid, the night guard, who heard a noiseto the pick block. The museum can now choose her. - Add a row
sandwich, no, your backpackto the bag. A new switch appears, ready to tick.
Next: Card games deals hands from short codes; Chess games replays a game move by move.
~~~ selfscribe
# Lesson: Build a small game in one page, where each choice is a link that jumps to a room.
# You learn: a block for the cover, headings that take their id from their own line, links with a label, a die to roll, a block that picks for you, a table of things to tick.
# Try this: change `night` in the palette, and the cover (a gradient made from it) follows.
# summary: A small game in one page: rooms, choices and items, made of links.
# category: fun
# tags: interactive, simple, creative
# uses: games
#
# ---- "use markdown" for headings and paragraphs, "use data" for the bag of things --------
use markdown
use data
set title Text adventure
set heading-font Fraunces
# ---- Colours: night for the cover, brass for choices and things you carry -------------
palette
night #22305a dark #2b3d6e
deep #0e1427 dark #0a0f1e
moon #f4efe0
brass #9a5b12 dark #f0b862
glow color-mix(in srgb, var(--brass) 15%, transparent)
# ---- "+++" … "+++": the cover, with a kicker "~ …" and the game's name "= …" --------------
block +++ ... +++ -> header.cover { padding: 1.7em 1.9em 1.5em; margin-bottom: 1.4em; border-radius: 18px; background: linear-gradient(150deg, var(--night), var(--deep)); color: var(--moon); line-height: 1.55 } as ad-cover
line ~ _ -> div.kicker { font: 700 0.7em/1 var(--doc-font-ui); letter-spacing: 0.22em; text-transform: uppercase; opacity: 0.7 } as ad-kicker
line = _ -> div.game-name { font: 600 2.6em/1.05 var(--doc-font-heading, Georgia, serif); letter-spacing: -0.02em; margin: 0.3em 0 0.35em } as ad-name
# ---- Rooms: "@ name: Title" is a place, [[name|what you do]] takes you there ------------
# id=_1 makes the name the heading's id, and href="#_1" in the link jumps to that id.
line @ _: _ -> h2.room id=_1 _2 fold 2 { padding-top: 1.1em; border-top: 1px solid color-mix(in srgb, currentColor 14%, transparent) } as ad-room
inline [[_|_]] -> a.choice href="#_1" _2 { display: inline-block; margin: 0.25em 0.35em 0.25em 0; padding: 0.4em 1em; border-radius: 999px; border: 1.5px solid var(--brass); background: var(--glow); color: var(--brass); text-decoration: none; font: 600 0.88em/1.3 var(--doc-font-ui) } as ad-choice
# "> …" holds a room's choices. It takes the place of Markdown's quote, since a later rule wins.
line > _ -> p.choices { margin: 0.9em 0 0.4em } as ad-choices
# The page colours its links; a choice keeps the brass instead.
class choices a { color: var(--brass) }
# ---- Things that happen: "* event *" lines, {things} you find, and THE END ---------------
line * _ * -> p.event { font-style: italic; opacity: 0.75; text-align: center; margin: 1.2em 0 } as ad-event
inline {_} -> span.item { font: 600 0.84em/1 var(--doc-font-ui); padding: 0.25em 0.55em; border-radius: 6px; background: var(--glow); color: var(--brass) } as ad-item
line THE END -> p.end { text-align: center; font: 600 1.5em/1 var(--doc-font-heading, Georgia, serif); letter-spacing: 0.32em; margin: 1.4em 0 1em } as ad-end
# ---- Chance: [roll 1d6] is a die to click, a ```pick block chooses one line for you -------
inline [roll _] -> dice verbatim
block ``` pick ... ``` -> pick verbatim
~~~
+++
~ A text adventure in five rooms
= Locked in at the Museum
You fell asleep on a bench during the school trip. It is 11:40 at night, the lights are off, and the front doors are chained shut.
+++
> [[clocks|Get up and look around]]
Not sure who you are tonight? Let the museum decide.
```pick
Tonight you play as
- Noor, eleven, who knows the floor plan by heart
- Mr Abebe, the teacher who counted the class wrong
- Biscuit, the museum cat, who does this every night
```
@ clocks: The Hall of Clocks
A hundred clocks tick out of step. A green exit sign glows at the far end, and a corridor leads off towards the gift shop. Under the bench you find your {torch}, its battery at half.
> [[dinosaurs|Follow the exit sign]] [[shop|Try the gift shop]]
@ shop: The gift shop
The shutter is down, but it stops a hand's width above the floor. You reach under it to a rack of leaflets and pull one out: a {floor plan}, with the staff door marked in red behind the guard's office.
> [[dinosaurs|Go back and follow the exit sign]]
@ dinosaurs: The Dinosaur Hall
Your torch finds ribs, then teeth, then the whole skeleton of a Diplodocus, as long as two buses. The exit sign points at a fire door. It is alarmed, and a notice says so in three languages.
> [[office|Take the stairs to the staff corridor]] [[clocks|Go back to the clocks]]
@ office: The guard's office
A radio plays quietly to nobody. A {key card} hangs on a hook by the door. Then footsteps come down the corridor: the guard is on his rounds. Roll [roll 1d6 sneak]. On a 1 or a 2 his torch sweeps the room, and you wait under the desk until he has gone.
> [[door|Take the card to the staff door]] [[dinosaurs|Back down to the dinosaurs]]
@ door: The staff door
You hold the card to the reader. A small light turns green, the lock clicks, and cold air comes in, smelling of rain and of chips from the van across the road.
* Behind you, a hundred clocks strike midnight, nearly together. *
THE END
> [[clocks|Play again]]
## Your bag
```data bag
item, found:toggle, where
torch, no, the Hall of Clocks
floor plan, no, the gift shop
key card, no, the guard's office
```
## How it's made
- Each room is an `@ name: Title` line. Its rule turns the line into a heading, with the name as the heading's id.
- Each choice is `[[name|what you do]]`, a link to the room with that name. A line starting with `>` holds a room's choices.
- `{torch}` marks a thing to pick up. The bag is a data block, and its `found` column is a switch to tick.
- `[roll 1d6 sneak]` is a die to click, and the `pick` block chooses one line for you. Both are plugins.
## Try it yourself
1. Add a room `@ roof: The roof` with a line of story under it, then add `[[roof|Climb the fire escape]]` to the choices at the staff door. The new choice takes you there.
2. Change `[roll 1d6 sneak]` to `[roll 2d6 sneak]`. The die rolls two dice and adds them up.
3. Add `- Ingrid, the night guard, who heard a noise` to the pick block. The museum can now choose her.
4. Add a row `sandwich, no, your backpack` to the bag. A new switch appears, ready to tick.
Next: *Card games* deals hands from short codes; *Chess games* replays a game move by move.