Spielerando

Lesson 3 of 16Find a mechanic8 minFree

How to Write a Game Mechanic Down So a Test Can Check It

A pixel art beekeeper stands in front of her hive and squirts water from a spray bottle at a stream of wasps; a soaked wasp drops to the grass. Twelve blue pips at the top show the squirts in the bottle and a watering can shows the water in reserve. While she refills, a small bar over her hat runs and the pips only light up at its end; later a rain cloud fills the empty can.

The bottle holds 12 squirts, at most 4 a second however fast you press. A refill takes 1.6 seconds and the water moves from the can only at its end, so walking off halfway loses nothing. An empty bottle and an empty can only puff.

Try it: Start a refill and walk off before the bar is full: the bottle and the can keep exactly what they had. Squirt until both are empty and press again: only a puff. Each cloud you stand under puts 20 into the can; once it holds 120, what does not fit shows in red.

Keys: ←→ walk, stops a refill Space squirt, hold to keep on X refill the bottle

This little game runs this blueprint's tested code in your browser. Nothing you press is sent anywhere. Its code on GitHub.

The game above is the playable demo of our blueprint Ammo and Reload. It runs that blueprint's tested code in your browser.

A rule you can test says what happens in every situation and for every button, in words a test can check. "Refilling takes a while and should feel fair" is a wish. "A refill takes 1.6 seconds, and the water moves at its end" is a rule. This lesson writes down the rule of the spray bottle above as states, moves and what-if questions, and then you write yours.

Also in this lesson 5 things to read up on 3 questions to check yourself

The idea

The last lesson ended with one sentence you would like to press. Before your AI builds it, write its rule down in a form that leaves nothing open. It has three parts:

Why write it down, when the AI could just build it? Because every line written this way can become a test, and a test is how you find out whether the code does what you decided. In our own experiment, asking an AI to draw the states first did not make its code pass our tests more often. Running the tests was what showed which code was wrong. So the written rule is not the proof. It is what the proof is made from.

How it works

Play the game above for a minute. Wasps fly at the hive, and the beekeeper squirts water at them from a spray bottle. In the blueprint's words the bottle is the weapon and her watering can the reserve. Squirt until the bottle is empty, refill it, and watch the blue dots at the top and the number next to the can.

Step 1: the states. Four are enough:

On the blueprint's own page, the same game lights up the state it is in while you play.

Step 2: the moves. For every state, write what each button does. Think of it as a grid with one row per state and one column per button: every cell needs an answer. The bottle holds 12 squirts.

Walking changes nothing in the bottle in any state but Refilling.

Step 3: the what ifs. Go through every cell and ask what a first draft would forget. Three you can try now:

Look at the last one again. The rule is right, and the list has shown you a decision you had not made: should a refused refill get an answer, the way the first lesson of this course asked for every refused press?

Step 4: every line a test. Each line becomes one sentence of the form given …, when …, then …. "Given a bottle with 12 squirts, when refill is pressed, then no refill starts." "Given a refill that has run for half of its 1.6 seconds, when she walks, then the bottle and the can hold what they held before." The blueprint's page lists such lines for this mechanic, and its tested code passes every one of them. Now your AI has something to check its code against, and you have its output to read instead of its word.

What goes wrong

Asked to write the rule down, an AI tends to write something like this: "The player has limited water. Refilling takes a moment and should feel fair. Sometimes she runs dry, which adds tension." Each sentence sounds right. No test can check "sometimes", "a moment" or "feels fair".

Then the AI builds from its own reading of those words, and the holes sit where the what ifs would have been. Two of them are in the blueprint's list of what goes wrong. The water moves into the bottle when the refill starts, so walking off halfway gives a full bottle for free. Or a second press on refill starts the bar again, so a nervous player never finishes. Neither shows when you play normally. You have to walk off, or press twice, at the wrong moment.

You notice the shortcut by trying to answer one what if from the rule alone. What happens if she walks off halfway through a refill? If the written rule does not say, the AI decided, and you do not know what.

The fix is to turn each soft word into a number or a state. "A moment" becomes 1.6 seconds. "Sometimes she runs dry" becomes the state Dry, with what each button does in it. "Feels fair" is not part of the rule at all: it is a question for playing, and the next lessons ask it.

Tell your AI

Before you build [name of the mechanic], write its rule down so that a test can check every line. Do not write code yet.

1. States: list the few situations it can be in, so that at any moment exactly one is true. Give each a short name.
2. Moves: for every state, write what each input does: [list the player's buttons or gestures]. For each, give the next state, what changes and what the player sees. If a case cannot happen, write "cannot happen" and why.
3. What ifs: list the questions a first draft forgets for this mechanic, such as a press in the middle of something, a press when something is full or empty, and stopping halfway. Answer each one.
4. Mark every word that no test can check, such as "sometimes", "a while", "a bit", "fast" or "feels". Replace each with a number or a state, or tell me it is a question for playtesting.

Show me the whole rule and wait for my changes. Then write one test for every line, in the form "given ..., when ..., then ...". Build the mechanic, run the tests and show me their output. If a test fails, fix the code, not the test.

Try it

Read up

Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.

Check yourself: three questions

Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.

Question 1 of 3Easy

Which of these lines from a written-down rule can a test check?

All questions of this course · The quiz and the Game Coder IQ

Next lesson The Toy Test: Prototype One Mechanic Before You Build a Game

How useful was this page?

Nobody has rated this page yet.

One click, no account. It tells us which pages to make better next.

Last updated: 2026-11-27