Run details
- Model
- tencent/hy4-preview
- Route
- OpenRouter API
- Served by
- Tencent
- Effort
- high (OpenRouter default)
- Limits
- 60 min · 64,000 output tokens (model max)
- Date
- 24 Oct 2026
- Time
- 12 min 37 s
- Size
- 35 kB
- Tokens
- 97 in, 44,632 out (31,922 thinking)
- List price
- $0.11 at today's price ($0.834 in / $2.50 out per M)
- Paid
- $0.11
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
35 kB 1,245 lines Raw
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Neon Breakout</title>
<link rel="icon" href="data:," />
<style>
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
background:
radial-gradient(circle at 15% 10%, rgba(56,189,248,.18), transparent 40%),
radial-gradient(circle at 85% 20%, rgba(168,85,247,.18), transparent 42%),
radial-gradient(circle at 50% 100%, rgba(244,114,182,.14), transparent 45%),
#05070f;
color: #e5eefc;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
padding: 16px;
overflow: hidden;
}
.wrap {
width: min(1100px, 100%);
display: flex;
flex-direction: column;
align-items: center;
gap: 12px;
}
.hud {
width: min(960px, 100%);
display: flex;
gap: 10px;
justify-content: space-between;
flex-wrap: wrap;
}
.chip {
background: rgba(10, 16, 34, .72);
border: 1px solid rgba(125, 211, 252, .22);
border-radius: 999px;
padding: 8px 14px;
color: #9fb2d0;
font-size: 13px;
letter-spacing: .08em;
text-transform: uppercase;
box-shadow: 0 10px 30px rgba(0, 0, 0, .35), inset 0 0 18px rgba(125, 211, 252, .08);
backdrop-filter: blur(6px);
white-space: nowrap;
}
.chip b {
display: inline-block;
margin-left: 7px;
color: #fff;
font-size: 18px;
letter-spacing: 0;
text-transform: none;
text-shadow: 0 0 14px rgba(125, 211, 252, .65);
}
canvas {
display: block;
width: min(96vw, calc((100vh - 135px) * 1.5), 1100px);
height: auto;
aspect-ratio: 3 / 2;
border-radius: 22px;
background: #060814;
border: 1px solid rgba(125, 211, 252, .25);
box-shadow:
0 0 0 1px rgba(255, 255, 255, .03),
0 30px 80px rgba(0, 0, 0, .55),
0 0 60px rgba(56, 189, 248, .12);
touch-action: none;
cursor: none;
}
.hint {
margin: 0;
color: #8ea3c6;
font-size: 13px;
text-align: center;
}
@media (max-width: 700px) {
.chip { font-size: 11px; padding: 6px 10px; }
.chip b { font-size: 14px; }
.hint { font-size: 11px; }
}
</style>
</head>
<body>
<div class="wrap">
<div class="hud">
<div class="chip">Score <b id="scoreEl">0</b></div>
<div class="chip">Best <b id="bestEl">0</b></div>
<div class="chip">Lives <b id="livesEl">3</b></div>
<div class="chip">Level <b id="levelEl">1/6</b></div>
<div class="chip">Combo <b id="comboEl">—</b></div>
</div>
<canvas id="game" aria-label="Neon Breakout game"></canvas>
<p class="hint">
Move: mouse or ←/→ / A/D · Launch: click or Space · Pause: P · Restart: R · Mute: M
</p>
</div>
<script>
(() => {
"use strict";
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d", { alpha: false });
const W = 960, H = 640, TAU = Math.PI * 2;
const BASE_SPEED = 430, SPEED_STEP = 35, MAX_SPEED = 780;
const COLS = 11, MARGIN_X = 48, TOP_Y = 92;
const CELL_W = (W - MARGIN_X * 2) / COLS;
const BRICK_W = CELL_W - 10, BRICK_H = 24, ROW_H = 34;
function fitCanvas() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = Math.round(W * dpr);
canvas.height = Math.round(H * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
fitCanvas();
window.addEventListener("resize", fitCanvas);
const scoreEl = document.getElementById("scoreEl");
const bestEl = document.getElementById("bestEl");
const livesEl = document.getElementById("livesEl");
const levelEl = document.getElementById("levelEl");
const comboEl = document.getElementById("comboEl");
const clamp = (v, a, b) => v < a ? a : v > b ? b : v;
const rand = (a, b) => a + Math.random() * (b - a);
const pick = arr => arr[(Math.random() * arr.length) | 0];
const format = n => Math.floor(n).toLocaleString("en-US");
function roundRectPath(x, y, w, h, r) {
r = Math.min(r, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function drawRoundRect(x, y, w, h, r) {
roundRectPath(x, y, w, h, r);
}
function loadBest() {
try { return Number(localStorage.getItem("neon-breakout-best")) || 0; }
catch (e) { return 0; }
}
function saveBest() {
try { localStorage.setItem("neon-breakout-best", String(best)); }
catch (e) {}
}
// ---------- Audio ----------
let audioCtx = null, master = null, soundOn = true;
function initAudio() {
if (!audioCtx) {
try {
const AC = window.AudioContext || window.webkitAudioContext;
if (!AC) return;
audioCtx = new AC();
master = audioCtx.createGain();
master.gain.value = 0.22;
master.connect(audioCtx.destination);
} catch (e) {
audioCtx = null;
}
}
if (audioCtx && audioCtx.state === "suspended") {
const p = audioCtx.resume();
if (p && p.catch) p.catch(() => {});
}
}
function tone(opts) {
if (!audioCtx || !master || !soundOn) return;
const now = audioCtx.currentTime + (opts.delay || 0);
const dur = opts.dur || 0.1;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = opts.type || "square";
osc.frequency.setValueAtTime(Math.max(20, opts.freq || 440), now);
if (opts.to && opts.to !== opts.freq) {
osc.frequency.exponentialRampToValueAtTime(Math.max(20, opts.to), now + dur);
}
const vol = Math.max(0.0001, opts.vol == null ? 0.2 : opts.vol);
gain.gain.setValueAtTime(0.0001, now);
gain.gain.exponentialRampToValueAtTime(vol, now + Math.min(0.02, dur * 0.35));
gain.gain.exponentialRampToValueAtTime(0.0001, now + dur);
osc.connect(gain);
gain.connect(master);
osc.start(now);
osc.stop(now + dur + 0.03);
}
function noise(duration = 0.2, vol = 0.2) {
if (!audioCtx || !master || !soundOn) return;
const len = Math.max(1, Math.floor(audioCtx.sampleRate * duration));
const buffer = audioCtx.createBuffer(1, len, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < len; i++) {
data[i] = (Math.random() * 2 - 1) * (1 - i / len);
}
const src = audioCtx.createBufferSource();
const filter = audioCtx.createBiquadFilter();
const gain = audioCtx.createGain();
src.buffer = buffer;
filter.type = "bandpass";
filter.frequency.value = rand(700, 1800);
gain.gain.setValueAtTime(vol, audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.0001, audioCtx.currentTime + duration);
src.connect(filter);
filter.connect(gain);
gain.connect(master);
src.start();
}
const sfx = {
paddle() {
tone({ freq: 220, to: 460, dur: 0.08, type: "square", vol: 0.22 });
tone({ freq: 700, to: 240, dur: 0.08, type: "triangle", vol: 0.1, delay: 0.01 });
},
wall() {
tone({ freq: 430, to: 300, dur: 0.05, type: "triangle", vol: 0.1 });
},
hit() {
tone({ freq: 180 + Math.min(combo, 14) * 18, to: 440, dur: 0.06, type: "square", vol: 0.13 });
},
brick() {
tone({ freq: 420 + Math.min(combo, 15) * 28, to: 150, dur: 0.14, type: "sawtooth", vol: 0.18 });
noise(0.13, 0.11);
},
steel() {
tone({ freq: 900, to: 500, dur: 0.07, type: "square", vol: 0.11 });
noise(0.05, 0.05);
},
launch() {
tone({ freq: 180, to: 760, dur: 0.14, type: "sine", vol: 0.16 });
},
life() {
tone({ freq: 320, to: 70, dur: 0.35, type: "sawtooth", vol: 0.22 });
noise(0.25, 0.12);
},
clear() {
[523, 659, 784, 1046].forEach((f, i) => {
tone({ freq: f, to: f, dur: 0.14, type: "triangle", vol: 0.18, delay: i * 0.08 });
});
},
win() {
[523, 659, 784, 1046, 1318].forEach((f, i) => {
tone({ freq: f, to: f * 1.01, dur: 0.18, type: "triangle", vol: 0.18, delay: i * 0.1 });
});
}
};
// ---------- Game state ----------
const LEVELS = [
{
name: "Ignition",
rows: 6,
pattern: (row) => row < 2 ? 3 : row < 4 ? 2 : 1
},
{
name: "Checkerboard",
rows: 7,
pattern: (row, col) => {
if ((row + col) % 2 === 0) return 0;
return row < 2 ? 3 : row < 4 ? 2 : 1;
}
},
{
name: "Pyramid",
rows: 7,
pattern: (row, col) => {
const count = Math.min(9, 3 + row * 2);
const start = Math.floor((COLS - count) / 2);
if (col >= start && col < start + count) {
return row < 2 ? 3 : row < 4 ? 2 : 1;
}
if (row === 5 && (col === 0 || col === COLS - 1)) return { type: "steel" };
return 0;
}
},
{
name: "Fortress",
rows: 7,
pattern: (row, col) => {
if (row === 0) return 3;
if (col === 0 || col === COLS - 1) return 2;
if (row >= 2 && row <= 4 && col % 2 === 1) return 1;
return 0;
}
},
{
name: "Diagonal Rain",
rows: 7,
pattern: (row, col) => {
if ((col + row * 2) % 3 === 0) return 0;
return row < 3 ? 2 : 1;
}
},
{
name: "Core Meltdown",
rows: 8,
pattern: (row, col) => {
if (row >= 2 && row <= 5 && col >= 4 && col <= 6) {
return (row === 2 || row === 5) ? 4 : 3;
}
if (row === 0) return 2;
if (col === 0 || col === COLS - 1) return row === 7 ? { type: "steel" } : 2;
if (row === 7) return col % 2 === 0 ? 1 : 0;
if ((row + col) % 2 === 0) return 1;
return 0;
}
}
];
let state = "menu";
let pausedFrom = "ready";
let score = 0, best = 0, lives = 3, combo = 0, level = 1;
let levelName = LEVELS[0].name;
let bricks = [], breakableCount = 0;
let particles = [], floaters = [], trail = [];
let shake = 0, bgTime = 0;
const paddle = {
x: W / 2 - 66, y: H - 58, w: 132, h: 17,
targetX: W / 2, prevX: W / 2 - 66, vx: 0, squash: 0
};
const ball = {
x: W / 2, y: H - 80, vx: 0, vy: 0,
r: 8, speed: BASE_SPEED, stuck: true
};
const keys = { left: false, right: false };
let lastInput = "mouse";
// ---------- Effects ----------
function addParticles(x, y, count, color, opts = {}) {
const minSpeed = opts.minSpeed == null ? 40 : opts.minSpeed;
const maxSpeed = opts.maxSpeed == null ? 250 : opts.maxSpeed;
const spread = opts.spread == null ? Math.PI : opts.spread;
const baseAngle = opts.angle == null ? null : opts.angle;
const lifeMin = opts.lifeMin == null ? 0.25 : opts.lifeMin;
const lifeMax = opts.lifeMax == null ? 0.7 : opts.lifeMax;
const gravity = opts.gravity == null ? 380 : opts.gravity;
const drag = opts.drag == null ? 0.8 : opts.drag;
for (let i = 0; i < count; i++) {
const angle = baseAngle == null ? rand(0, TAU) : baseAngle + rand(-spread, spread);
const speed = rand(minSpeed, maxSpeed);
const life = rand(lifeMin, lifeMax);
particles.push({
x, y,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed,
life, maxLife: life,
size: rand(1.4, 4.6),
color: Array.isArray(color) ? pick(color) : color,
gravity, drag
});
}
if (particles.length > 1000) particles.splice(0, particles.length - 1000);
}
function addFloater(x, y, text, color, size = 24) {
floaters.push({ x, y, text, color, size, life: 0.9, maxLife: 0.9, vy: 52 });
if (floaters.length > 40) floaters.shift();
}
function updateBest() {
if (score > best) {
best = score;
saveBest();
}
}
// ---------- Levels ----------
function cellSpec(cell) {
if (!cell) return null;
if (typeof cell === "number") return { hp: cell, type: "normal" };
return { hp: cell.hp || 1, type: cell.type || "normal" };
}
function buildLevel(n) {
const def = LEVELS[(n - 1) % LEVELS.length];
const list = [];
for (let row = 0; row < def.rows; row++) {
for (let col = 0; col < COLS; col++) {
const spec = cellSpec(def.pattern(row, col));
if (!spec) continue;
const steel = spec.type === "steel";
list.push({
x: MARGIN_X + col * CELL_W + (CELL_W - BRICK_W) / 2,
y: TOP_Y + row * ROW_H,
w: BRICK_W,
h: BRICK_H,
hp: steel ? Infinity : clamp(spec.hp, 1, 4),
maxHp: steel ? 0 : clamp(spec.hp, 1, 4),
type: steel ? "steel" : "normal",
flash: 0
});
}
}
breakableCount = list.filter(b => b.type !== "steel").length;
return list;
}
function speedForLevel(n) {
return Math.min(MAX_SPEED, BASE_SPEED + (n - 1) * SPEED_STEP);
}
function resetPaddle() {
paddle.w = Math.max(96, 132 - (level - 1) * 6);
paddle.x = clamp(W / 2 - paddle.w / 2, 0, W - paddle.w);
paddle.targetX = paddle.x + paddle.w / 2;
paddle.prevX = paddle.x;
paddle.vx = 0;
paddle.squash = 0;
}
function resetBall() {
ball.stuck = true;
ball.speed = speedForLevel(level);
ball.vx = 0;
ball.vy = 0;
ball.x = paddle.x + paddle.w / 2;
ball.y = paddle.y - ball.r - 4;
trail.length = 0;
}
function newLevel(n) {
level = n;
levelName = LEVELS[(n - 1) % LEVELS.length].name;
bricks = buildLevel(n);
particles = [];
floaters = [];
combo = 0;
resetPaddle();
resetBall();
shake = 8;
state = "ready";
updateHUD();
}
function launchBall() {
if (!ball.stuck) return;
ball.stuck = false;
ball.speed = speedForLevel(level);
const angle = -Math.PI / 2 + rand(-0.35, 0.35);
ball.vx = Math.cos(angle) * ball.speed;
ball.vy = Math.sin(angle) * ball.speed;
addParticles(ball.x, ball.y, 20, "#67e8f9", {
angle: angle + Math.PI, spread: 0.8, minSpeed: 60, maxSpeed: 260
});
sfx.launch();
}
function startGame(launch = true) {
score = 0;
lives = 3;
newLevel(1);
if (launch) launchBall();
updateHUD();
}
function advanceLevel() {
if (level >= LEVELS.length) {
state = "win";
updateBest();
sfx.win();
updateHUD();
return;
}
newLevel(level + 1);
}
function clearLevel() {
state = "clear";
ball.vx = 0;
ball.vy = 0;
const bonus = 250 * level + lives * 100;
score += bonus;
updateBest();
addFloater(W / 2, H / 2 - 24, "Level Bonus +" + format(bonus), "#fde047", 36);
for (let i = 0; i < 140; i++) {
addParticles(rand(0, W), rand(70, 380), 1, ["#22d3ee", "#a78bfa", "#fb7185", "#fde047"], {
minSpeed: 20, maxSpeed: 380, lifeMin: 0.35, lifeMax: 1.15, gravity: 220
});
}
shake = 14;
sfx.clear();
updateHUD();
}
function loseLife() {
lives--;
combo = 0;
shake = Math.max(shake, 16);
addParticles(ball.x, H - 6, 48, ["#fb7185", "#fca5a5", "#fde047"], {
angle: -Math.PI / 2, spread: 1.35, minSpeed: 80, maxSpeed: 430,
lifeMin: 0.35, lifeMax: 0.95
});
sfx.life();
if (lives <= 0) {
state = "over";
ball.vx = 0;
ball.vy = 0;
ball.stuck = true;
updateBest();
} else {
resetBall();
state = "ready";
}
updateHUD();
}
function primaryAction() {
initAudio();
if (state === "menu" || state === "over" || state === "win") {
startGame(true);
return;
}
if (state === "paused") {
state = pausedFrom || "play";
return;
}
if (state === "ready") {
launchBall();
return;
}
if (state === "clear") {
advanceLevel();
return;
}
}
function togglePause() {
if (state === "play" || state === "ready") {
pausedFrom = state;
state = "paused";
} else if (state === "paused") {
state = pausedFrom || "play";
}
}
// ---------- Collision ----------
function circleRect(c, r) {
const cx = clamp(c.x, r.x, r.x + r.w);
const cy = clamp(c.y, r.y, r.y + r.h);
const dx = c.x - cx;
const dy = c.y - cy;
const d2 = dx * dx + dy * dy;
if (d2 > c.r * c.r) return null;
if (d2 > 0.000001) {
const d = Math.sqrt(d2);
return { nx: dx / d, ny: dy / d, pen: c.r - d };
}
const left = c.x - r.x;
const right = r.x + r.w - c.x;
const top = c.y - r.y;
const bottom = r.y + r.h - c.y;
let pen = left, nx = -1, ny = 0;
if (right < pen) { pen = right; nx = 1; ny = 0; }
if (top < pen) { pen = top; nx = 0; ny = -1; }
if (bottom < pen) { pen = bottom; nx = 0; ny = 1; }
return { nx, ny, pen };
}
function normalizeBall() {
const s = Math.hypot(ball.vx, ball.vy);
if (s > 0.0001) {
ball.vx = ball.vx / s * ball.speed;
ball.vy = ball.vy / s * ball.speed;
} else {
ball.vx = 0;
ball.vy = -ball.speed;
}
}
function enforceAngle() {
const min = ball.speed * 0.25;
if (Math.abs(ball.vy) < min) {
const sign = ball.vy === 0 ? (ball.y < H / 2 ? 1 : -1) : Math.sign(ball.vy);
ball.vy = sign * min;
const vxMag = Math.sqrt(Math.max(0, ball.speed * ball.speed - ball.vy * ball.vy));
ball.vx = (ball.vx >= 0 ? 1 : -1) * vxMag;
}
}
function reflectBall(nx, ny) {
const dot = ball.vx * nx + ball.vy * ny;
if (dot < 0) {
ball.vx -= 2 * dot * nx;
ball.vy -= 2 * dot * ny;
normalizeBall();
enforceAngle();
}
}
function collideWalls() {
if (ball.x - ball.r < 0) {
ball.x = ball.r;
ball.vx = Math.abs(ball.vx);
addParticles(2, ball.y, 8, "#7dd3fc", { angle: 0, spread: 0.9, minSpeed: 40, maxSpeed: 180 });
normalizeBall();
enforceAngle();
sfx.wall();
} else if (ball.x + ball.r > W) {
ball.x = W - ball.r;
ball.vx = -Math.abs(ball.vx);
addParticles(W - 2, ball.y, 8, "#7dd3fc", { angle: Math.PI, spread: 0.9, minSpeed: 40, maxSpeed: 180 });
normalizeBall();
enforceAngle();
sfx.wall();
}
if (ball.y - ball.r < 0) {
ball.y = ball.r;
ball.vy = Math.abs(ball.vy);
addParticles(ball.x, 2, 8, "#7dd3fc", { angle: Math.PI / 2, spread: 0.9, minSpeed: 40, maxSpeed: 180 });
normalizeBall();
enforceAngle();
sfx.wall();
}
if (ball.y - ball.r > H) {
loseLife();
}
}
function collidePaddle() {
const rect = { x: paddle.x, y: paddle.y, w: paddle.w, h: paddle.h };
const hit = circleRect(ball, rect);
if (!hit) return;
if (hit.ny < -0.25 && ball.vy > 0) {
ball.y = paddle.y - ball.r - 0.01;
ball.speed = Math.min(MAX_SPEED, ball.speed + 8 + level * 1.4);
const offset = clamp((ball.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2), -1, 1);
const angle = -Math.PI / 2 + offset * 1.05 + clamp(paddle.vx / 2400, -0.22, 0.22);
ball.vx = Math.cos(angle) * ball.speed;
ball.vy = Math.sin(angle) * ball.speed;
combo = 0;
paddle.squash = 1;
shake = Math.max(shake, 3);
addParticles(ball.x, paddle.y, 16, ["#67e8f9", "#e0f2fe", "#22d3ee"], {
angle: -Math.PI / 2, spread: 1.1, minSpeed: 70, maxSpeed: 330
});
sfx.paddle();
} else {
ball.x += hit.nx * (hit.pen + 0.01);
ball.y += hit.ny * (hit.pen + 0.01);
reflectBall(hit.nx, hit.ny);
paddle.squash = 0.65;
combo = 0;
addParticles(ball.x, ball.y, 10, "#67e8f9", { minSpeed: 50, maxSpeed: 220 });
sfx.paddle();
}
}
function brickColor(b) {
if (b.type === "steel") return "#94a3b8";
return ["#22d3ee", "#a78bfa", "#fb7185", "#f59e0b"][clamp(b.maxHp - 1, 0, 3)];
}
function hitBrick(b, i, hit) {
if (b.type === "steel") {
b.flash = 1;
addParticles(hit ? ball.x : b.x + b.w / 2, hit ? ball.y : b.y + b.h / 2, 10, ["#e2e8f0", "#94a3b8"], {
minSpeed: 50, maxSpeed: 240
});
shake = Math.max(shake, 2.5);
sfx.steel();
return;
}
b.hp--;
b.flash = 1;
const color = brickColor(b);
if (b.hp > 0) {
score += 5 * b.maxHp;
addParticles(ball.x, ball.y, 8, color, { minSpeed: 40, maxSpeed: 200, lifeMin: 0.18, lifeMax: 0.45 });
sfx.hit();
return;
}
bricks.splice(i, 1);
breakableCount--;
const gained = 50 * b.maxHp + combo * 10;
score += gained;
combo++;
addFloater(b.x + b.w / 2, b.y + b.h / 2, "+" + format(gained), color, 24);
addParticles(b.x + b.w / 2, b.y + b.h / 2, 28, [color, "#ffffff", "#e0f2fe"], {
minSpeed: 50, maxSpeed: 360, lifeMin: 0.25, lifeMax: 0.85
});
shake = Math.max(shake, 5);
sfx.brick();
if (breakableCount <= 0) clearLevel();
}
function collideBricks() {
let hits = 0;
for (let i = bricks.length - 1; i >= 0 && hits < 2; i--) {
const b = bricks[i];
if (
ball.x + ball.r < b.x || ball.x - ball.r > b.x + b.w ||
ball.y + ball.r < b.y || ball.y - ball.r > b.y + b.h
) continue;
const hit = circleRect(ball, b);
if (!hit) continue;
hits++;
const dotBefore = ball.vx * hit.nx + ball.vy * hit.ny;
ball.x += hit.nx * (hit.pen + 0.01);
ball.y += hit.ny * (hit.pen + 0.01);
if (dotBefore < 0) reflectBall(hit.nx, hit.ny);
hitBrick(b, i, hit);
if (state !== "play") return;
}
}
function updateBall(dt) {
const dist = Math.hypot(ball.vx, ball.vy) * dt;
const steps = clamp(Math.ceil(dist / (ball.r * 0.7)), 1, 10);
const sdt = dt / steps;
for (let i = 0; i < steps; i++) {
ball.x += ball.vx * sdt;
ball.y += ball.vy * sdt;
collideWalls();
if (state !== "play") return;
collidePaddle();
if (state !== "play") return;
collideBricks();
if (state !== "play") return;
}
trail.push({ x: ball.x, y: ball.y });
if (trail.length > 18) trail.shift();
}
function updatePaddle(dt) {
paddle.prevX = paddle.x;
let movedByKey = false;
const kbSpeed = 780 + level * 8;
if (keys.left) {
paddle.x -= kbSpeed * dt;
movedByKey = true;
lastInput = "keyboard";
}
if (keys.right) {
paddle.x += kbSpeed * dt;
movedByKey = true;
lastInput = "keyboard";
}
if (movedByKey) {
paddle.x = clamp(paddle.x, 0, W - paddle.w);
paddle.targetX = paddle.x + paddle.w / 2;
} else if (lastInput === "mouse") {
const desired = paddle.targetX - paddle.w / 2;
paddle.x += (desired - paddle.x) * Math.min(1, dt * 24);
paddle.x = clamp(paddle.x, 0, W - paddle.w);
}
paddle.vx = (paddle.x - paddle.prevX) / Math.max(dt, 0.0001);
paddle.squash = Math.max(0, paddle.squash - dt * 5);
if (ball.stuck) {
ball.x = paddle.x + paddle.w / 2;
ball.y = paddle.y - ball.r - 4;
trail.length = 0;
}
}
function updateEffects(dt) {
shake = Math.max(0, shake - dt * 34);
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.life -= dt;
if (p.life <= 0) {
particles.splice(i, 1);
continue;
}
p.vy += p.gravity * dt;
p.vx *= Math.max(0, 1 - p.drag * dt);
p.vy *= Math.max(0, 1 - p.drag * dt * 0.55);
p.x += p.vx * dt;
p.y += p.vy * dt;
}
for (let i = floaters.length - 1; i >= 0; i--) {
const f = floaters[i];
f.life -= dt;
f.y -= f.vy * dt;
if (f.life <= 0) floaters.splice(i, 1);
}
for (const b of bricks) {
if (b.flash > 0) b.flash = Math.max(0, b.flash - dt * 4.5);
}
}
function updateHUD() {
scoreEl.textContent = format(score);
bestEl.textContent = format(Math.max(best, score));
livesEl.textContent = "●".repeat(Math.max(0, lives)) || "0";
levelEl.textContent = `${level}/${LEVELS.length} · ${levelName}`;
comboEl.textContent = combo > 0 ? "x" + combo : "—";
}
function update(dt) {
bgTime += dt;
if (state !== "paused") {
updatePaddle(dt);
updateEffects(dt);
if (state === "play") updateBall(dt);
}
updateHUD();
}
// ---------- Render ----------
function drawBackground() {
const hue = (205 + level * 34 + Math.sin(bgTime * 0.25) * 18 + 360) % 360;
const g = ctx.createLinearGradient(0, 0, 0, H);
g.addColorStop(0, `hsl(${hue}, 68%, 9%)`);
g.addColorStop(0.55, `hsl(${(hue + 42) % 360}, 58%, 7%)`);
g.addColorStop(1, "hsl(232, 62%, 4%)");
ctx.fillStyle = g;
ctx.fillRect(-60, -60, W + 120, H + 120);
ctx.save();
ctx.strokeStyle = "rgba(255,255,255,0.045)";
ctx.lineWidth = 1;
ctx.beginPath();
const off = (bgTime * 28) % 48;
for (let x = -60 + off; x < W + 60; x += 48) {
ctx.moveTo(x, -20);
ctx.lineTo(x, H + 20);
}
for (let y = -60 + off; y < H + 60; y += 48) {
ctx.moveTo(-20, y);
ctx.lineTo(W + 20, y);
}
ctx.stroke();
ctx.globalCompositeOperation = "lighter";
for (let i = 0; i < 3; i++) {
const x = W / 2 + Math.cos(bgTime * 0.25 + i * 2.1) * 300;
const y = H / 2 + Math.sin(bgTime * 0.31 + i * 1.7) * 170;
const rg = ctx.createRadialGradient(x, y, 0, x, y, 260);
rg.addColorStop(0, `hsla(${(hue + i * 55) % 360}, 95%, 58%, 0.10)`);
rg.addColorStop(1, "hsla(0,0%,0%,0)");
ctx.fillStyle = rg;
ctx.fillRect(x - 260, y - 260, 520, 520);
}
ctx.restore();
const vig = ctx.createRadialGradient(W / 2, H / 2, H * 0.32, W / 2, H / 2, H * 0.86);
vig.addColorStop(0, "rgba(0,0,0,0)");
vig.addColorStop(1, "rgba(0,0,0,0.42)");
ctx.fillStyle = vig;
ctx.fillRect(0, 0, W, H);
}
function drawWalls() {
ctx.save();
ctx.strokeStyle = "rgba(125,211,252,0.45)";
ctx.shadowColor = "rgba(56,189,248,0.75)";
ctx.shadowBlur = 18;
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(2, 0);
ctx.lineTo(2, H);
ctx.moveTo(W - 2, 0);
ctx.lineTo(W - 2, H);
ctx.moveTo(0, 2);
ctx.lineTo(W, 2);
ctx.stroke();
ctx.restore();
}
function drawBricks() {
ctx.save();
ctx.lineWidth = 2;
for (const b of bricks) {
ctx.save();
const color = brickColor(b);
const alive = b.type === "steel" ? 1 : b.hp / b.maxHp;
ctx.globalAlpha = 0.55 + 0.45 * alive;
ctx.shadowColor = color;
ctx.shadowBlur = b.flash > 0.02 ? 28 : 12;
drawRoundRect(b.x, b.y, b.w, b.h, 7);
if (b.type === "steel") {
const sg = ctx.createLinearGradient(b.x, b.y, b.x + b.w, b.y + b.h);
sg.addColorStop(0, "#e2e8f0");
sg.addColorStop(0.5, "#94a3b8");
sg.addColorStop(1, "#64748b");
ctx.fillStyle = sg;
} else {
ctx.fillStyle = color;
}
ctx.fill();
ctx.shadowBlur = 0;
const gloss = ctx.createLinearGradient(b.x, b.y, b.x, b.y + b.h);
gloss.addColorStop(0, "rgba(255,255,255,0.30)");
gloss.addColorStop(0.45, "rgba(255,255,255,0)");
gloss.addColorStop(1, "rgba(2,6,23,0.45)");
ctx.fillStyle = gloss;
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.32)";
ctx.stroke();
if (b.type === "steel") {
ctx.strokeStyle = "rgba(15,23,42,0.35)";
ctx.beginPath();
ctx.moveTo(b.x + 6, b.y + b.h - 6);
ctx.lineTo(b.x + b.w - 6, b.y + 6);
ctx.moveTo(b.x + 6, b.y + b.h * 0.55);
ctx.lineTo(b.x + b.w * 0.45, b.y + 6);
ctx.stroke();
} else if (b.maxHp > 1 && b.hp < b.maxHp) {
ctx.globalAlpha = 0.75;
ctx.strokeStyle = "rgba(15,23,42,0.55)";
ctx.beginPath();
ctx.moveTo(b.x + 8, b.y + b.h * 0.36);
ctx.lineTo(b.x + b.w * 0.46, b.y + b.h * 0.68);
ctx.lineTo(b.x + b.w - 9, b.y + b.h * 0.31);
ctx.stroke();
}
if (b.flash > 0) {
ctx.globalAlpha = b.flash * 0.65;
ctx.fillStyle = "#ffffff";
ctx.fill();
}
ctx.restore();
}
ctx.restore();
}
function drawParticles() {
ctx.save();
ctx.globalCompositeOperation = "lighter";
for (const p of particles) {
const a = clamp(p.life / p.maxLife, 0, 1);
ctx.globalAlpha = a;
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc(p.x, p.y, p.size * (0.28 + a), 0, TAU);
ctx.fill();
}
ctx.restore();
}
function drawPaddle() {
const squash = paddle.squash;
const w = paddle.w * (1 + 0.13 * squash);
const h = paddle.h * (1 - 0.38 * squash);
const x = paddle.x + paddle.w / 2 - w / 2;
const y = paddle.y + (paddle.h - h) / 2;
ctx.save();
ctx.shadowColor = "rgba(34,211,238,0.9)";
ctx.shadowBlur = 26;
const g = ctx.createLinearGradient(x, y, x, y + h);
g.addColorStop(0, "#a5f3fc");
g.addColorStop(0.5, "#22d3ee");
g.addColorStop(1, "#0e7490");
drawRoundRect(x, y, w, h, h / 2);
ctx.fillStyle = g;
ctx.fill();
ctx.shadowBlur = 0;
ctx.strokeStyle = "rgba(255,255,255,0.65)";
ctx.lineWidth = 1.5;
ctx.stroke();
ctx.fillStyle = "rgba(255,255,255,0.75)";
drawRoundRect(x + 10, y + h * 0.28, w - 20, 3, 2);
ctx.fill();
ctx.restore();
}
function drawBall() {
if (state === "over") return;
ctx.save();
ctx.globalCompositeOperation = "lighter";
for (let i = 0; i < trail.length; i++) {
const t = trail[i];
const a = (i / trail.length) * 0.24;
ctx.globalAlpha = a;
ctx.fillStyle = "#67e8f9";
ctx.beginPath();
ctx.arc(t.x, t.y, ball.r * (0.25 + i / trail.length * 0.85), 0, TAU);
ctx.fill();
}
ctx.restore();
ctx.save();
ctx.shadowColor = "rgba(125,211,252,0.95)";
ctx.shadowBlur = 26;
const g = ctx.createRadialGradient(ball.x - 3, ball.y - 4, 1, ball.x, ball.y, ball.r + 4);
g.addColorStop(0, "#ffffff");
g.addColorStop(0.45, "#a5f3fc");
g.addColorStop(1, "#22d3ee");
ctx.fillStyle = g;
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.r, 0, TAU);
ctx.fill();
ctx.restore();
}
function drawFloaters() {
ctx.save();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
for (const f of floaters) {
const a = clamp(f.life / f.maxLife, 0, 1);
ctx.globalAlpha = a;
ctx.font = `900 ${f.size}px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif`;
ctx.fillStyle = f.color;
ctx.shadowColor = "rgba(0,0,0,0.55)";
ctx.shadowBlur = 14;
ctx.fillText(f.text, f.x, f.y);
}
ctx.restore();
}
function drawText(text, x, y, size, color, weight = "800", blur = 14) {
ctx.save();
ctx.font = `${weight} ${size}px ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif`;
ctx.fillStyle = color;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.shadowColor = "rgba(0,0,0,0.6)";
ctx.shadowBlur = blur;
ctx.fillText(text, x, y);
ctx.restore();
}
function fullOverlay(lines, accent = "#7dd3fc") {
ctx.save();
ctx.fillStyle = "rgba(3,6,16,0.72)";
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = "rgba(10,16,34,0.74)";
drawRoundRect(W / 2 - 380, H / 2 - 150, 760, 300, 30);
ctx.fill();
ctx.strokeStyle = "rgba(125,211,252,0.32)";
ctx.lineWidth = 2;
ctx.stroke();
const titleSize = lines[0].length > 14 ? 50 : 64;
let y = H / 2 - 54;
drawText(lines[0], W / 2, y, titleSize, accent, "900", 24);
y += 64;
for (let i = 1; i < lines.length; i++) {
drawText(lines[i], W / 2, y, 24, "#cbd5e1", "700", 10);
y += 38;
}
ctx.restore();
}
function drawReadyBanner() {
ctx.save();
ctx.fillStyle = "rgba(2,6,23,0.55)";
drawRoundRect(W / 2 - 270, H - 190, 540, 78, 20);
ctx.fill();
ctx.strokeStyle = "rgba(125,211,252,0.32)";
ctx.lineWidth = 2;
ctx.stroke();
drawText("Click or press Space to launch", W / 2, H - 164, 26, "#e0f2fe", "800", 12);
drawText(`Level ${level} · ${levelName}`, W / 2, H - 136, 18, "#93c5fd", "700", 8);
ctx.restore();
}
function drawOverlays() {
if (state === "menu") {
fullOverlay([
"Neon Breakout",
"Move with the mouse or ←/→ / A/D",
"Clear every brick, keep the ball alive",
"Click or press Space to start"
]);
} else if (state === "ready") {
drawReadyBanner();
} else if (state === "paused") {
fullOverlay(["Paused", "Press P or click to resume"]);
} else if (state === "clear") {
if (level >= LEVELS.length) {
fullOverlay([
"Final Level Clear!",
`Score ${format(score)} · Best ${format(Math.max(best, score))}`,
"Click or press Space to finish"
], "#86efac");
} else {
fullOverlay([
"Level Clear!",
`${levelName} cleared · Score ${format(score)}`,
"Click or press Space for next level"
], "#86efac");
}
} else if (state === "over") {
fullOverlay([
"Game Over",
`Score ${format(score)} · Best ${format(Math.max(best, score))}`,
"Click or press Space to restart"
], "#fb7185");
} else if (state === "win") {
fullOverlay([
"You Win!",
`All ${LEVELS.length} levels cleared with ${format(score)} points`,
"Click or press Space to play again"
], "#fde047");
}
}
function render() {
ctx.save();
if (shake > 0.1) {
ctx.translate(rand(-shake, shake) * 0.5, rand(-shake, shake) * 0.5);
}
drawBackground();
drawWalls();
drawBricks();
drawParticles();
drawPaddle();
drawBall();
drawFloaters();
ctx.restore();
drawOverlays();
}
// ---------- Input ----------
function setPointer(e) {
const rect = canvas.getBoundingClientRect();
if (!rect.width) return;
const x = (e.clientX - rect.left) * (W / rect.width);
paddle.targetX = clamp(x, paddle.w / 2, W - paddle.w / 2);
lastInput = "mouse";
}
canvas.addEventListener("pointermove", setPointer);
canvas.addEventListener("pointerdown", e => {
e.preventDefault();
setPointer(e);
primaryAction();
});
canvas.addEventListener("contextmenu", e => e.preventDefault());
window.addEventListener("keydown", e => {
const c = e.code;
if (c === "ArrowLeft" || c === "KeyA") {
keys.left = true;
lastInput = "keyboard";
e.preventDefault();
} else if (c === "ArrowRight" || c === "KeyD") {
keys.right = true;
lastInput = "keyboard";
e.preventDefault();
}
if (e.repeat) return;
if (c === "Space" || c === "Enter") {
initAudio();
primaryAction();
e.preventDefault();
} else if (c === "KeyP") {
togglePause();
e.preventDefault();
} else if (c === "KeyR") {
initAudio();
startGame(true);
e.preventDefault();
} else if (c === "KeyM") {
soundOn = !soundOn;
addFloater(W / 2, H - 96, soundOn ? "Sound On" : "Sound Off", "#7dd3fc", 28);
e.preventDefault();
}
});
window.addEventListener("keyup", e => {
const c = e.code;
if (c === "ArrowLeft" || c === "KeyA") keys.left = false;
if (c === "ArrowRight" || c === "KeyD") keys.right = false;
});
window.addEventListener("blur", () => {
keys.left = false;
keys.right = false;
if (state === "play" || state === "ready") togglePause();
});
// ---------- Boot ----------
best = loadBest();
resetPaddle();
newLevel(1);
state = "menu";
updateHUD();
let last = performance.now();
function frame(now) {
const dt = Math.min(0.033, Math.max(0, (now - last) / 1000));
last = now;
update(dt);
render();
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
})();
</script>
</body>
</html>

