{"slug": "i-built-pac-man-in-150-lines-of-vanilla-javascript", "title": "I Built Pac-Man in ~150 Lines of Vanilla JavaScript", "summary": "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.", "body_md": "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.\n\n🟡 **Play it:** [https://dev48v.infy.uk/game/day11-pacman.html](https://dev48v.infy.uk/game/day11-pacman.html)\n\n``` js\nconst MAZE = [\"#####\",\"#...#\",\"#.#.#\"];  // # = wall, . = pellet\n```\n\nEverything — movement, collision, pellets, ghost moves — is a lookup into this grid. The whole level is data you can edit by typing.\n\n```\nif (MAZE[pac.y+dy][pac.x+dx] !== \"#\") { pac.x += dx; pac.y += dy; }\n```\n\nThat one check IS your collision system. No pixel math.\n\nStore a *wanted* direction separately; switch to it when that way opens. That tiny buffer is the difference between responsive and stiff controls.\n\n```\nif (grid[y][x] === \".\") { grid[y][x] = \" \"; score += 10; pellets--; }\n```\n\nWhen `pellets`\n\nhits 0, the maze is cleared.\n\nFrom 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.\n\nA text maze + grid steps + greedy ghosts = the whole arcade icon. [Play it](https://dev48v.infy.uk/game/day11-pacman.html) — the \"Understand\" tab walks each step.", "url": "https://wpnews.pro/news/i-built-pac-man-in-150-lines-of-vanilla-javascript", "canonical_source": "https://dev.to/dev48v/i-built-pac-man-in-150-lines-of-vanilla-javascript-39a0", "published_at": "2026-06-20 06:54:35+00:00", "updated_at": "2026-06-20 07:37:30.675976+00:00", "lang": "en", "topics": ["developer-tools", "artificial-intelligence"], "entities": ["Pac-Man"], "alternates": {"html": "https://wpnews.pro/news/i-built-pac-man-in-150-lines-of-vanilla-javascript", "markdown": "https://wpnews.pro/news/i-built-pac-man-in-150-lines-of-vanilla-javascript.md", "text": "https://wpnews.pro/news/i-built-pac-man-in-150-lines-of-vanilla-javascript.txt", "jsonld": "https://wpnews.pro/news/i-built-pac-man-in-150-lines-of-vanilla-javascript.jsonld"}}