Tutorials

Build a browser game with AI, part 1: plan and move

by

An AI coding assistant can produce a moving character quickly. The more useful skill is learning to describe a small game, inspect the changes and decide whether the result actually works. Over five tutorials published every other day from December 3 to 11, we will build Spark Run, a browser game about a little robot collecting eight sparks and escaping before its minute runs out.

The introduction to using AI for browser games sets out the approach behind this series. This is a project for readers who know basic JavaScript functions, objects and events. Use whichever coding assistant you already have access to; its output will vary. The finished game uses plain JavaScript and Canvas, with no AI service running during play. Our Game Off 2026 theme coverage offers a useful reminder of how a clear constraint can give a small project direction.

Get the reference project running

Download the Spark Run source package and extract it into a working folder. It contains five standalone checkpoints, the project brief, prompts and tests. These are reference implementations with progressively enabled features, rather than transcripts of one assistant's responses. You can build your own version alongside them or modify a copy of each checkpoint.

With Python 3 installed, open a terminal in the extracted folder and run:

python3 -m http.server 8000

On Windows, py -m http.server 8000 is an alternative. Open http://localhost:8000/part-01/, press Start run and use WASD or the arrow keys. Stop the server with Ctrl+C when finished. A local HTTP server keeps the workflow consistent when we add storage later.

Each checkpoint has four runtime files: index.html supplies the page, style.css presents it, core.js owns the rules and app.js connects input, drawing and browser events. There is no install step for the game. Node.js is only needed if you want to run the automated checks.

The teal Spark Run robot inside an otherwise empty blue grid arena.
Movement-only reference checkpoint: the robot and arena boundary. Credit: Gamedev.js reference project.

Write the brief before the prompt

Put the project's constraints in PROJECT.md. Start with an 800 × 480 arena, a robot moving at 190 logical pixels per second, eight fixed pickups, three predictable patrols, a 60-second timer and one exit. Today, only movement is enabled. Later parts add the remaining systems, then sound, touch controls and a local best time.

Ask your assistant to read that file explicitly. Different tools handle project instructions differently, so a filename alone should not be your only way of supplying context. GitHub's guidance on well-scoped coding tasks recommends clear acceptance criteria and directions about relevant files, a useful starting point for this workflow.

Try this prompt in a separate working copy:

Read PROJECT.md. Implement only the movement checkpoint using index.html, style.css, core.js and app.js. Use Canvas 2D with an 800 × 480 logical arena. Add Start and Restart, WASD and arrow-key movement at 190 pixels per second, normalized diagonal input and a boundary that keeps the whole player inside. Separate simulation from rendering. Show the changed files and explain how to verify the result. Do not add enemies, assets, packages or network calls.

Read the proposed changes before applying them. If a new library or build system appears, ask what requirement it solves. For this prototype, keeping the implementation small makes it easier to inspect an unexpected result.

Check what “190 pixels per second” means

A movement update should multiply speed by elapsed time in seconds. Adding a fixed distance every rendered frame makes the result depend on how often the screen updates. MDN's requestAnimationFrame documentation explains why the callback timestamp matters when calculating progress.

Our reference uses a fixed simulation step of 1 / 120 second, accumulated inside the animation callback. It limits a single frame's contribution to 100 milliseconds, so a long interruption does not become one enormous movement jump. That is a deliberate prototype policy: a stalled game can slow down instead of catching up indefinitely.

Inside step, the movement calculation includes:

const length = Math.hypot(x, y);
const scale = length > 1 ? 1 / length : 1;
s.player.x = clamp(
  s.player.x + x * scale * SPEED * dt,
  24 + s.player.r,
  WIDTH - 24 - s.player.r
);

The same calculation applies to y. Here x and y are input values, dt is the simulation interval, and s.player stores the position. Normalization prevents holding two directions from making the robot faster. The boundary includes the player's radius, rather than stopping only its centre at the wall.

Make the first checkpoint earn its place

Check four things before asking for another feature:

  • Straight and diagonal movement cover the same total distance over the same simulation time.
  • Releasing a key stops movement, including after leaving and returning to the window.
  • The whole robot stays within the arena's border.
  • Restart returns it to the same starting position without creating a second animation loop.

If you use the included project, run node --test part-01/core.test.cjs from its root folder for the logic checks. The test file also covers later systems because all checkpoints share the reference rules module; passing it does not replace playing today's enabled stage.

Keep a known-good copy before continuing. The focused mechanics in our js13kGames winners analysis are useful inspiration when deciding what deserves that next feature slot. In part two on December 5, the robot gets an objective, a deadline and a reason to restart.

1 comment

  1. This is a really clear way to start the series. I liked the emphasis on writing the brief in PROJECT.md first, with the 800 x 480 arena, eight sparks and the 190 pixels per second speed spelled out before prompting. The fixed 1/120 second step and the warning about diagonal movement being faster without normalisation were especially useful. I will try the movement checkpoint and the four checks at the end before moving on to part two.

Leave a Reply

Your email address will not be published. Required fields are marked *