← All articles

Ms. Pac-Man's Ghosts Only Scatter Once

Watch the first few seconds of a Pac-Man game a few times and you will see the same thing every time. The red ghost, Blinky, comes out of the middle of the maze and makes for the top right corner. Later in the level all four ghosts break off the chase, turn around, and drift back toward their own corners for a few seconds, then come after you again. It happens on a schedule, and players learned the schedule.

Ms. Pac-Man looks like the same game with new mazes. But at the start of a level the ghosts go somewhere different almost every game, and once they start chasing they never take that break again.

Both changes come from a few bytes in the original arcade code. This article walks through them in the program that ran on the Ms. Pac-Man boards.

Scatter and chase

In Pac-Man, the ghosts switch between two moods. In chase each ghost heads for a spot worked out from where Pac-Man is. In scatter each one heads for its own fixed corner, just outside the maze: Blinky the top right, Pinky the top left, Inky the bottom right, Clyde the bottom left. Every time the mood changes, all four ghosts turn around.

A clock decides the mood. On the first level of Pac-Man it goes like this:

Scatter and chase on level 1, Pac-Man against Ms. Pac-Man A timeline of the first 90 seconds of level 1. Pac-Man alternates: scatter 7 seconds, chase 20, scatter 7, chase 20, scatter 5, chase 20, scatter 5, then chase for good, with the ghosts turning around at every change. Ms. Pac-Man scatters for the first 7 seconds and then chases for the rest of the life; the ghosts still turn around once more at about 27 seconds. PAC-MAN MS. PAC-MAN turn around, keep chasing random 0 20 40 60 80 s scatter chase ghosts turn around
Pac-Man alternates scatter and chase four times; Ms. Pac-Man scatters once and then chases for the rest of the life.

Seven seconds of scatter, twenty of chase, seven of scatter, twenty of chase, then two shorter scatters, and after that the ghosts chase for good. The clock stops while the ghosts are blue, and starts again from zero when you lose a life.

A board that patches the old game

Ms. Pac-Man ran on a Pac-Man board with an extra circuit board added. The extra board brings three new program chips and keeps using the four Pac-Man ones. (According to the notes in MAME's driver, it plugs into the socket where the processor goes and carries the processor itself.) As the program runs, the extra board slips its own bytes in at forty small places in Pac-Man's code, eight bytes at a time. Most of those eight bytes are a jump into the new code.

So most of Ms. Pac-Man is still Pac-Man, running the same routines from the same addresses, and the changes can be found by comparing the two. Two of the forty patches change the ghosts.

The first patch: only one break

The scatter clock is a routine at address $0E36 in the program. Every frame it adds one to a counter, and when the counter reaches the end time of the current mood, it moves on to the next one. In Pac-Man the last step is:

srl  a            ; A = the current mood number (it was doubled earlier)
inc  a            ; + 1 = the next mood
ld   (phase),a    ; store it: 0, 1, 2, 3 ... 7

Even numbers are scatter and odd numbers are chase, so counting 0, 1, 2, 3 gives the back and forth. Ms. Pac-Man patches the first instruction:

xor  a            ; A = 0, whatever the mood was
inc  a            ; + 1 = 1
ld   (phase),a    ; store it: always 1, chase

The ghosts start in mood 0, scatter. When the first end time comes, they switch to 1, chase, and every later step lands on 1 again. There is no second scatter.

There is one leftover. About twenty seconds later the counter reaches the end time Pac-Man uses for its first chase, the routine "moves on" from chase to chase, and, because the mood changed as far as it knows, it turns all the ghosts around. In Ms. Pac-Man that turn has no other effect: they keep chasing.

The second patch: a random corner

In Pac-Man, when Blinky is in scatter the game picks his direction toward one fixed spot:

ld   de,$221D     ; Blinky's corner: above the top right of the maze
call $2966        ; choose the direction that leads toward DE

Ms. Pac-Man replaces the first line with a call to new code at $9561, which loads DE from a table of four corners instead:

ld   a,r          ; read the R register
and  $06          ; keep two bits: one of four table entries
rst  $10          ; look up that entry in this maze's corner table

The four corners are the same four tiles Pac-Man gives its four ghosts. So Blinky is sent to one of the four corners, chosen by two bits of something called R.

R is a counter inside the Z80 processor. Its real job is to keep the machine's memory chips refreshed. It goes up by one with every instruction the processor fetches and wraps around every 128, thousands of times a second. What it holds at any moment depends on exactly how many instructions have run, and that depends on everything that has happened since the machine was switched on, down to the timing of the coin and the start button. Two of its bits make a decent pair of dice.

The roll does not happen once. Blinky's direction is worked out again each time he reaches a new tile of the maze, about seven times a second, and each time the routine rolls again. So he is pulled toward one corner, then another, then back again, and ends up wandering. Pinky's routine got the same patch.

Inky and Clyde's routines call the same new code, but the very next instruction is Pac-Man's own, which loads their usual corner over the top of the random one. In the code, their scatter is unchanged.

The clock and the dice

The original program was run under test scripts that inserted a coin, pressed start, left the joystick alone, and recorded the scatter clock, the mood, the ghosts' positions, and every read of the corner tables.

What the code predictsWhat was recorded
Scatter at the start of level 1 lasts until the clock reaches 420 framesMood changed to chase at 420, about 7 seconds; all four ghosts turned around
No second scatterMood stayed at chase until the life ended
A new scatter after each deathClock and mood back to zero after each of three deaths, another 420 frames of scatter each time
One more turn-around at Pac-Man's next end timeClock pushed ahead to 1600 frames: at 1620 the ghosts turned around and kept chasing
Pac-Man does the full scheduleSame test on Pac-Man: moods 1 to 7 at 420, 1620, 2040, 3240, 3540, 4740 and 5040
Blinky and Pinky roll for a corner at each tile165 rolls by Blinky and 79 by Pinky over three scatters, all four corners picked
Shorter scatter later on420 frames on levels 1-4, 300 (about 5 seconds) on levels 5 to 23

Then the same game was started four times, pressing coin and start one frame later each time, a sixtieth of a second apart, and Blinky's position was recorded until the scatter ended:

Where Blinky goes in the first seven seconds Two outlines of the screen, 28 by 36 cells, with Blinky's path from the start of level 1 to the end of the first scatter phase. In Pac-Man four runs give one identical path into the top right. In Ms. Pac-Man four runs, started one frame apart, end in four different places. Small diamonds mark the four corner tiles just outside the maze. PAC-MAN MS. PAC-MAN 4 runs, 1 path 4 runs, 1 frame apart start after 420 frames corner targets shaded: the maze area; paths are Blinky's tile, measured in MAME
Blinky's path in the first seven seconds of level 1: one path in all four Pac-Man runs, four different paths in four Ms. Pac-Man runs started a frame apart.

In Pac-Man all four runs gave the same path, into the top right. In Ms. Pac-Man they ended in four different places. Shifting the start by one frame changes which instructions have run by the time of the first roll, so R holds a different number and Blinky goes somewhere else.

It is not truly random. Run the program twice with exactly the same inputs and the rolls come out exactly the same, 80 out of 80. But the inputs have to match to the frame, all the way from the coin drop.

What it changed

Pac-Man's ghosts follow rules that a player can learn: a schedule of breaks, and a fixed corner for each ghost. Ms. Pac-Man keeps the rules for chasing but cuts the breaks down to one, at the start of each life, and spends that one with two of the ghosts rolling dice at every step. Those first few seconds are the part of each life where the ghosts are least predictable.

Credits and further reading

  • The MAME project's Pac-Man driver, src/mame/pacman/pacman.cpp, for how the Ms. Pac-Man board decodes and patches the program, with notes by Steve Golson of General Computer Corporation on the board's design and history.
  • Retro Game Mechanics Explained, "Random Elements of Ms. Pac-Man" (video), as summarised by Set Side B in "What's So Random About Ms. Pac-Man": the same conclusion, that Ms. Pac-Man's ghosts scatter only at the start of a board and pick their corners at random. This article's analysis was done from the ROM first and compared afterwards.
  • The full technical notes behind this article, with addresses and measurements, are on the scatter systems page.