Adventure Kit

Write a choose-your-own-adventure story in a plain text file. Adventure Kit turns it into a game that runs in any web page, with a title menu, health, items, locked and timed choices, scene art and music.

Download Adventure Kit 1.0.0

You need both files.

New to the format? Start from the starter story (starter-adventure.yml).

Play the Space Adventure A full game made with Adventure Kit

Quick start

  1. Download adventure-kit.min.js and adventure-kit.min.css, and put them in the same folder as your web page.
  2. Download the starter story into that folder too, or write your own story file. Name it whatever you like; these examples use adventure.yml.
  3. Add the style sheet and script to your page, and an element that points at the story file:
    <link rel="stylesheet" href="adventure-kit.min.css">
    <script src="adventure-kit.min.js" defer></script>
    
    <div data-adventure-kit="adventure.yml" style="height: 100vh"></div>
    The game fills that element, so give it a height. 100vh fills the browser window.
  4. Open the page from a web server. Browsers won't load the story file into a page you open straight from your disk (an address starting with file://). Any server works. If you have Node.js, run npx serve in the folder and open the address it prints.

How a game plays

The game opens on a title menu built from your menu section, with switches for sound and timed choices. When the player starts, the game asks the questions in your questionnaire (if there are any), then shows your first screen.

Each screen has a heading, some text and a list of choices. Picking a choice moves the player to another screen. Arriving at a screen can add or take away health and items, and a status bar along the top shows both. Some choices stay locked until the player has a certain item. Some screens count down and move on by themselves when time runs out.

A run ends in one of two ways:

  • Defeat: health drops below your minimum. The game shows that screen's text, then "Health ran out" (with your name for health) and the buttons Try again and Main menu.
  • Ending: the player reaches a screen with nothing left to choose. The game shows "The end" and the buttons Play again and Main menu.

The Menu button in the status bar returns to the title menu at any time, and Resume goes back to the story.

The story file

A story is one YAML file: a plain text format where indentation shows what belongs to what, and key: value sets a value. Indent with spaces, never tabs. A story file has five sections:

SectionRequiredWhat it holds
adventureYesThe title and a few details about the story
settingsNoLogo, scene art and music, health, and the items players can carry
menuYesThe title menu, including the option that starts the story
questionnaireNoQuestions asked before the story starts
screensYesEvery moment in the story and the choices that connect them

You name each screen, item, scene, question and menu option yourself. That name is its ID, and the rest of the file uses the ID to refer to it. Use letters, numbers and underscores, like fetch_oil.

Put text in quotes if it contains a colon followed by a space, or starts with a character YAML treats specially, such as {, [, * or #. Long text that starts with > doesn't need quotes; see Writing text.

adventure

adventure:
  title: The Last Lamp
  description: A storm, a dark lighthouse, and a ship heading for the rocks.
  author:
    name: Your Name
  version: 1.0
  kit_version: 1.0
FieldWhat it does
titleRequired. The game's name on the title menu, and the heading of any screen that has no name.
descriptionOne line shown under the title.
author.name, versionFor your own records. The game doesn't show them.
kit_versionThe Adventure Kit version you wrote the story for. If its first number doesn't match the kit's (2.0 on Adventure Kit 1.0, for example), the game won't load and says why.

settings

Everything here is optional. Leave out the whole section and you get a game with no art or music, 5 of 10 health, and no items.

settings:
  logo: images/logo.svg
  default:                      # The title menu, and any screen whose scene leaves something out
    background: images/sky.jpg
    sounds:
      music: sounds/storm.mp3   # Loops while it plays
      select: sounds/click.mp3  # Plays when the player picks something
  scenes:
    tower:                      # A scene ID that screens can use
      background: images/tower.jpg
      sounds:
        music: sounds/wind.mp3
  health:
    name: Warmth
    namePlural: Warmth
    max: 5
    min: 1
    default: 4
    image: images/flame.svg
  inventory:
    name: Supplies
    contents:
      oil:                      # An item ID
        name: Can of lamp oil
        namePlural: Cans of lamp oil
        description: Enough to keep the lamp lit until morning.
        image: images/oil.svg
        start: 0
        max: 1

Logo and scenes

FieldWhat it does
logoAn image above the title on the title menu.
defaultThe background and sounds for the title menu, and for any screen whose scene leaves one out.
scenesNamed sets of background, sounds.music and sounds.select. A screen picks one with scene: tower. Leave out any of the three and the default fills in.

Health

Every game has health, shown in the status bar as one cell per point. Keep max at 10 or less so the cells fit on a phone.

FieldDefaultWhat it does
nameHealthWhat health is called in your story: Warmth, Life Support, Courage.
namePluralSame as nameUsed for amounts other than 1, as in "Needs 2 Life Support Units".
max10The most health a player can have.
min1Health below this ends the run in defeat. With the default of 1, the run ends at 0.
default5Starting health.
imageNoneAn icon beside the health cells.

Items

Each entry under inventory.contents is something the player can carry. Every item stays in the status bar, faded while the player has none, so players can see what's worth looking for. Pointing at an item shows its description.

FieldDefaultWhat it does
inventory.nameInventoryWhat the items are called as a group. Screen readers read it out.
nameThe item IDThe item's name for an amount of 1.
namePluralSame as nameThe name for other amounts.
descriptionNoneShown when the player points at the item.
imageNoneThe item's icon in the status bar.
start0How many the player starts with: a number, or the ID of a number question so the player chooses (see questionnaire).
max99The most the player can carry. Anything past it is lost.

questionnaire

Questions the player answers after pressing start and before the first screen. Leave the section out to skip straight to the story.

questionnaire:
  keeper:                       # The question ID, used as {keeper}
    question: What is the lighthouse keeper's name?
    type: string
    default: Morgan
  oil_cans:
    question: How many cans of oil did you bring?
    type: number
    min: 0
    max: 3
    default: 0
FieldWhat it does
questionRequired. The label for the answer field.
typestring for text (the default) or number for a whole number.
defaultFills the field to start with, and replaces an answer the player leaves blank.
min, maxNumber questions only. Answers outside the range are moved inside it.

Answers fill placeholders. Write {keeper} in a screen's text, its name or a choice's text, and the player's answer appears there. To let the player choose a starting amount, set an item's start to a number question's ID, as in start: oil_cans.

screens

Each screen is one moment in the story. Choices and timers reach a screen by its ID.

screens:
  start:
    content: >
      The lamp at the top of the lighthouse has gone out.

      Through the rain, {keeper} sees a fishing boat heading for the rocks.
    go:
      - fetch_oil
      - climb_tower

  fetch_oil:
    name: Run to the shed for oil
    health: -1
    inventory:
      oil: 1
    content: Soaked and shivering, {keeper} finds one last can of oil.
    go:
      - climb_tower

  climb_tower:
    name: Climb the tower and light the lamp
    scene: tower
    condition:
      - type: inventory
        item: oil
        comparison: greater
        amount: 0
    timer:
      duration: 10
      to: too_late
    content: The matches are damp, and the boat is close now.
    go:
      lamp_lit:
        name: Strike the last dry match
FieldWhat it does
contentRequired. The screen's text. See Writing text.
nameThe screen's heading, and the text of any choice that leads here. Without it, the heading is your story's title.
sceneA scene ID from settings.scenes for this screen's background and music.
goThe choices on this screen. See Choices.
healthHealth gained (positive) or lost (negative) on arrival.
inventoryItems gained or lost on arrival, by item ID.
conditionWhat the player needs before choices leading here can be picked. See Conditions.
timerA countdown that moves the player on. See Timers.

Writing text

Start long text with > and indent it on the lines below. A blank line starts a new paragraph. Single line breaks join into one paragraph, so you can wrap lines however you like.

content: >
  This is the first paragraph,
  written across two lines.

  This is the second paragraph.

Placeholders like {keeper} work in content, name and choice text. A placeholder that doesn't match a question ID stops the game from loading, so typos are caught before players see them.

Choices

List the screen IDs a screen leads to under go. Each choice shows the target screen's name, so give every screen that a choice leads to a name.

go:
  - fetch_oil
  - climb_tower

To word a choice differently on one screen, write go as a map and give that choice its own name. Choices without one still use the screen's name:

go:
  fetch_oil:
  lamp_lit:
    name: Strike the last dry match

Choices appear in the order you write them, and number keys 1 to 9 pick them. A choice can go to:

TargetWhat happens
A screen IDThe player moves to that screen.
Your first screen's IDThe story starts over with the same answers, and with starting health and items. Use it for "Play again".
A menu option ID with contentThat option's text opens in a panel over the story.
backThe player returns to the title menu. The choice reads "Main menu" unless you name it.

Conditions

Conditions go on the screen being entered, not on the choice. Every choice that leads to that screen checks them, and all of them must be true.

FieldWhat it does
typeinventory to check an item, or health.
itemThe item ID, for inventory conditions.
comparisonequal (the default), greater or less.
amountThe number to compare with.

When a condition isn't met, the choice is either locked or hidden:

  • Locked when the player is missing something. The choice stays on screen, greyed out, with a note such as "Needs 1 Can of lamp oil", so players know what to look for. This happens with greater, and with equal and an amount above 0.
  • Hidden when the player has too much of something. This happens with less, and with equal and an amount of 0. Use it to stop a player picking up the same item twice:
fetch_oil:
  name: Run to the shed for oil
  inventory:
    oil: 1
  condition:              # Hidden once the player has oil
    - type: inventory
      item: oil
      comparison: equal
      amount: 0

Health and item changes

A screen's health and inventory changes happen as the player arrives, every time they arrive. If players can come back to a screen, add a condition like the one above so they can't collect the same reward twice.

The status bar shows each change for a moment, like "+1" or "−2", and screen readers announce it. Health and items never go below 0 or above their max. When health drops below min, the run ends in defeat on that screen, and its choices aren't shown.

Timers

A screen with a timer shows a countdown bar. When it runs out, the player moves to the to screen.

timer:
  duration: 10            # Seconds
  to: too_late            # Screen ID

The countdown pauses while the title menu or a panel is open, and while the player is in another browser tab. Players can turn timers off with the Timed choices switch on the title menu, and timed screens then wait for them. Give every timed screen at least one choice; without its timer, a screen with no choices is an ending.

Endings

A screen with no choices is an ending. So is a screen where every choice is locked, because the player can't go anywhere. Endings show "The end" with Play again and Main menu. A story can have as many endings as you like.

Images, music and sound

Write paths relative to the story file. If the story is at stories/adventure.yml, then images/tower.jpg means stories/images/tower.jpg. Full web addresses work too.

  • Images can be any format browsers show: SVG, PNG, JPEG or WebP. Backgrounds fill the whole game and are cropped to fit the window. On wide screens the story panel sits on the right, so keep the subject of a background left of center, or move it with --ak-backdrop-position (see Styling).
  • Music loops for as long as its scene is on screen. Use MP3, which every current browser plays. When the next screen uses different music, the two crossfade; when it uses the same file, the music carries on without restarting. Silence at the start and end of a file is trimmed, so loops don't have a gap.
  • Select sounds play when the player picks something.

Browsers block sound until the player clicks or presses a key, so music starts with the player's first click. Players can turn sound off on the title menu, and the game remembers that choice.

Playing

Every game gets these without any setup:

  • Players click or tap a choice, or press its number key (1 to 9).
  • Everything works from the keyboard: Tab moves between controls, and Enter or Space presses them.
  • Screen readers announce new screens, the countdown, and health and item changes.
  • The sound and timed-choice switches are saved in the browser for the player's next visit.
  • Animations turn off for players who set their device to reduce motion.

Styling

Every color, font and size is a CSS custom property on .ak-root, the element the game runs in. To restyle a game, load your own style sheet after adventure-kit.min.css and set the properties you want to change:

.ak-root {
  --ak-font-display: 'Bungee', sans-serif;
  --ak-font-body: 'Alegreya', Georgia, serif;
  --ak-panel-bg: #efe3c4;
  --ak-panel-ink: #17110d;
  --ak-accent: #9c3523;
  --ak-backdrop-position: 34% center;
}

The most useful properties:

PropertyControls
--ak-font-display, --ak-font-bodyHeadings and buttons; story text
--ak-font-size, --ak-line-heightStory text size and spacing
--ak-panel-bg, --ak-panel-ink, --ak-panel-mutedThe story panel and its text
--ak-accent, --ak-accent-inkMain buttons, number keys and the countdown
--ak-choice-bg, --ak-choice-bg-hover, --ak-choice-borderChoice buttons
--ak-bar-bg, --ak-meter-fill, --ak-meter-criticalThe status bar and health cells
--ak-gain, --ak-lossThe "+1" and "−1" change notes
--ak-focus, --ak-focus-on-darkThe outline around the control that has keyboard focus
--ak-backdrop-positionWhich part of the background stays in view when it's cropped
--ak-panel-width, --ak-radius, --ak-gapPanel width, corner rounding and spacing

The Space Adventure's theme.css is a complete example. If you change colors, check that text still stands out clearly from its background.

JavaScript

When the page loads, the script starts a game in every element with a data-adventure-kit attribute. To start games yourself, use the AdventureKit object the script adds to the page:

// Start a game in an element
const story = await AdventureKit.mount(document.querySelector('#game'), { src: 'adventure.yml' });

// Start games in data-adventure-kit elements added after the page loaded
AdventureKit.mountAll(document.querySelector('#new-section'));

// Check a story file without showing it
try {
  await AdventureKit.loadAdventure('adventure.yml');
} catch (error) {
  console.log(error.problems); // One message per mistake
}
FunctionWhat it does
mount(element, { src })Loads the story at src and runs it in element. Resolves with the loaded story, or with undefined if it couldn't load (the element then shows what went wrong).
mountAll(scope)Starts a game in every data-adventure-kit element inside scope. Leave out scope for the whole page.
loadAdventure(url)Downloads and checks a story file. Resolves with the story, or rejects with an error whose problems list has every mistake found.
KIT_VERSIONThe version of Adventure Kit on the page, such as "1.0".

When a story won't load

The game checks the whole story file before it starts. If anything is wrong, it shows "Adventure Kit couldn't load this adventure" and lists every problem, each starting with where in the file it is:

screens.climb_tower.go: "lamp_lt" is not a screen, a menu option, or "back"
screens.start: {keper} does not match any questionnaire entry
settings.health.max: must be a number

It catches:

  • Mistakes in the YAML itself, such as tabs or broken indentation
  • Missing required fields, and values of the wrong kind (text where a number belongs)
  • Choices, timers, scenes and items that refer to an ID that doesn't exist
  • Placeholders that don't match a question
  • A menu with no option that starts the story
  • A kit_version this version of Adventure Kit can't run

"Could not download the file" usually means the path in data-adventure-kit is wrong, or the page was opened from your disk instead of a web server (see Quick start).

License

Adventure Kit is free to use in your games, personal or commercial. You can:

  • Copy adventure-kit.min.js and adventure-kit.min.css into your own sites, apps and projects.
  • Ship them with games and stories you make, including ones you sell.
  • Restyle them with your own style sheet and call them from your own code.

You can't sell or hand out the files on their own or in a game-making product (a template, theme or tool, for example), share edited copies of them, or remove the notice at the top of each file. Stories, art and music you make are yours.

The full license has the exact terms. For anything it doesn't cover, write to contact@lettersandcolors.com.

The script includes js-yaml, used under the MIT License. The Space Adventure demo uses the Bungee and Alegreya fonts under the SIL Open Font License.