The Soul of Pac-Man

Pac-Man flipped. You control the ghosts, and Pac-Man is the one running the maze. Corner him before he eats every dot, but watch out: his soul jumps from body to body.

Python 3 Pygame Single file Keyboard AI opponent
Play on itch.io
Screenshot: a blue maze full of yellow dots, four colored ghosts, Pac-Man, a blast ring and the hint banner
A soul blast ring bursting from the maze, with the HUD along the bottom.

About this project

A role-reversal take on a classic, with a mechanic of its own.

You steer one of four ghosts around a 29×23 maze while the other three move on their own AI. Pac-Man plays himself: he hunts for dots, runs from ghosts that get close, and every ten steps his soul leaps into the ghost farthest from him, leaving his old body behind as a corpse. Reading where he'll go next, and where he'll jump, is the whole game.

Be the ghost

Switch between four ghosts on the fly and use their different AI personalities to trap Pac-Man.

Soul transfers

Pac-Man possesses a distant ghost every 10 steps. The blast can take down another ghost nearby.

Combos

Chain transfers within a few seconds to build a combo multiplier and a bigger score.

Goal

Two ways the game ends.

You win

A ghost lands on Pac-Man's tile (or Pac-Man steps onto a ghost). The screen shows you caught him!

Pac-Man wins

Every dot on the board is eaten. The screen shows pac-man wins!

Ghosts also eat dots as they walk over them, so your own moves count against the dot total too. Hunting efficiently matters.

Controls

Keyboard only.

KeyAction
1 – 4Take control of that ghost (the number is shown under each ghost)
Arrow keysTap to move your ghost one tile in that direction
RRestart
  • Your ghost moves one step per tap, and faster than the AI ghosts, so tap rather than hold.
  • Switching ghosts adds a short 0.3 second delay before the new ghost will move.
  • You can't walk through the other ghosts or through corpses.

Soul transfer

The mechanic that makes it more than a chase.

  1. Countdown. After every 10 steps Pac-Man takes, his soul jumps. The bar in the HUD fills up, and for the last 4 steps it turns red and Pac-Man flashes red as a warning.
  2. Possession. He jumps into the ghost that is farthest from him, but only if that ghost is more than 3 tiles away. He takes over its tile.
  3. Corpses. His old body stays behind as a corpse. If another ghost is within 5 tiles of the blast, the closest one is also knocked out and becomes a corpse.
  4. Revival. Each corpse counts down five Pac-Man moves, then comes back as a ghost on the same tile.
  5. Score. Each transfer adds a combo count multiplied by 100 points. The combo resets if about three seconds pass without another transfer.
If the ghost you were controlling gets possessed or knocked out, control returns to the first ghost in the list.

Ghost personalities

The three ghosts you aren't controlling each behave differently.

Ghost 1 · Aggressive

Takes the shortest path to Pac-Man about 90% of the time.

Ghost 2 · Patrol

Wanders along its current direction, but locks on and chases when Pac-Man comes within 5 tiles.

Ghost 3 · Ambush

Aims for the spot four tiles ahead of Pac-Man's current heading, to cut him off.

Ghost 4 · Random

Chases about 40% of the time and moves randomly the rest, which makes it hard to predict.

AI ghosts speed up slightly when they are all more than 10 tiles from Pac-Man.

Pac-Man's AI

He isn't just wandering.

  • Flee mode: when any ghost is within 8 tiles, he picks the move that puts the most maze distance between himself and all ghosts, with a bonus for grabbing a dot and a penalty for reversing.
  • Hunt mode: otherwise he heads for the nearest dot using breadth-first search, avoiding ghost tiles.
  • Never steps onto a ghost if he has any safe move.

Run it

You need Python 3 and Pygame.

pip install pygame
python3 main.py

The window is 812 × 724 pixels. You can also play it on itch.io.

Under the hood

Everything lives in one Python file.

  • Maze: a 29×23 layout of walls and open tiles. Helper functions like passable() and is_wall() keep movement checks simple.
  • Pathfinding: bfs_next() finds the next step toward a target, bfs_dist_map() measures maze distance from a ghost, and nearest_dot_pos() finds the closest dot.
  • AI: pacman_step() handles flee and hunt, and ai_ghost_step() picks the behavior from each ghost's personality.
  • Soul transfer: try_soul_transfer() picks the farthest ghost, spawns corpses, scores the combo and triggers the blast effect.
  • Game state: all state lives in one dict created by new_game(), so restarting is just building a new one.
  • Effects: blast rings, particles and screen shake make each transfer feel punchy.
  • Async loop: the main loop is async with await asyncio.sleep(0), which keeps it compatible with browser builds such as pygbag.