AI & prompting

How to Make a Game with AI: A Browser Game, Step by Step

To make a game with AI, pick a very small game, write its rules in a short spec, and ask the AI to build it as a single HTML file that you open in a browser. Then add one feature at a time, playing after each change and reporting exactly what went wrong.

This guide uses a browser game because it needs no engine, no install and no build step. The same method works for other engines, but the first loop of ask, run, play and fix is much faster when the result is one file.

Step 1: Pick a game small enough to finish

Most failed first attempts fail on scope, not on the AI. Choose a game with one screen, one input method and a clear win or lose condition. Good first choices are Pong, Snake, Breakout, a falling-blocks dodger, or a memory-card matching game. Avoid anything with multiplayer, saved accounts, large maps or 3D until a small game works.

Write down what "finished" means before you start, for example: "The player moves a paddle with the left and right arrow keys, a ball bounces off the walls and paddle, the score goes up for each paddle hit, and the game ends when the ball passes the paddle."

Step 2: Write a short spec before the first prompt

A spec is a list of facts the AI should not have to guess. Five to ten lines is enough:

  • Platform: one HTML file with inline JavaScript and the Canvas 2D API, no libraries, no external files.
  • Controls: which keys or mouse actions do what.
  • Rules: how you score, how you lose, how you restart.
  • Look: canvas size, colours, text on screen. Plain rectangles are fine.
  • Out of scope: sound, menus, saving, anything you do not want yet.

For a longer project, the same idea scales up as a written plan the AI follows; see spec-driven development with AI.

Step 3: Ask for a playable first version

Paste the spec into your AI assistant with a request like this:

Build a Pong-style game as a single HTML file.
Use the Canvas 2D API and plain JavaScript, no libraries.
The canvas is 640x400. The player's paddle moves with the
left and right arrow keys. A ball bounces off the walls and
the paddle. Score +1 per paddle hit. The game ends when the
ball passes the paddle; press Space to restart.
Use requestAnimationFrame and move things by elapsed time,
not by a fixed amount per frame.
Return the complete file.

Save the reply as game.html, then double-click it. If the AI split the answer into several files or added a library you did not ask for, tell it to follow the spec and return one file.

What the game loop is, so you can read the code

You do not need to write the code, but you should recognise its shape. MDN's game anatomy page describes every game as a cycle: present a situation, accept input, interpret it, and calculate a new situation. In a browser that cycle is a function that calls requestAnimationFrame on itself, so the browser runs it before each repaint. A canvas game redraws the whole frame each time: clear it, draw everything, then schedule the next frame, as MDN's canvas animation tutorial shows.

Here is the skeleton, adapted from those two MDN pages. It is an illustration, not a complete game:

const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
let previous;

function frame(timestamp) {
  if (previous === undefined) previous = timestamp;
  const elapsedMs = timestamp - previous;
  previous = timestamp;

  update(elapsedMs);   // move things, check collisions, change score
  draw();              // clearRect, then draw every shape

  requestAnimationFrame(frame);
}

requestAnimationFrame(frame);

Two details from the MDN reference are worth checking in any AI-written game. First, the callback receives a timestamp in milliseconds, and MDN says not to advance animations by a fixed amount per callback, or the game runs faster on 120 Hz and 144 Hz screens. Second, most browsers pause requestAnimationFrame in background tabs, so a game stops when you switch tabs. That is normal, not a bug.

Step 4: Add one feature per request

Once the first version runs, change one thing at a time. Good requests are specific and small:

  • "Make the ball speed up by 5% after every paddle hit."
  • "Show the high score and keep it until the page is reloaded."
  • "Add a pause with the P key."

After each reply, play for a minute before asking for the next change. If you stack five changes into one request, you cannot tell which one broke the game. Keep the last working file under a different name, such as game-v3.html, so you can go back.

Step 5: Report bugs the way a tester would

"It doesn't work" gives the AI nothing to act on. Give three things: what you did, what you expected, and what happened. Add any text from the browser console, which you open with F12 and the Console tab. For example:

I pressed Space after losing. Expected: the ball resets
and the score goes to 0. Actual: the score resets but the
ball stays off screen. Console shows no errors.

If the console does show an error, paste it verbatim. A message such as Uncaught TypeError: Cannot read properties of null usually points to a missing element or a variable used before it was set, and the line number in the message tells the AI where to look.

Where AI-written games usually go wrong

  • Speed depends on the screen. Movement per frame instead of per elapsed millisecond. Ask for time-based movement.
  • Collisions miss at high speed. A fast ball can pass through a thin paddle between two frames. Ask the AI to check the ball's path between frames, or to cap its speed.
  • Restart leaves old state. Variables such as score or ball position are not all reset. Ask for a single resetGame() function that sets every value.
  • Keys stay "stuck". The code handles keydown but not keyup.
  • The AI rewrites too much. Ask it to change only the named function and leave the rest as is.

Treat the generated code like any code you did not write. How to review AI-generated code covers what to check, and how to use AI for coding covers the wider workflow. The same step-by-step method is applied to a different project in build a website with AI step by step.

Going beyond one file

When the single file becomes hard to manage, there are two common routes. You can keep the browser approach and ask the AI to split the code into separate script files, or you can move to a game engine and give the AI the engine's name and version in the spec. Either way, state the version, because engines change between releases and the AI may otherwise write code for an older one. Check anything unfamiliar against the engine's official documentation rather than trusting the AI's description of it.

Assets are a separate problem. The code can be generated, but images, music and sound effects have licences. Use assets you made, assets with a licence that allows your use, or plain shapes and colours, and check the terms of any AI image or audio tool before you publish a game that uses its output.

Frequently asked questions

Can I make a game with AI without knowing how to code?
You can get a simple game running, because an AI assistant can write the code and tell you how to open it. But when something breaks you still have to describe what you see on screen and test each fix. Starting with a one-file browser game keeps that manageable, and you will pick up the basics as you go.
What is the easiest kind of game to make with AI?
A single-screen 2D game in the browser, such as Pong, a snake game, a breakout clone or a memory-card game. It needs only one HTML file, runs by double-clicking it, and has a small set of rules that you can describe in a few sentences and check by playing.
Which AI tool should I use to make a game?
Any assistant that can write and edit code works for a first game. A chat assistant is enough for a single HTML file. A coding assistant that edits files in a folder is more convenient once the project has several files. The method below matters more than the tool.
Why does my AI-made game run at different speeds on different computers?
The code moves things by a fixed amount on every frame, and screens refresh at different rates (60 Hz, 120 Hz, 144 Hz). Movement should be based on elapsed time, using the timestamp that requestAnimationFrame passes to your function. Ask the AI to use time-based movement.

Sources