Game Development Reference
In-Depth Information
Ghost AI in Pac-Man
At first glance, it may seem like the AI for the arcade classic
Pac-Man
is very basic. The ghosts appear to either chase after the player or run
from the player, so one might think that it's just a binary state machine.
However, it turns out that the ghost AI is relatively complex.
Toru Iwatani, designer of
Pac-Man
, spoke with Susan Lammers on this
very topic in her 1986 topic
Programmers at Work
. He “wanted each
ghostlyenemytohaveaspecificcharacteranditsownparticularmove-
ments, so they weren't all just chasing after Pac-Man...which would
have been tiresome and flat.”
Each ofthe fourghosts have fourdifferent behaviors to define different
target points in relation to
Pac-Man
or the maze. The ghosts also al-
ternate between phases of attacking and dispersing, with the attack
phases increasing proportionally as the player progresses to further and
further levels.
An excellent write up on the implementation of the
Pac-Man
ghost AI
is available on the
Game Internals
blog at
http://tinyurl.com/238l7km
.
Basic State Machine Implementation
A state machine can be implemented in several ways. A minimum requirement is
that when the AI updates itself, the correct update action must be performed based
on the current state of the AI. Ideally, we would also like our state machine imple-
mentation to support enter and exit actions.
If the AI only has two states, we could simply use a Boolean check in the AI's
Update
function.Butthissolutionisnotveryrobust.Aslightlymoreflexibleim-
plementation, often used in simple games, is to have an enumeration (
enum
) that
9.9
would be the following:
enum
AIState
Patrol,
Death,
Attack
end