CognitoCoding All posts
3 July 2026 3 min read by Eris Taylor

How I Coded Pac-Man in Scratch — Grid Movement, Ghost Clones, and the State Machine Trick

scratchlearn-to-codegame-developmentstate-machinesbeginners

Building Pac-Man in Scratch sounds simple. Yellow dot. Four ghosts. Some pellets.

Then you actually try it, and three problems land at once:

  • How do you make Pac-Man only turn when he's lined up on a grid junction — not mid-corridor?
  • How do you give four ghosts different AI without copying the same sprite four times?
  • How do you flip the entire game — from "ghosts hunt you" to "you hunt ghosts" — with a single pellet?

Each one uses a different coding idea. Here's how they all work.

Watch It Get Built

Grid Movement — Only Turn at Junctions

In most Scratch games, sprites move pixel-by-pixel in any direction the moment you press a key. Pac-Man can't work that way. He follows corridors, and he can only change direction when he's exactly lined up on a grid cell.

The fix: only check for a direction change when Pac-Man's X and Y positions both sit on a multiple of your grid size.

if (x position mod 16 = 0) and (y position mod 16 = 0) then
    [check player input and change direction]
end

mod gives you the remainder after division. If x is 32, then 32 mod 16 = 0 — Pac-Man is lined up on the grid. If x is 35, 35 mod 16 = 3 — he's mid-cell, keep going straight.

One check. The jittery turning disappears.

This idea shows up in a huge number of games: RPG grids, dungeon crawlers, puzzle games. Whenever a character should snap to a grid rather than float freely, mod is the tool.

Four Ghosts, One Sprite — Clones

Blinky chases Pac-Man directly. Pinky tries to get ahead of him. Inky uses a calculation involving both of them. Clyde wanders toward his corner.

Four personalities. But you don't need four sprites.

In Scratch, one ghost sprite creates four clones at the start of the game. Each clone gets a unique ID — a number from 1 to 4 stored in a variable called ghost_id. Whenever a clone decides where to move, it checks its own ID.

if ghost_id = 1 then
    [move toward Pac-Man]
else if ghost_id = 2 then
    [aim 4 tiles ahead of Pac-Man]
else if ghost_id = 3 then
    [use Blinky + Pac-Man positions to find target]
else
    [wander toward home corner]
end

One sprite. Four brains. Each clone runs the same script but makes different decisions based on its ID.

This is the clone pattern — and it scales to any number of enemies, projectiles, particles, or collectables you'd ever want in a game. Build one, clone many.

Power Pellets — The State Machine

This is the most interesting one.

Most of the time, ghosts chase you. The moment you eat a power pellet, everything inverts — ghosts turn blue and run from you. Eat them all and they respawn. Wait long enough and they flip back to hunting.

That's a state machine. The game is always in exactly one "state", and a single event switches between them.

State What the ghosts do
Chase Head toward Pac-Man
Frightened Turn blue, flee Pac-Man
Scatter Retreat to their home corners

In Scratch, you track this with one variable: ghost_mode. Set it to "chase", "frightened", or "scatter". Every ghost clone checks ghost_mode each frame and reacts.

Pac-Man eats a power pellet:

set ghost_mode to "frightened"

Start a countdown. When it hits zero:

set ghost_mode to "chase"

One variable. The whole game responds.

State machines aren't just a game trick — they're a core programming idea. Traffic lights, login flows, vending machines, loading screens. Most interactive systems are state machines underneath. Learning to spot them is one of the biggest upgrades in how you think about code.

Remix and Build It Yourself

The full code is free on Scratch and GitHub. Remix it, change the ghost AI, add a fifth ghost, break it deliberately and fix it — that's how this stuff actually sticks.

🎮 Remix on Scratch
💻 GitHub source

The Scratch arcade series keeps going — subscribe on YouTube to catch the next build when it lands.