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!
Controls
Keyboard only.
| Key | Action |
|---|---|
1 – 4 | Take control of that ghost (the number is shown under each ghost) |
| Arrow keys | Tap to move your ghost one tile in that direction |
R | Restart |
- 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.
- 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.
- 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.
- 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.
- Revival. Each corpse counts down five Pac-Man moves, then comes back as a ghost on the same tile.
- Score. Each transfer adds a combo count multiplied by 100 points. The combo resets if about three seconds pass without another transfer.
Ghost personalities
The three ghosts you aren't controlling each behave differently.
Takes the shortest path to Pac-Man about 90% of the time.
Wanders along its current direction, but locks on and chases when Pac-Man comes within 5 tiles.
Aims for the spot four tiles ahead of Pac-Man's current heading, to cut him off.
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()andis_wall()keep movement checks simple. - Pathfinding:
bfs_next()finds the next step toward a target,bfs_dist_map()measures maze distance from a ghost, andnearest_dot_pos()finds the closest dot. - AI:
pacman_step()handles flee and hunt, andai_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
asyncwithawait asyncio.sleep(0), which keeps it compatible with browser builds such as pygbag.