Writing readable example games
Organize a game around the changes someone will come here to make. A writer changing a line of dialogue should be able to open the story file. A developer changing when a clue is awarded should be able to find the rule without reading how buttons are drawn.
Module boundaries
- Give each file a job you can describe in one sentence. Name it after that job. Prefer names like
story.jsandsaved-game.jsoverhelpers.js. - Keep things together when they change together. A scene’s layout and keyboard focus belong together. A save’s defaults and the checks used to restore it belong together.
- Give each piece of changing game state one owner. A screen shows the available choices and calls a named game action when the player picks one. The action decides what changes and when to save.
- Keep startup easy to trace. Open the entry file to see how the game loads, what runs on a fresh start, and what runs on resume.
- Extract a shared function when its callers need the same behavior. Keep a function beside its only caller until another responsibility gives it a natural home.
File length
Around 200 lines, review the file’s responsibilities. This is a prompt for a human review, not a build limit. Split a shorter file if it mixes unrelated jobs. Keep a longer conversation together if splitting it would make the story harder to follow.
Before extracting a module, name a real edit that would become easier to make. Then trace one player action across the proposed files. If following it requires several files that only forward the same arguments, keep those pieces together.
When the game grows
A one-room game can keep its screens together. As distinct parts of play grow, organize code around the names players and developers use, such as the market, farm, or road encounters. A good time to give a feature a directory is when an ordinary change to it repeatedly sends you through files full of unrelated features.
- Start with a real edit. To change how a market purchase works, a developer should find the stall and the purchase rule under
market/. Add files for jobs that actually exist, such asstall.tsxandbuy-paint.ts. A feature with one small rule can stay one file. - Keep a whole player action traceable. Buying paint checks stock and gold, charges the player, grants the paint, and marks it sold. Give that decision one named operation. Save the resulting changes together so a reload cannot restore only half the purchase. The stall handles the click and displays the result.
- Separate a decision from its display when they change independently. The encounter button displays a countdown. Choosing a chapter, battle, or adventure belongs to the encounter rules. Pass those rules the facts they need, including the random roll, so a test can exercise each outcome without mounting the screen.
- Keep shared systems narrow. The save system owns reading, writing, versions, and migrations. Features own the meaning of their data and can supply their own validation. If both the market and farm spend gold, give the balance one owner and have both use its spending rule.
- Move one feature at a time. Preserve existing saves and callers while moving a rule. Check it at two levels: exercise the rule with known inputs, then follow a player through the screen and reload. Keep the broader directory structure until another real change warrants moving it.
Statements and names
Use the words a developer will look for: suspect, clueId, and remainingSeconds. Write each state change as a separate statement so its timing is visible. Use a block when a condition controls several actions.
Prefer named functions for actions and screens. Use short arrow callbacks for a single, local calculation. A two-way choice such as picking an image can use a ternary. Give each outcome of a longer decision its own branch. Use comments for reasons a reader cannot infer, such as why the destination must be saved before a fade.
Keep story text, choices, and their conditions together as data. Use two spaces for indentation, double quotes, semicolons, and trailing commas in multiline literals. Wrap code around 100 columns where that helps reading. A line of dialogue can stay intact for editing.
Building a screen
Use small named helpers for repeated browser work. Keep the label and the player’s action visible at the call site. Use a loop when building controls involves attaching behavior or appending elements. Use a short .map() when each input simply becomes one output.
From the accusation screen (scenes.js):
for (const suspect of SUSPECTS) {
choices.append(createButton(suspect.label, () => actions.accuse(suspect.id)));
}
Where to make an edit in The Quiet Parlour
| To change… | Start here |
|---|---|
| How play starts or how the countdown appears | Game entry (game.js) |
| What happens when a player takes a clue or the train leaves | Actions (actions.js) |
| What a player sees, clicks, or focuses | Scenes (scenes.js) |
| What survives a reload or how a saved field is checked | Saved game (saved-game.js) |
| What a suspect says or which accusation solves the case | Story (story.js) |
| How a button or text element is created | Elements (elements.js) |
| How an artist compares a mantel variation with its approved image | Sprite preview (sprites.js) |
Writing hosted docs
- Link only to other docs, images in
docs/images/, files inexamples/, or public URLs. When referring to engine source or a private repository file, put its path in backticks. - Draw flowcharts from top to bottom with
flowchart TB. Keep charts at most about four nodes wide so they fit a narrow screen. - Give every Mermaid chart an
accTitle:line describing what it shows. The hosted site uses that title as alt text.