webdevpuneet.com

Dot Muncher Maze Game: Four Ghosts, Fruit and Tunnels

Dot Muncher Maze Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

Copy & paste

Source Code

<div class="game-card">
  <div class="game-header">
    <div class="game-title">
      <span class="game-icon">🟡</span>
      <h2>Dot Muncher</h2>
    </div>
    <div class="stat-badges">
      <span class="badge">Lvl <b id="level">1</b></span>
      <span class="badge">Best <b id="best-score">0</b></span>
    </div>
  </div>

  <div class="canvas-wrap">
    <canvas id="game-canvas" width="304" height="336"></canvas>
    <div class="hud">
      <span id="hud-score">0</span>
      <span class="lives" id="hud-lives">🟡🟡🟡</span>
    </div>
    <div class="overlay" id="start-overlay">
      <div class="overlay-card">
        <p class="overlay-title">Dot Muncher</p>
        <p class="overlay-sub">Eat every dot. Four ghosts hunt you, each in its own way. Grab a power pellet to turn them blue and chomp them, and catch the fruit for bonus points.</p>
        <button class="btn btn-primary" id="btn-start">Start</button>
      </div>
    </div>
    <div class="overlay hidden" id="pause-overlay">
      <div class="overlay-card">
        <p class="overlay-title">Paused</p>
        <button class="btn btn-primary" id="btn-resume">Resume</button>
      </div>
    </div>
    <div class="overlay hidden" id="gameover-overlay">
      <div class="overlay-card">
        <p class="overlay-title">Game Over</p>
        <p class="overlay-sub" id="final-score">Score 0</p>
        <p class="overlay-best" id="best-msg"></p>
        <button class="btn btn-primary" id="btn-retry">Play again</button>
      </div>
    </div>
  </div>

  <div class="controls">
    <div class="dpad-cross">
      <button class="ctrl-btn up" id="btn-up" aria-label="Up">▲</button>
      <button class="ctrl-btn left" id="btn-left" aria-label="Left">◀</button>
      <button class="ctrl-btn right" id="btn-right" aria-label="Right">▶</button>
      <button class="ctrl-btn down" id="btn-down" aria-label="Down">▼</button>
    </div>
    <div class="side-box">
      <div class="side-btns">
        <button class="mini-btn" id="btn-pause" type="button" aria-label="Pause">⏸</button>
        <button class="mini-btn" id="btn-mute" type="button" aria-label="Sound on" aria-pressed="false">🔊</button>
      </div>
      <div class="tip-box">Power pellets turn the hunters into the hunted. Eat them in a row: 200, 400, 800, 1600.</div>
    </div>
  </div>

  <p class="hint-text">Arrow keys / WASD or swipe the maze to steer · P to pause</p>
</div>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's HTML, CSS and JS into an AI assistant like Claude and ask it to walk through the tile-and-progress movement model, the four ghost targeting functions, and how the breadth-first distance maps send ghosts out of and back into the house. It is a good base to extend: ask for a second maze that is chosen on higher levels, a two-player alternating mode, ghost-specific sounds, a "cruise" difficulty setting, smoother cornering for the player, a replay of your best run, or a port to a React component with the game state in a ref. Treat it as a working prototype to question and rebuild.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a complete Pac-Man-style maze game on an HTML5 canvas in plain HTML, CSS and JavaScript, with no libraries and no image files.

Requirements:
- A 19x21 tile maze defined as an array of strings (# wall, . dot, o power pellet, P start, H ghost house, - ghost door), with a wrap-around tunnel across the middle row. Draw the walls once into off-screen canvases (neon outlines) and flash them white on level clear.
- Tile-locked movement for the player and every ghost: store the tile, a direction and a 0 to 1 progress toward the next tile, and only decide where to go when a tile is reached. Buffer the wanted direction from the keyboard (arrows and WASD), an on-screen D-pad and swipe gestures; allow instant reversal in a corridor.
- Four ghosts that each choose, at every junction, the exit (never straight back) closest to a target tile: Blinky targets the player, Pinky targets four tiles ahead of the player, Inky doubles the vector from Blinky to a tile two ahead of the player, Clyde targets the player when far away and its corner when close. Alternate scatter and chase on a timer, and make ghosts turn around when the mode changes.
- A ghost house: three ghosts start inside and leave on a timer; use breadth-first distance maps from the exit tile and the house centre so leaving ghosts and eaten ghosts (shown as eyes, moving fast) find their way.
- Power pellets turn the ghosts blue and slow with random movement, flash near the end, and can be eaten for 200, 400, 800, 1600 in a row, with a short freeze and a floating score. Touching a normal ghost loses a life with a death animation.
- Fruit twice per level (worth more on higher levels), an extra life at 5000 points, levels that speed up the ghosts and shorten the frightened time, a READY / LEVEL banner, pause (P, a button and when the tab is hidden), Web Audio sound effects with a mute button, and the best score in localStorage (with try/catch).

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

What's included

Features

Tile-locked movement with progress between tiles, so nothing clips through walls at any frame rate
Buffered turns, instant reversal and a wrap-around tunnel
Four ghosts with four targeting rules (chase, ambush, flank, shy) and a scatter and chase timer
A ghost house: ghosts leave on a timer and eaten ghosts return as eyes along a breadth-first distance map
Frightened mode with a 200, 400, 800, 1600 chain, flashing blue ghosts and a freeze on each catch
Fruit that appears twice per level and grows more valuable, plus an extra life at 5000 points
Levels that speed up the ghosts, shorten frightened time and release them sooner; Blinky speeds up near the end
Neon walls drawn once into off-screen canvases, a wall flash on level clear and a death animation
Keyboard, swipe and D-pad controls, pause (P, button or tab switch) and a mute button
Synthesised sound effects with the Web Audio API and a best score in localStorage

About this UI Snippet

Dot Muncher: Tile-Locked Movement, Four Ghost Personalities and a Ghost House

Screenshot of the Dot Muncher Maze Game snippet rendered live

The maze-chase arcade formula (eat every dot while being hunted, with power pellets that swap the roles) packs a lot of game programming into a small board: movement locked to a grid that turns cleanly at junctions, several enemies that each choose their path differently, a global timer that switches them between attacking and retreating, and a ghost house they leave and return to. This snippet builds the whole thing on one HTML5 <canvas> with no libraries or images. It plays with the arrow keys or WASD, with a swipe on the maze, or with an on-screen D-pad, and it pauses when you switch tabs.

Tile-locked movement with progress between tiles

Every mover (the player and each ghost) is stored as a current tile, a direction and a prog value from 0 to 1 for how far it has travelled toward the next tile. Its pixel position is the tile centre plus dir * prog * TS. Each frame prog advances by speed * dt, and when it passes 1 the mover steps onto the next tile and decides where to go next. Because decisions only happen at tile centres, nothing can clip through a wall or miss a turn at a low frame rate. Input is buffered: the D-pad, keys and swipes only set a wanted direction, and the muncher takes it at the first tile where that way is open. Turning back the way you came is allowed instantly, in the middle of a corridor. The middle row is a tunnel: tile lookups wrap around, so a mover leaving one side comes out on the other, and ghosts slow down inside it.

Four ghosts, four ways of hunting

At every junction a ghost looks at the exits (never straight back) and picks the one that ends up closest to a target tile. What differs is the target. Blinky heads for you directly and speeds up when only a few dots remain. Pinky aims four tiles ahead of the way you are facing, to ambush you. Inky uses Blinky as a pivot: it takes the tile two ahead of you and doubles the vector from Blinky, so it flanks. Clyde chases when it is far away and retreats to its corner when it gets close. A timer alternates between *scatter* (each ghost heads for its own corner) and *chase*, and the ghosts turn around when it flips. The scatter periods shrink on higher levels.

The ghost house and frightened mode

Three ghosts start inside the house and leave one by one on a timer. Leaving and returning use a breadth-first distance map computed once from a tile outside the door and from the middle of the house, so the ghost just steps to the neighbouring tile with the smallest distance. Eating a power pellet turns every ghost blue and random-walking, slows it down and reverses it. Catching one sends its eyes racing back to the house along the distance map, where it revives and leaves again. Eaten in a row they score 200, 400, 800 and 1600, shown as floating numbers while the game freezes for a moment.

Levels, fruit and polish

Clearing the board flashes the walls and starts the next level: the ghosts get faster, the frightened time shrinks from 7 seconds down to 1, and they leave the house sooner. A fruit appears twice per level and is worth more on higher levels, and an extra life comes at 5000 points. The walls are drawn once into two off-screen canvases (blue and white for the flash), the player's death plays a mouth-opening animation, and sound effects are made with the Web Audio API, with a mute button. The best score is saved in localStorage.

Step by step

How to Use

  1. 1
    Start and steerPress Start, then steer with the arrow keys or WASD, the on-screen D-pad, or a swipe on the maze. Your wanted direction is remembered, so you can press a turn just before the corridor opens.
  2. 2
    Eat every dotDots are worth 10 points. Clear them all to finish the level. The two openings in the middle row are a tunnel that wraps to the other side.
  3. 3
    Learn the ghostsBlinky (red) chases you, Pinky (pink) ambushes ahead of you, Inky (cyan) flanks using Blinky, and Clyde (orange) retreats when you get close.
  4. 4
    Use the power pelletsThe four big pellets (50 points) turn the ghosts blue for a few seconds. Chase them: 200, 400, 800, then 1600 points in a row. Eaten ghosts run back to the house as eyes.
  5. 5
    Grab the fruitFruit appears under the ghost house twice per level and disappears after about nine seconds. It is worth 100 on level 1 and more later.
  6. 6
    Pause, retry and beat your bestPress P or the pause button to pause (the game also pauses when you leave the tab). You get 3 lives and an extra one at 5000 points; the best score is saved in your browser.

Real-world uses

Common Use Cases

Learning grid-based game AI
Four small targeting functions plus one shared "pick the exit closest to the target" rule give four very different enemies, which is a clear way to see how simple rules create personality.
A tile-and-progress movement template
Storing a tile, a direction and a 0 to 1 progress value works for any maze, roguelike or sliding-puzzle game that must turn only at junctions.
A swipe and D-pad arcade template
Buffered direction input from keys, a D-pad and swipes shows how to make a touch-friendly action game feel responsive.
An interactive 404 page or loading screen
The game is self-contained and needs no network, so it works as a playful distraction, like the Maze Runner Arrow-Key Game.
Neon canvas rendering
Drawing glowing wall outlines once into an off-screen canvas and blitting it each frame keeps a decorative maze cheap to render.
Related: Rock Paper Scissors vs Computer
See the Rock Paper Scissors vs Computer for a related games pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

They all use the same rule at a junction: look at the exits except straight back and take the one that ends up closest to a target tile. Only the target differs. Blinky targets you, Pinky targets four tiles ahead of your direction, Inky doubles the vector from Blinky to a tile two ahead of you, and Clyde targets you when far away and its own corner when close. During scatter all of them target their corners.

A breadth-first search is run once from the middle of the house (and another from the tile outside the door), giving every tile its walking distance. An eaten ghost simply moves to the neighbouring tile with the smallest number, so it can never get lost, and it revives when it reaches the house and leaves again by following the other map.

Rows whose first and last tiles are open are marked as tunnel rows. tileCh() wraps the column number for those rows, and movers use wrapC() when they step onto a new tile, so a mover that leaves one side arrives on the other. Ghosts are slowed inside the tunnel.

Edit the MAZE array: # is a wall, . a dot, o a power pellet, P the start, H the ghost house, - the door and a space a corridor with no dot. Keep the layout the same width on every row, put the door above the middle of the house, and keep the exit tile (EXIT) and house tile (HOUSE) constants pointing at the tile above the door and the middle of the house.

Each level raises the ghost speed (up to a cap), shortens the scatter periods, cuts the frightened time by a second (down to 1 second) and shortens the ghosts' release times. Blinky also speeds up when 20 and then 10 dots remain.

Yes. You can swipe anywhere on the maze, or use the D-pad. The direction is buffered, so a swipe made just before a junction still turns there, and the game pauses by itself when the tab is hidden.