Skip to content
The Chat Archive
o3-2025-04-16· OpenAI LMArena Human Preference 140K

a pacman game, controllable using the arrow keys

  1. User

    a pacman game, controllable using the arrow keys

  2. Assistant

    Here’s a super‑small, self‑contained Pac‑Man prototype.
    Just put the snippet in an .html file, open it in a browser, and use the arrow keys.
    (It’s intentionally tiny: one Pac‑Man, a few pellets, no ghosts yet.)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>Tiny Pac‑Man</title>
    <style>
      canvas { background:#000; display:block; margin:0 auto; }
    </style>
    </head>
    <body>
    <canvas id="game" width="448" height="496"></canvas>
    
    <script>
    // ----------------------------------------------------
    // Basic constants
    // ----------------------------------------------------
    const TILE   = 16;                   // size of one grid square
    const ROWS   = 31;                   // 448×496 = 28×31 tiles
    const COLS   = 28;
    const SPEED  = 2;                    // pixels per frame
    const MOUTH  = Math.PI / 4;          // pac‑man mouth angle
    
    // ----------------------------------------------------
    // Map: 0 = empty, 1 = wall, 2 = pellet
    // (real Pac‑Man map simplified & shrunk)
    // ----------------------------------------------------
    const map = [
      "1111111111111111111111111111",
      "1222222222222112222222222221",
      "1211112111112112111112111121",
      "1222222222222222222222222221",
      "1211112111211111121112111121",
      "1222222112222112222122222221",
      "1111112111112111112111111111",
      "0000012110000000001121100000",
      "1111112111110001111112111111",
      "2222222222222222222222222222",
      "1111112111112111112111111111",
      "0000012110002110001121100000",
      "1111112111112111112111111111",
      "1222222222222222222222222221",
      "1211112111112111112111121121",
      "1222112222222222222212221121",
      "1112112111112111112111211111",
      "1222222222222112222222222221",
      "1111111111111111111111111111"
    ].map(r => r.split("").map(Number));
    
    // ----------------------------------------------------
    // Game state
    // ----------------------------------------------------
    let pac = { x: 14 * TILE + TILE / 2, y: 23 * TILE + TILE / 2,
                dx: 0, dy: 0, mouth: 0, open: true };
    
    // ----------------------------------------------------
    // Canvas setup
    // ----------------------------------------------------
    const cv   = document.getElementById("game");
    const ctx  = cv.getContext("2d");
    
    // ----------------------------------------------------
    // Handle keys
    // ----------------------------------------------------
    document.addEventListener("keydown", e => {
      if (e.key === "ArrowLeft")  pac.dx = -SPEED, pac.dy = 0;
      if (e.key === "ArrowRight") pac.dx =  SPEED, pac.dy = 0;
      if (e.key === "ArrowUp")    pac.dx = 0, pac.dy = -SPEED;
      if (e.key === "ArrowDown")  pac.dx = 0, pac.dy =  SPEED;
    });
    
    // ----------------------------------------------------
    // Helpers
    // ----------------------------------------------------
    function tileAt(px, py) {
      const col = Math.floor(px / TILE);
      const row = Math.floor(py / TILE);
      return map[row]?.[col] ?? 1;      // treat out‑of‑map as wall
    }
    
    function eatPellet(px, py) {
      const col = Math.floor(px / TILE);
      const row = Math.floor(py / TILE);
      if (map[row][col] === 2) map[row][col] = 0;
    }
    
    // ----------------------------------------------------
    // Main loop
    // ----------------------------------------------------
    function loop() {
      // --- move Pac‑Man ---
      const nextX = pac.x + pac.dx;
      const nextY = pac.y + pac.dy;
      if (!(tileAt(nextX, pac.y) === 1)) pac.x = nextX;
      if (!(tileAt(pac.x, nextY) === 1)) pac.y = nextY;
      eatPellet(pac.x, pac.y);
    
      // --- animate mouth ---
      pac.mouth += 0.15 * (pac.open ? 1 : -1);
      if (pac.mouth > MOUTH) pac.open = false;
      if (pac.mouth < 0)     pac.open = true;
    
      // --- render ---
      ctx.clearRect(0, 0, cv.width, cv.height);
    
      // draw pellets
      ctx.fillStyle = "#ffb8ae";
      for (let r = 0; r < map.length; r++) {
        for (let c = 0; c < map[r].length; c++) {
          if (map[r][c] === 2) {
            ctx.beginPath();
            ctx.arc(c * TILE + TILE / 2, r * TILE + TILE / 2, 2, 0, 2 * Math.PI);
            ctx.fill();
          }
        }
      }
    
      // draw walls
      ctx.fillStyle = "#0031ff";
      for (let r = 0; r < map.length; r++) {
        for (let c = 0; c < map[r].length; c++) {
          if (map[r][c] === 1) {
            ctx.fillRect(c * TILE, r * TILE, TILE, TILE);
          }
        }
      }
    
      // draw Pac‑Man
      ctx.fillStyle = "#ff0";
      ctx.beginPath();
      const angle  = pac.dx < 0 ? Math.PI
                  : pac.dx > 0 ? 0
                  : pac.dy < 0 ? -Math.PI / 2
                  : Math.PI / 2;
      ctx.moveTo(pac.x, pac.y);
      ctx.arc(pac.x, pac.y, TILE / 2,
              angle + pac.mouth, angle - pac.mouth, false);
      ctx.closePath();
      ctx.fill();
    
      requestAnimationFrame(loop);
    }
    loop();
    </script>
    </body>
    </html>
    

    What’s included
    • 28×31 grid, crude walls, and pellets
    • Arrow‑key movement with simple wall‑collision checks
    • Mouth animation to look like classic Pac‑Man

    Fun next steps to try

    1. Add ghosts with chase logic (BFS, A*, or random for starters).
    2. Implement tunnels that wrap Pac‑Man from one side to the other.
    3. Sound effects (AudioContext) for waka‑waka and pellet chimes.
    4. Score, extra lives, energizers, and ghost “frightened” mode.
    5. Replace the text‑based map with something editable in Tiled or JSON.

    Enjoy hacking!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.