Lesson goal: Pac-Man (advanced ideas)

Previous: Pac-Man | Home | Next: Lunar Lander: Gravity & Retro-Thrust

The genius of Toru Iwatani's Pac-Man AI lies in the unique mathematical targeting algorithms assigned to each ghost. Rather than chasing blindly, the ghosts collaborate using distinct vector strategies:

1. Blinky (Red / "Shadow"): Direct Pursuit Vector

Blinky is relentless. In Chase Mode, his target coordinate is always Pac-Man's exact tile: $$\vec{T}_{\text{Blinky}} = (x_{\text{Pac}}, y_{\text{Pac}})$$

2. Pinky (Pink / "Speedy"): Ambush Offset Vector

Pinky does not target where Pac-Man is—she targets where Pac-Man is going! If Pac-Man is moving with direction vector $\vec{d} = (dx, dy)$, Pinky calculates her target $4$ tiles ahead: $$\vec{T}_{\text{Pinky}} = (x_{\text{Pac}} + 4 \cdot dx,\; y_{\text{Pac}} + 4 \cdot dy)$$ When Pac-Man changes direction, Pinky's target instantly pivots $4$ tiles in that new heading!

3. Inky (Cyan / "Bashful"): Vector Doubling Flank

Inky coordinates directly with Blinky! First, find the pivot tile $2$ tiles ahead of Pac-Man: $\vec{P} = (x_{\text{Pac}} + 2 \cdot dx, y_{\text{Pac}} + 2 \cdot dy)$. Then double the vector from Blinky through this pivot: $$\vec{T}_{\text{Inky}} = 2 \cdot \vec{P} - \vec{Blinky}$$ When Blinky is far away, Inky flanks wide; when Blinky draws near, Inky closes in as a pincer!

4. Clyde (Orange / "Pokey"): Proximity Threshold Switch

Clyde checks the Euclidean distance to Pac-Man: $D = \sqrt{(x_{\text{Clyde}} - x_{\text{Pac}})^2 + (y_{\text{Clyde}} - y_{\text{Pac}})^2}$.
  • If $D \ge 8\text{ tiles}$: Clyde targets Pac-Man directly (just like Blinky!).
  • If $D < 8\text{ tiles}$: Clyde cowers and retreats to his bottom-left corner $(1, 30)$!


Intersection Geometry & Distance Metric

At every intersection, a ghost looks one step ahead in every valid direction (excluding a $180^\circ$ reverse turn). It computes the Euclidean distance squared from each candidate tile $(x_c, y_c)$ to its target $(x_T, y_T)$: $$D^2 = (x_c - x_T)^2 + (y_c - y_T)^2$$ The ghost chooses whichever direction minimizes $D^2$. If two directions tie, arcade rules resolve the tie with strict priority: $$\text{UP} > \text{LEFT} > \text{DOWN} > \text{RIGHT}$$

Timed Mode Switches (Scatter vs. Chase)

Every level has a deterministic mode schedule:
  • $0.0\text{s} - 7.0\text{s}$ (Scatter): Ghosts ignore Pac-Man and head toward their home corners:
    • Blinky $\to$ Top-Right $(26, 0)$
    • Pinky $\to$ Top-Left $(1, 0)$
    • Inky $\to$ Bottom-Right $(26, 30)$
    • Clyde $\to$ Bottom-Left $(1, 30)$
  • $7.0\text{s} - 27.0\text{s}$ (Chase): Ghosts hunt Pac-Man using their pursuit, ambush, and pincer vectors.
  • Reversal Rule: The moment a mode switch occurs (e.g. at $T = 7.0\text{s}$), all ghosts are forced to reverse direction $180^\circ$!
pac_game()
left(5)

up(6)

right(10)
Move the mouse over a dotted box for more information.

  • Target Vector Visualization: Notice the 4 colored crosshairs and dotted lines on the canvas (Red = Blinky, Pink = Pinky, Cyan = Inky, Orange = Clyde). Watch how each ghost calculates its vector!
  • Exploiting Scatter Mode: Because all 4 ghosts spend the first $7.0\text{ seconds}$ running toward the corners, you have $7 \times 4 = 28\text{ tile-steps}$ of free movement to sweep the central maze before they turn to hunt you.
  • The $180^\circ$ Turnaround: At $T = 7.0\text{s}$, watch all 4 ghosts suddenly reverse their tracks as Chase mode activates.

Now you try. Try inserting wait(2) before turning up, and watch how Pinky's vector overshoots your position!

Type your code here:


See your results here: