cd /news/developer-tools/i-built-pac-man-in-150-lines-of-vani… · home topics developer-tools article
[ARTICLE · art-34673] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

I Built Pac-Man in ~150 Lines of Vanilla JavaScript

A developer built a fully playable Pac-Man clone in about 150 lines of vanilla JavaScript, using a text-based maze, grid movement, and simple greedy ghost AI. The entire game logic relies on array lookups rather than a physics engine, with collision detection reduced to a single character check. The project demonstrates that classic arcade mechanics can be implemented with minimal code.

read1 min views1 publishedJun 20, 2026

Pac-Man looks like it needs serious AI and a physics engine. It needs neither — a maze stored as text, grid movement, and ghosts that just step toward you. About 150 lines of vanilla JavaScript.

🟡 Play it: https://dev48v.infy.uk/game/day11-pacman.html

const MAZE = ["#####","#...#","#.#.#"];  // # = wall, . = pellet

Everything — movement, collision, pellets, ghost moves — is a lookup into this grid. The whole level is data you can edit by typing.

if (MAZE[pac.y+dy][pac.x+dx] !== "#") { pac.x += dx; pac.y += dy; }

That one check IS your collision system. No pixel math.

Store a wanted direction separately; switch to it when that way opens. That tiny buffer is the difference between responsive and stiff controls.

if (grid[y][x] === ".") { grid[y][x] = " "; score += 10; pellets--; }

When pellets

hits 0, the maze is cleared.

From a ghost's legal moves, pick the one closest to Pac-Man (plus a little randomness so you can escape). Greedy distance-minimising is surprisingly menacing.

A text maze + grid steps + greedy ghosts = the whole arcade icon. Play it — the "Understand" tab walks each step.

── more in #developer-tools 4 stories · sorted by recency
── more on @pac-man 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/i-built-pac-man-in-1…] indexed:0 read:1min 2026-06-20 ·