How Pac-Man's Ghosts Actually Think (We Built the Whole Thing in Scratch)
Four Ghosts, One Power Pellet, Infinite Interesting Code
Pac-Man looks simple. It's not.
The reason it still feels alive after 45 years is the ghosts. Each one behaves differently. And their behaviour isn't random — it's a set of rules that change depending on what state the game is in.
We built a full Pac-Man clone in Scratch. Here's what we learned about how the ghosts actually work.
The Three Modes
Every ghost runs in one of three modes at any moment:
Chase mode. The ghost targets Pac-Man. Each ghost has a slightly different targeting rule — one aims directly at where Pac-Man is, one aims ahead of where Pac-Man is heading, one tries to flank from a different angle. This is why they feel different to avoid.
Scatter mode. The ghost retreats to a fixed corner of the maze. This happens at set intervals and stops the ghosts from permanently hunting in a pack. It gives you breathing room — and it's designed into the original arcade game, not a glitch.
Frightened mode. Triggered by a power pellet. All four ghosts turn blue, slow down, and now Pac-Man can eat them. Their targeting logic inverts — instead of chasing, they try to move away.
These three modes form what programmers call a state machine: a system that can only be in one state at a time, with clear rules for when to switch.
State Machines in Plain English
A state machine is just three things:
- What state am I in right now?
- What do I do in this state?
- What triggers me to switch to a different state?
In Scratch, you can model this with a single variable. Call it ghost_state. Set it to 0 for chase, 1 for scatter, 2 for frightened. Every movement block checks the variable first: what is ghost_state right now?
When Pac-Man eats a power pellet, you broadcast a message that sets every ghost's ghost_state to 2. A timer starts. When the timer runs out, it resets to whichever of 0 or 1 was active before.
That's the whole ghost AI. Not magic — a variable, some timing, and a few if-blocks.
Grid Movement and the Junction Rule
Here's the other clever piece: the ghosts don't move freely around the screen. They move on a grid, and they can only change direction at junctions — places where two or more paths cross.
Between junctions, a ghost keeps moving in the same direction no matter what. This gives the game its rhythm. Once you know where the junctions are, you can predict where any ghost will be next.
In our Scratch version, we handle this by only running direction checks when a ghost sprite is lined up with the grid — when its position is a clean multiple of one tile's width. Mid-tile, the ghost just keeps going. This one rule makes the movement feel exactly like the original.
Frightened Mode Is a Full System Switch
When a power pellet hits, the game flips. Ghosts slow down. They flash white when the timer is almost up, warning you that chase mode is about to return. Each ghost you eat in a single power-pellet window scores more points than the last.
This isn't just a colour change — it's a different set of movement rules running inside the same ghost sprite. In our Scratch project, we use a separate stack of blocks that only runs when ghost_state = 2, so the frightened logic stays completely separate from the chase logic. No tangled code, no checking multiple conditions in every block.
Why This Is Real Programming
State machines appear in real software constantly:
- A traffic light cycles through red → green → amber → red
- A login screen moves from "waiting for input" to "checking password" to "logged in" (or "wrong password, try again")
- A game character switches between standing, walking, jumping, and landing
If you understand how Pac-Man's ghosts work, you understand state machines. And state machines are one of the most transferable ideas in all of programming — useful whether you're writing Scratch blocks, Python scripts, or anything else.
Scratch doesn't simplify the concept. It shows it in its clearest form.
Play It, Then Remix It
The full Pac-Man clone is live on Scratch. Play it, look at the scripts, remix it and change the ghost behaviour. What happens if you remove scatter mode? What if all four ghosts use the same targeting rule? What if frightened mode lasts twice as long?
Breaking things on purpose is one of the best ways to understand how they work.