• Updated 5 Dec, 14:20 UTC
  • Next run 6 Dec, 05:17 UTCNext 6 Dec 05:17
  • Last 7 days: 7 new models · 66 runs7 d: 7 models · 66 runs
Playable Breakout

Grok 4.7xAI

Network offOpen alone (new tab)

Run details

Model
x-ai/grok-4.7
Route
OpenRouter API
Served by
xAI
Effort
high (OpenRouter default)
Limits
60 min · 250,000 output tokens (model max)
Date
24 Oct 2026
Time
7 min 49 s
Size
39 kB
Tokens
1,320 in, 42,925 out (28,328 thinking)
List price
$0.26 at today's price ($2.00 in / $6.00 out per M)
Paid
$0.26

Raw: output · meta.json

Prompt

Make a Breakout game I can play right now with the keyboard or the mouse. Give it several levels, a score and some juice.

It must be one complete, self-contained HTML file: inline all CSS and JavaScript, and make no network requests of any kind (no CDNs, web fonts or remote images). Reply with the file in a single ```html code block.

Source

39 kB 1,354 lines Raw

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Breakout</title>
<style>
  * { box-sizing: border-box; }
  html, body {
    margin: 0;
    height: 100%;
    background: #05060e;
    overflow: hidden;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
  }
  canvas {
    display: block;
    width: 100vw;
    height: 100vh;
    cursor: none;
  }
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
(function () {
  "use strict";

  var canvas = document.getElementById("c");
  var ctx = canvas.getContext("2d");
  var LW = 960;
  var LH = 540;
  var dpr = 1;

  var PALETTE = ["#3ee0c5", "#3db7ff", "#7aa2ff", "#c084fc", "#ff6ad5", "#ff5c7a", "#ffb020", "#c6f25a"];

  var LEVELS = [
    {
      name: "Warmup",
      rows: [
        "1111111111111",
        "1111111111111",
        "1111111111111",
        "1111111111111"
      ]
    },
    {
      name: "Pyramid",
      rows: [
        "......1......",
        ".....111.....",
        "....11111....",
        "...1111111...",
        "..111111111..",
        ".11111111111.",
        "1111111111111"
      ]
    },
    {
      name: "Checkerboard",
      rows: [
        "1212121212121",
        "2121212121212",
        "1212121212121",
        "2121212121212",
        "1212121212121",
        "2121212121212"
      ]
    },
    {
      name: "Fortress",
      rows: [
        "3333333333333",
        "3...........3",
        "3.222222222.3",
        "3.2.......2.3",
        "3.222222222.3",
        "3...........3",
        "1111111111111"
      ]
    },
    {
      name: "Diamond",
      rows: [
        "......2......",
        ".....222.....",
        "....22222....",
        "...2222222...",
        "....22222....",
        ".....222.....",
        "......2......"
      ]
    },
    {
      name: "Columns",
      rows: [
        "3.111111111.3",
        "3.222222222.3",
        "3.111111111.3",
        "3.333333333.3",
        "3.111111111.3",
        "3.222222222.3",
        "3.111111111.3"
      ]
    },
    {
      name: "Crossfire",
      rows: [
        "3333333333333",
        "......2......",
        "1111112111111",
        "......2......",
        "2222222222222",
        "......2......",
        "1111112111111",
        "......3......"
      ]
    },
    {
      name: "Final Gauntlet",
      rows: [
        "3.3.3.3.3.3.3",
        "2222222222222",
        "3.111111111.3",
        "2.333333333.2",
        "3.111111111.3",
        "2222222222222",
        "3.3.3.3.3.3.3",
        "1111111111111"
      ]
    }
  ];

  var DROP_TYPES = [
    { id: "wide", label: "WIDE", color: "#4cc9ff" },
    { id: "multi", label: "MULTI", color: "#ff6ad5" },
    { id: "slow", label: "SLOW", color: "#9ef01a" },
    { id: "pierce", label: "PIERCE", color: "#ffb703" },
    { id: "sticky", label: "STICK", color: "#c084fc" },
    { id: "life", label: "LIFE", color: "#ff5d73" }
  ];

  var state = "menu";
  var pausedFrom = "play";
  var level = 1;
  var score = 0;
  var shownScore = 0;
  var hi = 0;
  var newRecord = false;
  var lives = 3;
  var combo = 0;
  var comboTimer = 0;
  var bricks = [];
  var brickTotal = 1;
  var balls = [];
  var drops = [];
  var particles = [];
  var rings = [];
  var popups = [];
  var stars = [];
  var keys = {};
  var mouseX = null;
  var pointerActive = false;
  var shake = 0;
  var flash = 0;
  var flashColor = "255,255,255";
  var announce = null;
  var introT = 0;
  var clearT = 0;
  var victoryT = 0;
  var fwTimer = 0;
  var menuT = 0;
  var muted = false;
  var actx = null;

  var wideT = 0, slowT = 0, pierceT = 0, stickyT = 0;

  var paddle = {
    x: LW / 2 - 58,
    y: LH - 46,
    w: 116,
    h: 14,
    baseW: 116,
    targetW: 116,
    flash: 0,
    squash: 1
  };

  try { hi = +localStorage.getItem("breakout-neon-hi") || 0; } catch (e) {}

  for (var i = 0; i < 90; i++) {
    stars.push({
      x: Math.random() * LW,
      y: Math.random() * LH,
      s: Math.random() * 1.7 + 0.3,
      v: 10 + Math.random() * 28,
      a: 0.15 + Math.random() * 0.55
    });
  }

  function view() {
    var scale = Math.min(window.innerWidth / LW, window.innerHeight / LH);
    var ox = (window.innerWidth - LW * scale) / 2;
    var oy = (window.innerHeight - LH * scale) / 2;
    return { scale: scale, ox: ox, oy: oy };
  }

  function layout() {
    dpr = Math.min(window.devicePixelRatio || 1, 2);
    canvas.width = Math.floor(window.innerWidth * dpr);
    canvas.height = Math.floor(window.innerHeight * dpr);
    canvas.style.width = window.innerWidth + "px";
    canvas.style.height = window.innerHeight + "px";
  }

  function toLogical(cx, cy) {
    var v = view();
    return { x: (cx - v.ox) / v.scale, y: (cy - v.oy) / v.scale };
  }

  function roundRect(x, y, w, h, r) {
    var rr = Math.max(0, Math.min(r, w / 2, h / 2));
    ctx.beginPath();
    ctx.moveTo(x + rr, y);
    ctx.arcTo(x + w, y, x + w, y + h, rr);
    ctx.arcTo(x + w, y + h, x, y + h, rr);
    ctx.arcTo(x, y + h, x, y, rr);
    ctx.arcTo(x, y, x + w, y, rr);
    ctx.closePath();
  }

  function unlockAudio() {
    var AC = window.AudioContext || window.webkitAudioContext;
    if (!AC) return;
    if (!actx) actx = new AC();
    if (actx.state === "suspended") actx.resume();
  }

  function tone(freq, dur, type, vol, slideTo) {
    if (!actx || muted || vol <= 0) return;
    var t0 = actx.currentTime;
    var o = actx.createOscillator();
    var g = actx.createGain();
    o.type = type;
    o.frequency.setValueAtTime(freq, t0);
    if (slideTo) o.frequency.exponentialRampToValueAtTime(Math.max(40, slideTo), t0 + dur);
    g.gain.setValueAtTime(vol, t0);
    g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
    o.connect(g);
    g.connect(actx.destination);
    o.start(t0);
    o.stop(t0 + dur + 0.02);
  }

  function addScore(n) {
    score += n;
    if (score > hi) {
      hi = score;
      newRecord = true;
      try { localStorage.setItem("breakout-neon-hi", String(hi)); } catch (e) {}
    }
  }

  function addShake(n) { shake = Math.min(18, shake + n); }

  function doFlash(a, rgb) {
    flash = a;
    flashColor = rgb || "255,255,255";
  }

  function say(text, color) {
    announce = { text: text, color: color || "#ffffff", life: 1.05, max: 1.05 };
  }

  function burst(x, y, color, n, speed) {
    if (particles.length > 380) particles.splice(0, 50);
    for (var i = 0; i < n; i++) {
      var a = Math.random() * Math.PI * 2;
      var s = speed * (0.25 + Math.random());
      particles.push({
        x: x, y: y,
        vx: Math.cos(a) * s,
        vy: Math.sin(a) * s - Math.random() * 30,
        life: 0.35 + Math.random() * 0.45,
        max: 0.8,
        color: color,
        r: 1.2 + Math.random() * 2.4
      });
    }
  }

  function ring(x, y, color) {
    rings.push({ x: x, y: y, life: 0.38, max: 0.38, color: color });
  }

  function popup(x, y, text, color) {
    popups.push({ x: x, y: y, text: text, life: 0.75, max: 0.75, color: color || "#fff" });
  }

  function currentSpeed() {
    return (348 + (level - 1) * 28) * (slowT > 0 ? 0.64 : 1);
  }

  function normSpeed(ball, target) {
    var sp = Math.hypot(ball.vx, ball.vy) || 1;
    ball.vx = ball.vx / sp * target;
    ball.vy = ball.vy / sp * target;
  }

  function loadLevel(index) {
    var spec = LEVELS[index];
    var rows = spec.rows;
    var cols = rows[0].length;
    var gap = 5;
    var side = 26;
    var top = 76;
    var bw = (LW - side * 2 - gap * (cols - 1)) / cols;
    var bh = 18;
    bricks = [];
    for (var r = 0; r < rows.length; r++) {
      for (var c = 0; c < cols; c++) {
        var ch = rows[r].charAt(c);
        if (ch === "." || ch === " " || ch === "") continue;
        var hp = parseInt(ch, 10);
        if (!hp) continue;
        bricks.push({
          x: side + c * (bw + gap),
          y: top + r * (bh + gap),
          w: bw,
          h: bh,
          hp: hp,
          max: hp,
          color: PALETTE[r % PALETTE.length],
          dead: false,
          dying: 0,
          flash: 0
        });
      }
    }
    brickTotal = Math.max(1, bricks.length);
  }

  function makeBall(x, y, stuck) {
    return {
      x: x, y: y, vx: 0, vy: 0, r: 8,
      stuck: stuck, stickOffset: 0,
      trail: [], hitSet: [], dead: false
    };
  }

  function spawnStuck() {
    balls = [makeBall(paddle.x + paddle.w / 2, paddle.y - 10, true)];
  }

  function clearPowers() {
    wideT = slowT = pierceT = stickyT = 0;
    paddle.targetW = paddle.baseW;
    paddle.w = paddle.baseW;
  }

  function startGame() {
    score = 0;
    shownScore = 0;
    newRecord = false;
    lives = 3;
    level = 1;
    combo = 0;
    comboTimer = 0;
    drops = [];
    popups = [];
    rings = [];
    particles = [];
    announce = null;
    clearPowers();
    paddle.x = LW / 2 - paddle.baseW / 2;
    loadLevel(0);
    spawnStuck();
    state = "intro";
    introT = 1.35;
    unlockAudio();
    tone(440, 0.08, "square", 0.04);
    tone(660, 0.12, "square", 0.035);
  }

  function nextLevel() {
    level += 1;
    if (level > LEVELS.length) {
      state = "victory";
      victoryT = 0;
      fwTimer = 0.2;
      say("ALL CLEAR", "#ffe08a");
      tone(523, 0.14, "square", 0.05);
      setTimeout(function () { tone(659, 0.14, "square", 0.05); }, 120);
      setTimeout(function () { tone(784, 0.2, "triangle", 0.06); }, 240);
      setTimeout(function () { tone(1046, 0.28, "square", 0.04); }, 380);
      return;
    }
    combo = 0;
    drops = [];
    clearPowers();
    loadLevel(level - 1);
    spawnStuck();
    state = "intro";
    introT = 1.25;
  }

  function loseLife() {
    lives -= 1;
    combo = 0;
    comboTimer = 0;
    drops = [];
    clearPowers();
    addShake(14);
    doFlash(0.55, "255,70,90");
    tone(220, 0.28, "sawtooth", 0.05, 70);
    if (lives <= 0) {
      state = "gameover";
      tone(160, 0.4, "triangle", 0.05, 50);
      return;
    }
    spawnStuck();
    state = "ready";
    say("BALL LOST", "#ff8aa0");
  }

  function beginClear() {
    state = "cleared";
    clearT = 1.75;
    addScore(800 * level);
    addShake(10);
    doFlash(0.4, "180,230,255");
    burst(LW / 2, LH / 2 - 40, "#ffffff", 36, 280);
    burst(LW / 2 - 120, LH / 2, "#7fd4ff", 18, 220);
    burst(LW / 2 + 120, LH / 2, "#ff6ad5", 18, 220);
    say("LEVEL CLEAR", "#ffffff");
    tone(523, 0.12, "square", 0.05);
    setTimeout(function () { tone(659, 0.12, "square", 0.045); }, 100);
    setTimeout(function () { tone(784, 0.2, "square", 0.045); }, 200);
  }

  function launchStuck() {
    if (state !== "play" && state !== "ready") return;
    var any = false;
    var sp = currentSpeed();
    for (var i = 0; i < balls.length; i++) {
      var ball = balls[i];
      if (!ball.stuck) continue;
      any = true;
      ball.stuck = false;
      var off = paddle.w > 0 ? ball.stickOffset / (paddle.w / 2) : 0;
      var ang = Math.max(-1, Math.min(1, off)) * 0.85;
      if (Math.abs(ang) < 0.12) ang = (Math.random() < 0.5 ? -1 : 1) * (0.18 + Math.random() * 0.22);
      ball.vx = Math.sin(ang) * sp;
      ball.vy = -Math.cos(ang) * sp;
    }
    if (any) {
      state = "play";
      tone(300, 0.06, "square", 0.04);
    }
  }

  function applyDrop(type) {
    addScore(250);
    tone(520, 0.08, "square", 0.045);
    tone(780, 0.12, "square", 0.035);
    burst(paddle.x + paddle.w / 2, paddle.y, type.color, 18, 160);
    if (type.id === "wide") {
      wideT = 12;
      paddle.targetW = paddle.baseW * 1.7;
      say("WIDE PADDLE", type.color);
    } else if (type.id === "multi") {
      spawnExtra();
      say("MULTI BALL", type.color);
    } else if (type.id === "slow") {
      slowT = 9;
      say("SLOW BALL", type.color);
    } else if (type.id === "pierce") {
      pierceT = 6.5;
      say("PIERCE", type.color);
    } else if (type.id === "sticky") {
      stickyT = 11;
      say("STICKY PADDLE", type.color);
    } else if (type.id === "life") {
      lives = Math.min(6, lives + 1);
      say("EXTRA LIFE", type.color);
      tone(880, 0.15, "triangle", 0.05);
    }
  }

  function spawnExtra() {
    if (balls.length >= 5) {
      addScore(200);
      return;
    }
    var src = null;
    for (var i = 0; i < balls.length; i++) if (!balls[i].stuck) src = balls[i];
    if (!src) src = balls[0];
    var sp = currentSpeed();
    var angles = [-0.7, 0.7, 0.15];
    for (var a = 0; a < angles.length; a++) {
      if (balls.length >= 5) break;
      var ang = angles[a];
      var b = makeBall(src.x, src.y, false);
      b.vx = Math.sin(ang) * sp;
      b.vy = -Math.abs(Math.cos(ang) * sp);
      balls.push(b);
    }
  }

  function spawnDrop(x, y, bias) {
    var type = DROP_TYPES[(Math.random() * DROP_TYPES.length) | 0];
    if (type.id === "life" && Math.random() < 0.55) type = DROP_TYPES[(Math.random() * 5) | 0];
    if (bias === 3 && Math.random() < 0.35) type = DROP_TYPES[3];
    drops.push({ x: x - 20, y: y, w: 42, h: 16, vy: 90, type: type });
  }

  function hitSetHas(ball, brick) {
    for (var i = 0; i < ball.hitSet.length; i++) if (ball.hitSet[i] === brick) return true;
    return false;
  }

  function hitSetAdd(ball, brick) { ball.hitSet.push(brick); }

  function hitSetPrune(ball, brick) {
    for (var i = ball.hitSet.length - 1; i >= 0; i--) {
      if (ball.hitSet[i] === brick) ball.hitSet.splice(i, 1);
    }
  }

  function overlapInfo(ball, b) {
    var cx = Math.max(b.x, Math.min(ball.x, b.x + b.w));
    var cy = Math.max(b.y, Math.min(ball.y, b.y + b.h));
    var dx = ball.x - cx;
    var dy = ball.y - cy;
    var d2 = dx * dx + dy * dy;
    if (d2 > ball.r * ball.r) return null;
    var dist = Math.sqrt(d2);
    if (dist < 0.001) {
      var dl = ball.x - b.x;
      var dr = b.x + b.w - ball.x;
      var dt = ball.y - b.y;
      var db = b.y + b.h - ball.y;
      var m = Math.min(dl, dr, dt, db);
      var nx = 0, ny = -1;
      if (m === dl) { nx = -1; ny = 0; }
      else if (m === dr) { nx = 1; ny = 0; }
      else if (m === dt) { nx = 0; ny = -1; }
      else { nx = 0; ny = 1; }
      return { nx: nx, ny: ny, pen: ball.r + 0.5 };
    }
    return { nx: dx / dist, ny: dy / dist, pen: ball.r - dist };
  }

  function damageBrick(b) {
    b.hp -= 1;
    b.flash = 1;
    var cx = b.x + b.w / 2;
    var cy = b.y + b.h / 2;
    if (b.hp <= 0) {
      b.dead = true;
      b.dying = 0.2;
      combo += 1;
      comboTimer = 1.15;
      var gain = 80 * b.max * combo;
      addScore(gain);
      popup(cx, cy - 6, "+" + gain, "#ffffff");
      burst(cx, cy, b.color, 14 + b.max * 3, 170 + combo * 6);
      ring(cx, cy, b.color);
      addShake(2.4 + Math.min(6, combo * 0.35));
      tone(190 + Math.min(combo, 14) * 32, 0.07, "square", 0.04);
      if (combo === 4 || combo === 8 || combo === 12 || combo === 16) {
        say(combo + " HIT COMBO", "#ffe08a");
        doFlash(0.2, "255,220,140");
      }
      var chance = 0.11 + b.max * 0.045;
      if (Math.random() < chance) spawnDrop(cx, cy, b.max);
    } else {
      addScore(15);
      burst(cx, cy, "#ffffff", 6, 90);
      tone(150 + b.hp * 40, 0.045, "triangle", 0.03);
      addShake(1.5);
    }
  }

  function collideBricks(ball) {
    var hitOne = false;
    for (var i = 0; i < bricks.length; i++) {
      var b = bricks[i];
      if (b.dead) continue;
      var info = overlapInfo(ball, b);
      if (!info) {
        hitSetPrune(ball, b);
        continue;
      }
      if (!hitSetHas(ball, b)) {
        hitSetAdd(ball, b);
        damageBrick(b);
      }
      if (pierceT <= 0 && !hitOne) {
        ball.x += info.nx * info.pen;
        ball.y += info.ny * info.pen;
        var dot = ball.vx * info.nx + ball.vy * info.ny;
        if (dot < 0) {
          ball.vx -= 2 * dot * info.nx;
          ball.vy -= 2 * dot * info.ny;
        }
        hitOne = true;
      }
    }
  }

  function walls(ball) {
    var top = 58 + ball.r;
    if (ball.x < ball.r) {
      ball.x = ball.r;
      ball.vx = Math.abs(ball.vx);
      tone(90, 0.03, "sine", 0.02);
    } else if (ball.x > LW - ball.r) {
      ball.x = LW - ball.r;
      ball.vx = -Math.abs(ball.vx);
      tone(90, 0.03, "sine", 0.02);
    }
    if (ball.y < top) {
      ball.y = top;
      ball.vy = Math.abs(ball.vy);
      tone(110, 0.03, "sine", 0.02);
    }
    if (state === "play" && ball.y > LH + ball.r + 10) ball.dead = true;
  }

  function paddleCollide(ball) {
    if (ball.stuck || ball.vy <= 0) return;
    if (ball.y + ball.r < paddle.y) return;
    if (ball.y > paddle.y + paddle.h + ball.r) return;
    if (ball.x < paddle.x - ball.r || ball.x > paddle.x + paddle.w + ball.r) return;
    if (stickyT > 0) {
      ball.stuck = true;
      ball.stickOffset = Math.max(-paddle.w / 2 + 8, Math.min(paddle.w / 2 - 8, ball.x - (paddle.x + paddle.w / 2)));
      ball.vx = 0;
      ball.vy = 0;
      ball.y = paddle.y - ball.r - 1;
      tone(210, 0.05, "sine", 0.04);
      burst(ball.x, paddle.y, "#c084fc", 8, 80);
      return;
    }
    var hit = (ball.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2);
    hit = Math.max(-1, Math.min(1, hit));
    var sp = currentSpeed();
    var ang = hit * 1.08;
    ball.vx = Math.sin(ang) * sp + (keys.arrowleft || keys.a ? -40 : 0) + (keys.arrowright || keys.d ? 40 : 0);
    ball.vy = -Math.abs(Math.cos(ang) * sp);
    normSpeed(ball, sp);
    ball.y = paddle.y - ball.r - 0.8;
    paddle.squash = 0.55;
    paddle.flash = 1;
    burst(ball.x, paddle.y, "#b9e6ff", 10, 130);
    addShake(2);
    tone(130 + (hit + 1) * 50, 0.05, "triangle", 0.05);
  }

  function updateBall(ball, dt, doBricks) {
    if (ball.stuck) {
      ball.x = paddle.x + paddle.w / 2 + ball.stickOffset;
      ball.y = paddle.y - ball.r - 1;
      ball.trail.push({ x: ball.x, y: ball.y });
      if (ball.trail.length > 12) ball.trail.shift();
      return;
    }
    var remain = dt;
    var guard = 0;
    while (remain > 0.00001 && !ball.dead && guard < 24) {
      guard++;
      var sp = Math.hypot(ball.vx, ball.vy) || 1;
      var step = Math.min(remain, 6 / sp);
      ball.x += ball.vx * step;
      ball.y += ball.vy * step;
      walls(ball);
      if (!ball.dead) paddleCollide(ball);
      if (!ball.dead && doBricks && !ball.stuck) collideBricks(ball);
      remain -= step;
    }
    var target = currentSpeed();
    var sp2 = Math.hypot(ball.vx, ball.vy) || 1;
    if (!ball.stuck && !ball.dead) {
      var ns = sp2 + (target - sp2) * Math.min(1, dt * 2.4);
      ball.vx = ball.vx / sp2 * ns;
      ball.vy = ball.vy / sp2 * ns;
      if (Math.abs(ball.vx) < target * 0.1) {
        ball.vx = (ball.vx < 0 ? -1 : 1) * target * 0.14;
        normSpeed(ball, target);
      }
      if (Math.abs(ball.vy) < target * 0.2) {
        ball.vy = (ball.vy < 0 ? -1 : 1) * target * 0.28;
        normSpeed(ball, target);
      }
    }
    ball.trail.push({ x: ball.x, y: ball.y });
    if (ball.trail.length > 14) ball.trail.shift();
  }

  function updatePaddle(dt) {
    var left = keys.arrowleft || keys.a;
    var right = keys.arrowright || keys.d;
    if (left || right) {
      paddle.x += ((right ? 1 : 0) - (left ? 1 : 0)) * 620 * dt;
    } else if (mouseX != null && pointerActive) {
      paddle.x = mouseX - paddle.w / 2;
    }
    paddle.x = Math.max(10, Math.min(LW - 10 - paddle.w, paddle.x));
    paddle.w += (paddle.targetW - paddle.w) * Math.min(1, dt * 7);
  }

  function updatePowers(dt) {
    if (wideT > 0) {
      wideT -= dt;
      if (wideT <= 0) paddle.targetW = paddle.baseW;
    }
    if (slowT > 0) slowT -= dt;
    if (pierceT > 0) pierceT -= dt;
    if (stickyT > 0) stickyT -= dt;
  }

  function updateDrops(dt) {
    for (var i = drops.length - 1; i >= 0; i--) {
      var d = drops[i];
      d.vy += 40 * dt;
      d.y += d.vy * dt;
      d.x += Math.sin(d.y * 0.05) * 12 * dt;
      if (d.y + d.h >= paddle.y && d.y <= paddle.y + paddle.h && d.x + d.w >= paddle.x && d.x <= paddle.x + paddle.w) {
        applyDrop(d.type);
        drops.splice(i, 1);
        continue;
      }
      if (d.y > LH + 30) drops.splice(i, 1);
    }
  }

  function bricksLeft() {
    var n = 0;
    for (var i = 0; i < bricks.length; i++) if (!bricks[i].dead) n++;
    return n;
  }

  function update(dt) {
    if (state !== "paused") {
      for (var s = 0; s < stars.length; s++) {
        stars[s].y += stars[s].v * dt;
        if (stars[s].y > LH) {
          stars[s].y = -2;
          stars[s].x = Math.random() * LW;
        }
      }
      for (var p = particles.length - 1; p >= 0; p--) {
        var pt = particles[p];
        pt.life -= dt;
        pt.x += pt.vx * dt;
        pt.y += pt.vy * dt;
        pt.vy += 280 * dt;
        if (pt.life <= 0) particles.splice(p, 1);
      }
      for (var r = rings.length - 1; r >= 0; r--) {
        rings[r].life -= dt;
        if (rings[r].life <= 0) rings.splice(r, 1);
      }
      for (var u = popups.length - 1; u >= 0; u--) {
        popups[u].life -= dt;
        popups[u].y -= 36 * dt;
        if (popups[u].life <= 0) popups.splice(u, 1);
      }
      for (var bi = 0; bi < bricks.length; bi++) {
        if (bricks[bi].dying > 0) bricks[bi].dying -= dt;
        if (bricks[bi].flash > 0) bricks[bi].flash -= dt * 4;
      }
      shownScore += (score - shownScore) * Math.min(1, dt * 10);
      if (announce) {
        announce.life -= dt;
        if (announce.life <= 0) announce = null;
      }
      flash = Math.max(0, flash - dt * 1.35);
      paddle.flash = Math.max(0, paddle.flash - dt * 3.2);
      paddle.squash += (1 - paddle.squash) * Math.min(1, dt * 12);
      shake *= Math.pow(0.035, dt);
      menuT += dt;
    }

    if (state === "menu") return;
    if (state === "paused") return;
    if (state === "gameover") return;
    if (state === "victory") {
      victoryT += dt;
      fwTimer -= dt;
      if (fwTimer <= 0) {
        fwTimer = 0.26;
        var fx = 70 + Math.random() * (LW - 140);
        var fy = 50 + Math.random() * 220;
        var cols = ["#ff6ad5", "#4cc9ff", "#ffb703", "#9ef01a", "#ffffff", "#c084fc"];
        burst(fx, fy, cols[(Math.random() * cols.length) | 0], 26, 240);
        tone(380 + Math.random() * 520, 0.09, "square", 0.025);
      }
      return;
    }

    updatePaddle(dt);

    if (state === "intro") {
      introT -= dt;
      for (var ib = 0; ib < balls.length; ib++) if (balls[ib].stuck) updateBall(balls[ib], dt, false);
      if (introT <= 0) state = "ready";
      return;
    }

    if (state === "ready") {
      for (var rb = 0; rb < balls.length; rb++) updateBall(balls[rb], dt, false);
      return;
    }

    if (state === "cleared") {
      clearT -= dt;
      for (var cb = 0; cb < balls.length; cb++) updateBall(balls[cb], dt, false);
      if (clearT <= 0) nextLevel();
      return;
    }

    updatePowers(dt);
    updateDrops(dt);
    if (comboTimer > 0) {
      comboTimer -= dt;
      if (comboTimer <= 0) combo = 0;
    }
    for (var i = 0; i < balls.length; i++) updateBall(balls[i], dt, true);
    var alive = [];
    for (var j = 0; j < balls.length; j++) if (!balls[j].dead) alive.push(balls[j]);
    balls = alive;
    if (bricksLeft() === 0) beginClear();
    else if (balls.length === 0) loseLife();
  }

  function drawHeart(x, y, s, fill) {
    ctx.beginPath();
    ctx.moveTo(x, y + s * 0.35);
    ctx.bezierCurveTo(x, y - s * 0.15, x - s * 0.55, y - s * 0.15, x - s * 0.55, y + s * 0.28);
    ctx.bezierCurveTo(x - s * 0.55, y + s * 0.62, x, y + s * 0.78, x, y + s);
    ctx.bezierCurveTo(x, y + s * 0.78, x + s * 0.55, y + s * 0.62, x + s * 0.55, y + s * 0.28);
    ctx.bezierCurveTo(x + s * 0.55, y - s * 0.15, x, y - s * 0.15, x, y + s * 0.35);
    ctx.closePath();
    ctx.fillStyle = fill;
    ctx.fill();
  }

  function drawBackground() {
    var g = ctx.createLinearGradient(0, 0, 0, LH);
    g.addColorStop(0, "#121833");
    g.addColorStop(0.55, "#0b1024");
    g.addColorStop(1, "#070912");
    ctx.fillStyle = g;
    ctx.fillRect(0, 0, LW, LH);
    for (var i = 0; i < stars.length; i++) {
      var s = stars[i];
      ctx.globalAlpha = s.a;
      ctx.fillStyle = "#d7e4ff";
      ctx.fillRect(s.x, s.y, s.s, s.s);
    }
    ctx.globalAlpha = 1;
    ctx.fillStyle = "rgba(0,0,0,0.13)";
    for (var y = 0; y < LH; y += 4) ctx.fillRect(0, y, LW, 1);
  }

  function drawBricks() {
    for (var i = 0; i < bricks.length; i++) {
      var b = bricks[i];
      if (b.dead && b.dying <= 0) continue;
      var grow = b.dead ? (1 - b.dying / 0.2) * 10 : 0;
      var alpha = b.dead ? Math.max(0, b.dying / 0.2) : 1;
      ctx.save();
      ctx.globalAlpha = alpha;
      var x = b.x - grow / 2;
      var y = b.y - grow / 2;
      var w = b.w + grow;
      var h = b.h + grow;
      ctx.fillStyle = "rgba(0,0,0,0.35)";
      roundRect(x + 2, y + 3, w, h, 4);
      ctx.fill();
      roundRect(x, y, w, h, 4);
      ctx.fillStyle = b.color;
      ctx.fill();
      ctx.save();
      ctx.clip();
      ctx.fillStyle = "rgba(255,255,255,0.32)";
      ctx.fillRect(x, y, w, h * 0.42);
      ctx.restore();
      if (!b.dead && b.hp < b.max) {
        ctx.fillStyle = "rgba(0,0,0," + (0.22 * (b.max - b.hp)) + ")";
        roundRect(x, y, w, h, 4);
        ctx.fill();
      }
      if (b.flash > 0) {
        ctx.fillStyle = "rgba(255,255,255," + (0.55 * Math.min(1, b.flash)) + ")";
        roundRect(x, y, w, h, 4);
        ctx.fill();
      }
      ctx.strokeStyle = "rgba(255,255,255,0.28)";
      ctx.lineWidth = 1;
      roundRect(x + 0.5, y + 0.5, w - 1, h - 1, 4);
      ctx.stroke();
      if (!b.dead && b.max > 1) {
        ctx.fillStyle = "rgba(0,0,0,0.45)";
        for (var p = 0; p < b.hp; p++) {
          ctx.beginPath();
          ctx.arc(x + 8 + p * 6, y + h - 5, 1.6, 0, Math.PI * 2);
          ctx.fill();
        }
      }
      ctx.restore();
    }
  }

  function drawPaddle() {
    ctx.save();
    ctx.translate(paddle.x + paddle.w / 2, paddle.y + paddle.h / 2);
    ctx.scale(1, paddle.squash);
    var pw = paddle.w;
    var ph = paddle.h;
    ctx.shadowColor = stickyT > 0 ? "#c084fc" : "#67d4ff";
    ctx.shadowBlur = 16;
    var g = ctx.createLinearGradient(-pw / 2, 0, pw / 2, 0);
    g.addColorStop(0, "#7af");
    g.addColorStop(0.5, "#e9fbff");
    g.addColorStop(1, "#4cf");
    roundRect(-pw / 2, -ph / 2, pw, ph, 7);
    ctx.fillStyle = g;
    ctx.fill();
    if (paddle.flash > 0) {
      ctx.fillStyle = "rgba(255,255,255," + (0.65 * paddle.flash) + ")";
      ctx.fill();
    }
    ctx.shadowBlur = 0;
    ctx.fillStyle = "rgba(255,255,255,0.55)";
    roundRect(-pw / 2 + 8, -ph / 2 + 2, pw - 16, 3, 2);
    ctx.fill();
    ctx.restore();
  }

  function drawBall(ball) {
    var col = pierceT > 0 ? "#ffb703" : "#9adfff";
    for (var i = 0; i < ball.trail.length; i++) {
      var t = ball.trail[i];
      var a = ((i + 1) / ball.trail.length) * 0.4;
      ctx.globalAlpha = a;
      ctx.fillStyle = col;
      ctx.beginPath();
      ctx.arc(t.x, t.y, ball.r * (0.35 + 0.65 * i / ball.trail.length), 0, Math.PI * 2);
      ctx.fill();
    }
    ctx.globalAlpha = 1;
    ctx.save();
    ctx.shadowColor = col;
    ctx.shadowBlur = 18;
    ctx.fillStyle = "#ffffff";
    ctx.beginPath();
    ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2);
    ctx.fill();
    ctx.shadowBlur = 0;
    ctx.fillStyle = "rgba(255,255,255,0.9)";
    ctx.beginPath();
    ctx.arc(ball.x - 2.2, ball.y - 2.4, 2.4, 0, Math.PI * 2);
    ctx.fill();
    ctx.restore();
    if (ball.stuck && (state === "ready" || state === "play" || state === "intro")) {
      var off = paddle.w ? ball.stickOffset / (paddle.w / 2) : 0;
      var ang = Math.max(-1, Math.min(1, off)) * 0.85;
      if (Math.abs(ang) < 0.01 && state !== "play") ang = 0;
      ctx.save();
      ctx.setLineDash([4, 7]);
      ctx.strokeStyle = "rgba(255,255,255,0.35)";
      ctx.lineWidth = 2;
      ctx.beginPath();
      ctx.moveTo(ball.x, ball.y);
      ctx.lineTo(ball.x + Math.sin(ang) * 78, ball.y - Math.cos(ang) * 78);
      ctx.stroke();
      ctx.setLineDash([]);
      ctx.restore();
    }
  }

  function drawDrops() {
    for (var i = 0; i < drops.length; i++) {
      var d = drops[i];
      ctx.save();
      ctx.shadowColor = d.type.color;
      ctx.shadowBlur = 12;
      roundRect(d.x, d.y, d.w, d.h, 8);
      ctx.fillStyle = d.type.color;
      ctx.fill();
      ctx.shadowBlur = 0;
      ctx.fillStyle = "#081018";
      ctx.font = "700 10px ui-monospace, Menlo, Consolas, monospace";
      ctx.textAlign = "center";
      ctx.textBaseline = "middle";
      ctx.fillText(d.type.label, d.x + d.w / 2, d.y + d.h / 2 + 0.5);
      ctx.restore();
    }
  }

  function drawFX() {
    for (var i = 0; i < rings.length; i++) {
      var rg = rings[i];
      var t = 1 - rg.life / rg.max;
      ctx.globalAlpha = 1 - t;
      ctx.strokeStyle = rg.color;
      ctx.lineWidth = 2;
      ctx.beginPath();
      ctx.arc(rg.x, rg.y, 6 + t * 48, 0, Math.PI * 2);
      ctx.stroke();
    }
    ctx.globalAlpha = 1;
    for (var p = 0; p < particles.length; p++) {
      var pt = particles[p];
      ctx.globalAlpha = Math.max(0, pt.life / pt.max);
      ctx.fillStyle = pt.color;
      ctx.beginPath();
      ctx.arc(pt.x, pt.y, pt.r, 0, Math.PI * 2);
      ctx.fill();
    }
    ctx.globalAlpha = 1;
    ctx.font = "700 14px ui-monospace, Menlo, Consolas, monospace";
    ctx.textAlign = "center";
    for (var u = 0; u < popups.length; u++) {
      var pop = popups[u];
      ctx.globalAlpha = Math.max(0, pop.life / pop.max);
      ctx.fillStyle = pop.color;
      ctx.fillText(pop.text, pop.x, pop.y);
    }
    ctx.globalAlpha = 1;
  }

  function drawHUD() {
    ctx.fillStyle = "rgba(4,6,16,0.55)";
    ctx.fillRect(0, 0, LW, 54);
    var cleared = brickTotal - bricksLeft();
    ctx.fillStyle = "rgba(255,255,255,0.1)";
    ctx.fillRect(0, 54, LW, 3);
    ctx.fillStyle = "#7fd4ff";
    ctx.fillRect(0, 54, LW * (cleared / brickTotal), 3);

    ctx.textAlign = "left";
    ctx.textBaseline = "alphabetic";
    ctx.fillStyle = "#8ea0c8";
    ctx.font = "700 11px system-ui, sans-serif";
    ctx.fillText("SCORE", 18, 20);
    ctx.fillStyle = "#f4f7ff";
    ctx.font = "700 20px ui-monospace, Menlo, Consolas, monospace";
    ctx.fillText(String(Math.round(shownScore)).padStart(6, "0"), 18, 42);

    ctx.textAlign = "center";
    ctx.fillStyle = "#8ea0c8";
    ctx.font = "700 11px system-ui, sans-serif";
    ctx.fillText("LEVEL " + level + " / " + LEVELS.length, LW / 2, 20);
    ctx.fillStyle = "#ffffff";
    ctx.font = "700 16px system-ui, sans-serif";
    ctx.fillText(state === "menu" ? "" : LEVELS[Math.min(LEVELS.length, level) - 1].name, LW / 2, 40);

    ctx.textAlign = "right";
    ctx.fillStyle = "#8ea0c8";
    ctx.font = "700 11px system-ui, sans-serif";
    ctx.fillText(muted ? "SFX OFF" : "SFX ON", LW - 18, 18);
    var hx = LW - 22;
    for (var h = 0; h < lives; h++) {
      drawHeart(hx - h * 18, 28, 8, h === 0 ? "#ff5d73" : "#ff8aa0");
    }

    var chips = [];
    if (wideT > 0) chips.push(["WIDE", wideT / 12, "#4cc9ff"]);
    if (slowT > 0) chips.push(["SLOW", slowT / 9, "#9ef01a"]);
    if (pierceT > 0) chips.push(["PIERCE", pierceT / 6.5, "#ffb703"]);
    if (stickyT > 0) chips.push(["STICK", stickyT / 11, "#c084fc"]);
    for (var c = 0; c < chips.length; c++) {
      var x = 16 + c * 78;
      var y = LH - 20;
      ctx.fillStyle = "rgba(0,0,0,0.4)";
      roundRect(x, y, 72, 11, 4);
      ctx.fill();
      ctx.fillStyle = chips[c][2];
      roundRect(x, y, 72 * Math.max(0, Math.min(1, chips[c][1])), 11, 4);
      ctx.fill();
      ctx.fillStyle = "#fff";
      ctx.font = "700 8px system-ui, sans-serif";
      ctx.textAlign = "left";
      ctx.fillText(chips[c][0], x + 5, y + 9);
    }

    if (combo >= 2 && state === "play") {
      ctx.textAlign = "center";
      ctx.font = "700 18px system-ui, sans-serif";
      ctx.fillStyle = "#ffe08a";
      ctx.globalAlpha = 0.75 + Math.sin(menuT * 10) * 0.25;
      ctx.fillText("COMBO x" + combo, LW / 2, 78);
      ctx.globalAlpha = 1;
    }

    if (announce && announce.life > 0) {
      var aa = Math.min(1, announce.life / 0.25);
      ctx.globalAlpha = Math.max(0, Math.min(1, aa));
      ctx.textAlign = "center";
      ctx.font = "700 28px system-ui, sans-serif";
      ctx.fillStyle = announce.color;
      ctx.fillText(announce.text, LW / 2, LH / 2 - 10);
      ctx.globalAlpha = 1;
    }
  }

  function drawLowWarning() {
    if (state !== "play") return;
    var low = false;
    for (var i = 0; i < balls.length; i++) {
      if (!balls[i].stuck && balls[i].y > LH * 0.78) low = true;
    }
    if (!low) return;
    var g = ctx.createLinearGradient(0, LH - 90, 0, LH);
    g.addColorStop(0, "rgba(255,50,70,0)");
    g.addColorStop(1, "rgba(255,50,70," + (0.12 + Math.sin(menuT * 8) * 0.05) + ")");
    ctx.fillStyle = g;
    ctx.fillRect(0, LH - 90, LW, 90);
  }

  function drawCard(title, lines, accent) {
    ctx.fillStyle = "rgba(4,6,16,0.58)";
    ctx.fillRect(0, 0, LW, LH);
    ctx.fillStyle = "rgba(12,16,38,0.94)";
    roundRect(LW / 2 - 230, LH / 2 - 138, 460, 276, 18);
    ctx.fill();
    ctx.strokeStyle = accent || "rgba(130,190,255,0.45)";
    ctx.lineWidth = 2;
    roundRect(LW / 2 - 230, LH / 2 - 138, 460, 276, 18);
    ctx.stroke();
    ctx.textAlign = "center";
    ctx.fillStyle = "#ffffff";
    ctx.font = "700 40px system-ui, sans-serif";
    ctx.fillText(title, LW / 2, LH / 2 - 62);
    ctx.font = "16px system-ui, sans-serif";
    ctx.fillStyle = "#c9d6f5";
    for (var i = 0; i < lines.length; i++) {
      ctx.fillText(lines[i], LW / 2, LH / 2 - 18 + i * 26);
    }
  }

  function draw() {
    var v = view();
    var sx = shake ? (Math.random() - 0.5) * shake * 2 : 0;
    var sy = shake ? (Math.random() - 0.5) * shake * 2 : 0;
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
    ctx.fillStyle = "#05060e";
    ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);

    ctx.save();
    ctx.translate(v.ox, v.oy);
    ctx.scale(v.scale, v.scale);
    ctx.translate(sx, sy);
    ctx.beginPath();
    ctx.rect(0, 0, LW, LH);
    ctx.clip();

    drawBackground();

    if (state === "menu") {
      ctx.textAlign = "center";
      var bob = Math.sin(menuT * 2) * 6;
      ctx.save();
      ctx.shadowColor = "#4cc9ff";
      ctx.shadowBlur = 24;
      ctx.fillStyle = "#f7fbff";
      ctx.font = "700 72px system-ui, sans-serif";
      ctx.fillText("BREAKOUT", LW / 2, 200 + bob);
      ctx.restore();
      ctx.fillStyle = "#9eb4de";
      ctx.font = "18px system-ui, sans-serif";
      ctx.fillText("Eight levels. One paddle. Do not blink.", LW / 2, 242 + bob);
      ctx.fillStyle = "#d5e4ff";
      ctx.font = "16px system-ui, sans-serif";
      var pulse = 0.55 + Math.sin(menuT * 4) * 0.45;
      ctx.globalAlpha = pulse;
      ctx.fillText("Click, tap, or press Space to play", LW / 2, 310);
      ctx.globalAlpha = 1;
      ctx.fillStyle = "#7e90b8";
      ctx.font = "14px system-ui, sans-serif";
      ctx.fillText("Mouse or A / D / arrows to move", LW / 2, 360);
      ctx.fillText("Space or click launches  ·  P pause  ·  M mute  ·  R restart", LW / 2, 384);
      ctx.fillStyle = "#ffe08a";
      ctx.font = "700 14px ui-monospace, Menlo, Consolas, monospace";
      ctx.fillText("BEST  " + String(hi).padStart(6, "0"), LW / 2, 430);
      ctx.strokeStyle = "rgba(140,180,255,0.2)";
      ctx.strokeRect(1, 1, LW - 2, LH - 2);
      ctx.restore();
      canvas.style.cursor = "pointer";
      return;
    }

    drawBricks();
    drawLowWarning();
    drawDrops();
    drawPaddle();
    for (var i = 0; i < balls.length; i++) drawBall(balls[i]);
    drawFX();
    if (flash > 0) {
      ctx.fillStyle = "rgba(" + flashColor + "," + (flash * 0.28) + ")";
      ctx.fillRect(0, 0, LW, LH);
    }
    drawHUD();

    if (state === "intro") {
      ctx.fillStyle = "rgba(6,10,24,0.5)";
      roundRect(LW / 2 - 190, 168, 380, 112, 14);
      ctx.fill();
      ctx.textAlign = "center";
      ctx.fillStyle = "#ffffff";
      ctx.font = "700 34px system-ui, sans-serif";
      ctx.fillText("LEVEL " + level, LW / 2, 214);
      ctx.fillStyle = PALETTE[(level - 1) % PALETTE.length];
      ctx.font = "18px system-ui, sans-serif";
      ctx.fillText(LEVELS[level - 1].name, LW / 2, 248);
    }

    if (state === "ready" || (state === "play" && balls.some(function (b) { return b.stuck; }))) {
      ctx.textAlign = "center";
      ctx.fillStyle = "rgba(255,255,255,0.8)";
      ctx.font = "14px system-ui, sans-serif";
      ctx.fillText(stickyT > 0 ? "Stuck — click or Space to launch" : "Click or Space to launch", LW / 2, LH - 70);
    }

    if (state === "paused") {
      drawCard("PAUSED", ["Click or Space to resume", "P also toggles pause"], "rgba(180,160,255,0.5)");
    } else if (state === "gameover") {
      var lines = [
        "Score  " + score,
        "Best  " + hi,
        newRecord ? "New record!" : "The bricks remain.",
        "Click or Space to play again"
      ];
      drawCard("GAME OVER", lines, "rgba(255,110,130,0.55)");
    } else if (state === "victory") {
      drawCard("YOU CLEARED IT", [
        "Final score  " + score,
        "Best  " + hi,
        newRecord ? "New record — gorgeous." : "A clean sweep.",
        "Click or Space to play again"
      ], "rgba(255,210,120,0.6)");
    }

    ctx.strokeStyle = "rgba(150,190,255,0.16)";
    ctx.lineWidth = 2;
    ctx.strokeRect(1, 1, LW - 2, LH - 2);
    ctx.restore();

    canvas.style.cursor = (state === "gameover" || state === "victory") ? "pointer" : "none";
  }

  function onAction() {
    unlockAudio();
    if (state === "menu" || state === "gameover" || state === "victory") {
      startGame();
      return;
    }
    if (state === "paused") {
      state = pausedFrom;
      return;
    }
    launchStuck();
  }

  function togglePause() {
    if (state === "paused") {
      state = pausedFrom;
      return;
    }
    if (state === "play" || state === "ready" || state === "intro") {
      pausedFrom = state;
      keys = {};
      shake = 0;
      state = "paused";
    }
  }

  window.addEventListener("keydown", function (e) {
    var k = e.key.toLowerCase();
    keys[k] = true;
    if (k === "arrowleft" || k === "arrowright" || k === "arrowup" || k === "arrowdown" || k === " ") e.preventDefault();
    if (e.repeat) return;
    if (k === "m") muted = !muted;
    if (k === "p") togglePause();
    if (k === "r" && state !== "menu") startGame();
    if (k === " " || k === "arrowup" || k === "enter") onAction();
  });

  window.addEventListener("keyup", function (e) {
    keys[e.key.toLowerCase()] = false;
  });

  window.addEventListener("blur", function () {
    keys = {};
    if (state === "play" || state === "ready" || state === "intro") {
      pausedFrom = state;
      state = "paused";
    }
  });

  window.addEventListener("mousemove", function (e) {
    var p = toLogical(e.clientX, e.clientY);
    mouseX = p.x;
    pointerActive = true;
  });

  canvas.addEventListener("mousedown", function (e) {
    var p = toLogical(e.clientX, e.clientY);
    mouseX = p.x;
    pointerActive = true;
    onAction();
  });

  canvas.addEventListener("touchstart", function (e) {
    e.preventDefault();
    var t = e.changedTouches[0];
    var p = toLogical(t.clientX, t.clientY);
    mouseX = p.x;
    pointerActive = true;
    onAction();
  }, { passive: false });

  canvas.addEventListener("touchmove", function (e) {
    e.preventDefault();
    var t = e.changedTouches[0];
    mouseX = toLogical(t.clientX, t.clientY).x;
    pointerActive = true;
  }, { passive: false });

  canvas.addEventListener("contextmenu", function (e) { e.preventDefault(); });
  window.addEventListener("resize", layout);

  layout();
  var last = performance.now();
  function frame(t) {
    var dt = Math.min(0.034, Math.max(0, (t - last) / 1000));
    last = t;
    update(dt);
    draw();
    requestAnimationFrame(frame);
  }
  requestAnimationFrame(frame);
})();
</script>
</body>
</html>

Compare

Other runs