Asperia Games

Arcade

Pac-Man

The full 28×28 maze, 240 dots, four ghosts and a tunnel that wraps. Eat a power pellet and they run from you for ten seconds.

  • Arcade
  • Maze

Controls

Steer
WASD
Also steer

About this port

Ported from a React component in my tech blog, rewritten as a single dependency-free HTML file. Maze layout and character sprites came with the original.

The maze is one array of 784 numbers — 0 dot, 1 wall, 2 lair, 3 power pellet, 4 empty — and it ported over untouched. Everything else was rebuilt.

The ghosts didn’t work

The original picked each ghost’s next move like this:

const dir = [1, -1, width, -width];
const newPos = addAmount || dir[Math.floor(Math.random() * dir.length)];
// ...if blocked:
return getValidSquare(value);

Two problems. The recursion passes no direction, so a boxed-in ghost retries random directions forever — with four walls around it, that’s an unbounded recursion waiting to overflow the stack.

The bigger one is design, not correctness: a uniformly random walk almost never finds the exit from the lair. I watched a build for a full minute and all four ghosts were still milling about in their box. The maze had no threat in it at all — you could clear all 240 dots unopposed.

Ghosts now mostly move toward Pac-Man, with a bit of noise so they don’t march in lockstep, and the move list is enumerated rather than sampled so it always terminates. They leave the lair within a few seconds and the game has stakes.

The power pellets did nothing

setEnergize was called, the scared-ghost sprite was swapped in, and a ten-second timer cleared it. That was the entire feature — the ghosts stayed lethal the whole time. Eating one now sends it back to the lair for 200 points, and while you’re energised the ghosts invert their logic and run.

Smaller fixes

  • Ghosts weren’t reset on restart, so a second game started with them wherever the last one ended.
  • Turns are queued. The original changed direction on keypress and lost the input if that direction was a wall. A queued turn now takes effect at the first cell where it’s legal, which is what makes cornering feel right.

Arcade

More small games

All arcade games →