Pac-Man Code Fever
Thinking Through How Inky, Blinky, Pinky and Clyde
Posted by Charlie Recksieck
on 2024-10-10
Another fun one you'll see as a coding assignment is to re-create Pac-Man.
Granted, it might be more practical to work on your programming chops on a new project that you might be able to use. But by rebuilding classic software, developers deepen their understanding and learn how early programmers solved problems limited resources. Plus, it's kinda fun.
What This Article Features For You
If you're a non-programmer, it's a little bit of look under the hood but perhaps a little more understandable than normal because we all know how Inky and Blinky are supposed to chase Pac-Man.
But I also like to show pseudo-code (or what I was taught as Programming Design Language). No matter what you call it, pseudocode is just written out step-by-step plans for what the program should do - but written completely in plain English, not any particular programming language.
The Assignment
Here's a well-described Code Challenge for Pac-Man in Python.
Movement of the Pac-Man from the joystick is easy enough to conceptualize and code. The trick is that in the original Pac-Man (1980), the ghost movement is not random at all-each ghost follows a specific algorithm. Their AI is simple but brilliantly designed so the ghosts feel unpredictable while actually following deterministic rules.
Here's how the movement logic is supposed to work:
Modes
Scatter Mode - Each ghost retreats to its own corner.
Chase Mode - Each ghost uses its special targeting behavior to pursue Pac-Man.
Frightened Mode - (When Pac-Man eats a power pellet) Movement becomes semi-random and slower.
The game alternates between Scatter and Chase on a fixed timer.
The Four Ghost Behaviors
Blinky (Red) - "Chaser" - Targets Pac-Man's current tile directly and speeds up as you clear dots ("Cruise Elroy").
Pinky (Pink) - "Ambusher" - Aims for a tile four spaces ahead of Pac-Man's current direction; tries to cut you off.
Inky (Blue) - "Unpredictable" - Uses a slightly more complex calculation: Look two tiles ahead of Pac-Man; uses Blinky's position to create a vector.
Target a position based on that projection.
This makes Inky feel "random," but it's not.
Clyde (Orange) - "Shy" - If far from Pac-Man: chase like Blinky.
If within 8 tiles: run back to his corner. He oscillates between chasing and fleeing.

Why It Feels Smarter Than It Is
As you can read above, the rules and guidelines are relatively simple. But all taken together the mixture of four unique algorithms all blend into a pattern that feels like intelligent pursuit.
Some Code
Here is a little pseudocode that represents how ghost movement works in the original 1980 Pac-Man arcade code. This is not the exact code, but a reconstruction of the actual logic used in the game, based on reverse-engineering of the ROM that I found in a search.
It's safe, conceptual, and focuses on behavior (not hackable technical details).
loop every game tick:
for each ghost:
if ghost is FRIGHTENED
... move_frightened(thisGhost)
else if ghost in SCATTER mode
... ghost run scatter routine
... move towards new target
else if ghost is in CHASE mode
... calculate new chase target
... move towards new target
Update Animation
for each ghost:
if ghost is FRIGHTENED
... move_frightened(thisGhost)
else if ghost in SCATTER mode
... ghost run scatter routine
... move towards new target
else if ghost is in CHASE mode
... calculate new chase target
... move towards new target
Update Animation
Enjoy
This whole post is a little silly so we might as well leave you going full 1980’s silliness:

