A character moving around an empty arena is a useful first checkpoint. A game needs something the player can complete or fail. Today we will turn Spark Run into a short collection challenge: find eight sparks, reach the exit and finish before the timer expires.
Start from the movement code in part one of this AI-assisted game tutorial or open the supplied part-02 checkpoint for comparison. Keep the Spark Run reference package nearby while you work; its numbered folders run independently through the same local server.
Give the run one owner
The reference keeps the current run in a single state object created by Spark.create(2). It contains the player, pickup list, elapsed time, remaining time and status. The browser-facing file reads that state to draw the arena and update the text. It does not maintain a separate score that can disagree with the rules.
The important distinction is between the loop that updates the program and the loop the player experiences. Our update loop already exists. Today's player loop is start, collect, escape, inspect the result, restart. MDN's anatomy of a game provides background on updating and presenting game state if that separation is unfamiliar.
Try this prompt against your working version:
Extend the existing movement checkpoint with eight fixed sparks and a 60-second countdown. Keep run state in core.js. Collect each spark once. The exit wins only after all eight are collected, and reaching zero time loses before any pickup or exit check that step. Freeze simulation after a win or loss. Restart must create a clean run. Add readable score, time and result text in app.js. Keep movement and the current file structure unchanged. Explain the state transitions and add focused tests for the rules.
Ask for the state transitions before accepting the patch. You should be able to trace ready → playing → won or ready → playing → lost without following several competing Boolean flags. The reference uses a single status value for that reason.

Make a pickup happen exactly once
Each spark has a position, collision radius and taken flag. The rule checks both contact and whether that spark has already been collected:
for (const cell of s.cells) {
if (!cell.taken && overlaps(s.player, cell)) {
cell.taken = true;
s.collected++;
s.events.push('pickup');
}
}
This excerpt belongs inside step, after the timeout check. The shared overlaps helper compares the distance between two centres with the sum of their radii. Rendering skips taken sparks, while the simulation skips them because of the flag. Simply hiding a sprite would not stop an invisible pickup from scoring again.
The pickup event is a small record of what happened during that update. We will use it for feedback later. It does not decide the score, and the event list is cleared at the start of each step. That separation prevents a sound or animation from accidentally becoming responsible for a game rule.
Place the sparks so the player has to cross the arena. For the initial version, fixed positions make a bug easier to reproduce. Procedural layouts can wait until the basic challenge behaves consistently.
Decide the awkward cases now
The timer uses simulated elapsed time: s.time = Math.max(0, 60 - s.elapsed). When it reaches zero, the update changes the status to lost and returns. Only a still-active run can collect a spark or reach the exit afterward.
That order answers a real design question. If the final spark, exit and deadline coincide, does the player win? In this implementation, the deadline wins. Another game could choose differently, but an assistant should not make that decision by accident through the order it writes its conditions.
The exit rule is similarly small:
if (s.collected === s.cells.length && overlaps(s.player, s.exit)) {
s.status = 'won';
s.events.push('win');
}
Until then, the exit remains visibly locked. This is the same kind of readable cause and effect that makes a compact Phaser gravity-flip platformer useful to study: the player needs to understand what changed and what action is available next.
Test a complete run, including the boring parts
Start a run and sit on a spark. The count should increase once. Visit the exit early and confirm that it stays locked. Collect the rest, enter it again and check that movement and the timer freeze. Restart after winning, then repeat after losing. Every spark and the full minute should return.
For a fast timeout check, open part-02/?debug=1, start the game and enter this in the browser console:
sparkDebug.getState().elapsed = 59.9;
The reference exposes this inspection aid only when that query parameter is present. It is a development convenience, not a score-protection system. A local game is under the player's control anyway.
Describe any failure with exact steps, the expected result and what actually happened. “Restart after winning leaves the timer at zero” gives an assistant a smaller, more useful task than “fix the game.” Save the working checkpoint once both outcomes behave correctly. In part three on December 7, we will put patrols between the robot and its destination, and use those new hazards to practice debugging.