clive's profile picture

Published by


Category: Blogging

View Blog

View Profile

Report Blog Entry

The secrets behind the ghost behavior in pac-man

I was obsessed with Pac-Man a kid -- I'm old enough to have played it back when it was originally in arcades, and I even bought the book Mastering Pac-Man for the patterns. (I eventually got good enough at those patterns I rolled the machine over twice, which took I think about four hours? Enough that I was kind of a mess afterwards physically.)

Anyway, today I stumbled upon this insanely nuanced analysis of the algorithms that guide the ghosts. It's nuts -- I've never seen anyone lavish such talmudic attention on the algorithms of a retro game. Also, wow, the ghost algorithms are way more complex than I'd have thought!

Basically the ghosts have three modes -- either they're chasing you, they're "scattering", or they're in the "scared" mode where you can eat them. In any of those cases, they pick a target they're heading towards, and use that target to figure out what they should do when they get to an intersection: Turn or not?

It turns out the reason the red ghost is so aggressive is that when it's in chase mode, its target is you -- it's aiming at the precise spot where Pac-Man currently is.

The pink ghost has a slightly weirder method of chasing you. It doesn't aim at you directly; it aims four "tiles" away from you, like here ...

... and this is why it's possible to sometimes play a game of chicken with the pink ghost. As he writes ...

Since his target tile is set four tiles in front of Pac-Man, if Pac-Man heads directly towards him, Pinky's target tile will actually be behind himself once they are less than four tiles apart. This will cause Pinky to choose to take any available turn-off in order to loop back around to his target. Because of this, it is a common strategy to momentarily "fake" back towards Pinky if he starts following closely. This will often send him off in an entirely different direction.

A pic of that ...

That's wild. I knew that behavior existed -- I'd figured out, through trial and error, that you could do this with the pink ghost. But it's fascinating to now understand the ruleset that explains why it's possible.

The blue ghost's algorithm is even crazier. It aims at a spot that is combination of your position and the red ghost's position. This explains why the blue ghost's behavior can vary so dramatically. If the red ghost happens to be far away from you, then the blue ghost is triangulating some destination that is itself quite distant from you, and it looks ... sort of lost. But if the red ghost gets close to you, then the blue ghost's triangulated position becomes much, much closer to you itself, and its behavior begins, rather abruptly, to appear "smarter".

Dangerously smart, I might add. There's an interesting emergent aspect of this triangulated targeting. When the red ghost is close to you, the blue ghost is going to chasing a spot that is near you but not on you -- which means the blue ghost might wind up flanking you into a corner. Devious!

One rather poetic side note: The are periods in each level when the ghosts are in "scatter" mode, and they stop chasing you, giving you a temporary breather. But where, precisely, are they heading during "scatter" mode? They're aiming at spots outside the maze.

This is some straight-up pre-Copernical medieval cosmology, and I love it. The ghosts are, however briefly, aiming at a spot outside the shell of their universe, some point that exists in the aeth'rial realms they are forbidden to visit. Which could make one playfully wonder, pondering the interior psychology of retro-game characters -- are they aware that the spot at which they aim is offworld? Or do they just sense the pull of some distant star, its hungry gravity?

Heh. Anyway, the destinations in this "scatter" mode come into play when understanding the chase mode of the last ghost, the orange one. That's because it has a fascinating shelf that suddenly changes its chase pattern. If it's more than eight tiles away from you, it behaves the red ghost, heading straight for your position with the laser focus of a great white shark. 

But if it gets less than eight tiles from you? It suddenly changes its target to its outside-the-maze spot that it aims at during "scatter" mode. 

Thus ...

The combination of these two methods has the overall effect of Clyde alternating between coming directly towards Pac-Man, and then changing his mind and heading back to his corner whenever he gets too close. On the diagram above, the X marks on the path represent the points where Clyde's mode switches. If Pac-Man somehow managed to remain stationary in that position, Clyde would indefinitely loop around that T-shaped area.

You should go read the entire analysis -- there's a lot more depth to it, and I'm only scraping the surface here.

But I'm curious to see, if I played Pac-Man again now, whether any of this knowledge would help me play it in a fresh fashion. I mean, I used to play it merely relying on the patterns I'd learned, so I was like a goddamned robot on the Chrysler auto-manufacturing line, numbly executing my moves while paying no attention to anything else. But were I to actually play the game -- i.e. try to elude the ghosts dynamically, by observing their movements -- maybe this info would help me make better rapid mental-chess decisions.

Either way, I'd love to see more such analyses of the algorithmic rulesets behind other retro games. The invisibility of game mechanics creates an interesting culture around it: When we don't have direct info on the algorithms behind our NPC opponents, we sort of reverse-engineer them, intuiting the dark and invisible laws that they behave, set down by their creator. There's something clearly pleasant in that; it's a key part of what makes video games fun. But there's something enthralling, too, about having precise knowledge of those rulesets -- so one can sit back and play them out in the model of one's mind, using them like Newton's laws of physics to iterate through possible interactions of the characters in the game.

8 Kudos


Displaying 7 of 7 comments ( View all | Add Comment )

Aaron The Emissary

Aaron The Emissary's profile picture

Great post. I can still remember how exciting it was when my neighborhood friend got an Atari for Christmas and of course the first thing we played was hours and hours of PacMan- I never took it to the level of learning patterns- that sounds right out of Ready Player One! Meanwhile at home at that time we had a C64 with a tape drive that took bloody forever to load anything.

Report Comment


Anton's profile picture

Really nice write-up! Love reading about old video games and these older ways of designing elements in video games (and I'm not just saying that because I have to do that as a game design student).

Report Comment


clive's profile picture

@Michael Thank you sir!

Report Comment


Violation's profile picture


Report Comment


clive's profile picture

Shanen, glad you dug it!

Alex, heh, yes -- we build these models of the motivations of people, too, don't we? Skinner-box constructs! And thanks for the suggestions ... I'll go friend them now!

Report Comment


Shanen's profile picture

This was quite a captivating read, I've never had the opportunity to play PacMan on an arcade, and played it on a computer. The range looks simple enough, but never thought there would be so much going on bts

Report Comment


alex's profile picture

Hey Clive, really fun (and man does your skill in writing shine through :D) post, thanks for sharing! I barely played as a kid but evidently I wasn't able to get to the point where I could intuit anything, I'd quit completely ignorant of the forces working against me.

Got me thinking that we do a similar thing in our interactions with people: we end up building (and learning from external sources) an intuition into how we should interpret certain behaviours, and then we read a serious psychology book and go "a-ha, so that could be the reason". Of course the human brain works with far more complex algorithms but they seem to be there :D

P.S. I think you should friend Noah and Willis, I saw that they seem to be really into Super Smash Bros which has a similar depth to it (although that's played versus other people)

Report Comment