Run details
- Model
- inclusionai/ling-3.1-flash
- Route
- OpenRouter API
- Served by
- Novita
- Effort
- model default
- Limits
- 60 min · 235,929 output tokens (model max)
- Date
- 24 Oct 2026
- Time
- 11 min 45 s
- Size
- 40 kB
- Tokens
- 98 in, 63,384 out (34,063 thinking)
- List price
- $0.057 at today's price ($0.30 in / $0.90 out per M)
- Paid
- $0.014
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
40 kB 1,125 lines Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Neon Breakout</title>
<style>
html,body{margin:0;padding:0;height:100%;background:#04050c;overflow:hidden;}
#wrap{position:fixed;inset:0;display:flex;align-items:center;justify-content:center;
background:radial-gradient(1200px 800px at 50% 40%, #0a0f26 0%, #04050c 70%);}
canvas{display:block;touch-action:none;user-select:none;-webkit-user-select:none;
border-radius:10px;
box-shadow:0 0 80px rgba(70,110,255,.18), 0 0 0 1px rgba(110,140,255,.18);}
</style>
</head>
<body>
<div id="wrap"><canvas id="game"></canvas></div>
<script>
(() => {
'use strict';
/* ============================ CANVAS ============================ */
const LW = 960, LH = 720;
const WALL = 10, TOP = 52;
const FONT = '"Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif';
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
let bgGrad = null, vigGrad = null, dangerGrad = null;
function resize(){
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const scale = Math.min(window.innerWidth / LW, window.innerHeight / LH) * 0.97;
canvas.style.width = Math.floor(LW * scale) + 'px';
canvas.style.height = Math.floor(LH * scale) + 'px';
canvas.width = Math.round(LW * dpr);
canvas.height = Math.round(LH * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
bgGrad = ctx.createLinearGradient(0, 0, 0, LH);
bgGrad.addColorStop(0, '#0d1233');
bgGrad.addColorStop(0.55, '#080b1e');
bgGrad.addColorStop(1, '#04050d');
vigGrad = ctx.createRadialGradient(LW/2, LH/2, LH*0.32, LW/2, LH/2, LH*0.78);
vigGrad.addColorStop(0, 'rgba(0,0,0,0)');
vigGrad.addColorStop(1, 'rgba(0,0,8,0.55)');
dangerGrad = ctx.createLinearGradient(0, LH-70, 0, LH);
dangerGrad.addColorStop(0, 'rgba(255,60,90,0)');
dangerGrad.addColorStop(1, 'rgba(255,60,90,0.14)');
}
window.addEventListener('resize', resize);
resize();
/* ============================ AUDIO ============================ */
const AudioSys = {
ctx:null, master:null, muted:false,
ensure(){
if(this.ctx){ if(this.ctx.state==='suspended') this.ctx.resume(); return; }
const AC = window.AudioContext || window.webkitAudioContext;
if(!AC) return;
try{
this.ctx = new AC();
this.master = this.ctx.createGain();
this.master.gain.value = 0.32;
this.master.connect(this.ctx.destination);
}catch(e){ this.ctx = null; }
},
tone(freq, dur, type, vol, slideTo, delay){
if(!this.ctx || this.muted) return;
type = type||'square'; vol = vol==null?0.3:vol; delay = delay||0;
const t0 = this.ctx.currentTime + delay;
const o = this.ctx.createOscillator(), g = this.ctx.createGain();
o.type = type;
o.frequency.setValueAtTime(freq, t0);
if(slideTo) o.frequency.exponentialRampToValueAtTime(Math.max(1,slideTo), t0+dur);
g.gain.setValueAtTime(vol, t0);
g.gain.exponentialRampToValueAtTime(0.0001, t0+dur);
o.connect(g); g.connect(this.master);
o.start(t0); o.stop(t0+dur+0.03);
},
noise(dur, vol, freq){
if(!this.ctx || this.muted) return;
const t0 = this.ctx.currentTime;
const len = Math.max(1, Math.floor(this.ctx.sampleRate*dur));
const buf = this.ctx.createBuffer(1, len, this.ctx.sampleRate);
const d = buf.getChannelData(0);
for(let i=0;i<len;i++) d[i] = (Math.random()*2-1)*(1-i/len);
const src = this.ctx.createBufferSource(); src.buffer = buf;
const f = this.ctx.createBiquadFilter(); f.type='bandpass'; f.frequency.value=freq||1200; f.Q.value=0.8;
const g = this.ctx.createGain();
g.gain.setValueAtTime(vol||0.3, t0);
g.gain.exponentialRampToValueAtTime(0.0001, t0+dur);
src.connect(f); f.connect(g); g.connect(this.master);
src.start(t0);
}
};
function sfx(name){
if(!AudioSys.ctx || AudioSys.muted) return;
switch(name){
case 'paddle': AudioSys.tone(210,0.08,'square',0.32,150); break;
case 'wall': AudioSys.tone(150,0.05,'triangle',0.18,110); break;
case 'hit': AudioSys.tone(320,0.05,'square',0.22,260); break;
case 'armor': AudioSys.tone(170,0.06,'square',0.25,110); break;
case 'clank': AudioSys.tone(95,0.06,'square',0.22,60); AudioSys.noise(0.05,0.12,2500); break;
case 'break': AudioSys.noise(0.12,0.32,1800); AudioSys.tone(520,0.09,'square',0.2,180); break;
case 'power': AudioSys.tone(440,0.07,'square',0.28); AudioSys.tone(660,0.07,'square',0.28,null,0.07); AudioSys.tone(880,0.11,'square',0.28,null,0.14); break;
case 'powerdown':AudioSys.tone(280,0.16,'square',0.18,110); break;
case 'launch': AudioSys.tone(330,0.1,'square',0.28,540); break;
case 'lose': AudioSys.tone(300,0.4,'sawtooth',0.32,55); AudioSys.noise(0.3,0.22,300); break;
case 'laser': AudioSys.tone(920,0.08,'square',0.16,180); break;
case 'combo': AudioSys.tone(700,0.06,'square',0.22,980); break;
case 'click': AudioSys.tone(600,0.05,'square',0.18); break;
case 'clear': [523,659,784,1046].forEach((f,i)=>AudioSys.tone(f,0.13,'square',0.28,null,i*0.09)); break;
case 'over': [400,300,220,150].forEach((f,i)=>AudioSys.tone(f,0.22,'sawtooth',0.24,null,i*0.16)); break;
case 'win': [523,659,784,1046,1318,1568].forEach((f,i)=>AudioSys.tone(f,0.16,'square',0.28,null,i*0.1)); break;
}
}
/* ============================ UTILS ============================ */
const TAU = Math.PI*2;
const clamp = (v,a,b)=> v<a?a : v>b?b : v;
const lerp = (a,b,t)=> a+(b-a)*t;
const rand = (a,b)=> a+Math.random()*(b-a);
const frac = v => v - Math.floor(v);
function easeOutBack(t){ const c1=1.70158, c3=c1+1; return 1 + c3*Math.pow(t-1,3) + c1*Math.pow(t-1,2); }
function rr(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 circleRect(cx,cy,r, rx,ry,rw,rh){
const nx = clamp(cx,rx,rx+rw), ny = clamp(cy,ry,ry+rh);
const dx = cx-nx, dy = cy-ny;
return dx*dx + dy*dy <= r*r;
}
/* ============================ LEVELS ============================ */
const BRICK_DEFS = {
'1':{hp:1}, '2':{hp:2}, '3':{hp:3},
'A':{hp:2, armor:true},
'B':{hp:3, armor:true},
'X':{hp:999, unbreakable:true}
};
const LEVELS = [
{ name:'FIRST CONTACT', speed:430, layout:[
'............',
'111111111111',
'111111111111',
'111111111111',
'.1111111111.',
'..11111111..',
]},
{ name:'CHECKMATE', speed:455, layout:[
'1.1.1..1.1.1',
'.2.2.22.2.2.',
'2.2.2..2.2.2',
'.1.1.11.1.1.',
'1.1.1..1.1.1',
'.2.2.22.2.2.',
]},
{ name:'THE FORTRESS', speed:480, layout:[
'X..........X',
'X1111111111X',
'X1A111111A1X',
'X1111111111X',
'X..111111..X',
'X...1111...X',
]},
{ name:'DIAMOND CORE', speed:505, layout:[
'.....11.....',
'....1221....',
'...122221...',
'..122AA221..',
'...122221...',
'....1221....',
'.....11.....',
]},
{ name:'INVADER', speed:530, layout:[
'..1......1..',
'...1....1...',
'..11111111..',
'.11.1111.11.',
'111111111111',
'1.11111111.1',
'1.1......1.1',
'...11..11...',
]},
{ name:'THE GAUNTLET', speed:560, layout:[
'3.3.3..3.3.3',
'333333333333',
'A.A.A.A.A.A.',
'222222222222',
'X.2.2..2.2.X',
'..11111111..',
]},
];
const POWER_STYLE = {
W:{c:'#2ed573', t:'W', name:'WIDE PADDLE'},
M:{c:'#ff6b81', t:'M', name:'MULTIBALL'},
S:{c:'#54a0ff', t:'S', name:'SLOW-MO'},
G:{c:'#c77dff', t:'G', name:'LASERS'},
L:{c:'#ffd32a', t:'+', name:'+1 LIFE'},
};
/* ============================ STATE ============================ */
let state = 'menu'; // menu | playing | paused | clear | gameover | win
let time = 0;
let score = 0, hiScore = 0, prevHi = 0, newBest = false;
let lives = 3, levelIndex = 0, combo = 0, bestCombo = 0;
let bricksLeft = 0, bricksBroken = 0, speedBoost = 1;
let levelTime = 0, bannerT = 0, clearTimer = 0, clearBonus = 0;
let shakeT = 0, shakeDur = 1, shakeMag = 0, flashT = 0, flashColor = '255,255,255';
let wideTimer = 0, slowTimer = 0, laserTimer = 0, fireCD = 0;
let mouseIgnore = 0;
let paddle, balls = [], bricks = [], particles = [], popups = [], rings = [], powerups = [], lasers = [], stars = [];
try{ hiScore = parseInt(localStorage.getItem('neonBreakoutHi')||'0',10)||0; }catch(e){ hiScore = 0; }
function saveHi(){ try{ localStorage.setItem('neonBreakoutHi', String(hiScore)); }catch(e){} }
function addScore(n){ score += n; if(score > hiScore) hiScore = score; }
for(let i=0;i<90;i++) stars.push({x:Math.random()*LW, y:Math.random()*LH, z:rand(0.25,1), tw:rand(0,TAU)});
function newPaddle(){ return {x:LW/2, y:LH-56, w:116, h:16, baseW:116, vx:0, tilt:0, squash:0}; }
paddle = newPaddle();
function levelSpeed(){ return LEVELS[Math.min(levelIndex, LEVELS.length-1)].speed; }
function targetSpeed(){ return levelSpeed()*speedBoost; }
/* ============================ FX HELPERS ============================ */
function shake(t,m){ shakeT=t; shakeDur=t; shakeMag=m; }
function flash(a,c){ flashT=Math.max(flashT,a); flashColor=c||'255,255,255'; }
function addPopup(x,y,text,color,size,life){
popups.push({x:clamp(x,80,LW-80), y, text, color, size:size||16, life:life||0.9, max:life||0.9, vy:-46});
if(popups.length>40) popups.splice(0, popups.length-40);
}
function ring(x,y,color,maxR){ rings.push({x,y,r:6,vr:maxR*3.4,life:0.35,max:0.35,color}); }
function spawnShards(br){
for(let i=0;i<12;i++){
const a=rand(0,TAU), sp=rand(60,320);
particles.push({x:br.x+br.w/2+rand(-8,8), y:br.y+br.h/2+rand(-6,6),
vx:Math.cos(a)*sp, vy:Math.sin(a)*sp-70, grav:640,
life:rand(0.35,0.8), max:0.8, size:rand(2.5,6), rot:rand(0,TAU), vr:rand(-9,9),
color: Math.random()<0.25 ? '#ffffff' : 'hsl('+br.hue+',85%,'+(rand(55,72)|0)+'%)', shard:true});
}
ring(br.x+br.w/2, br.y+br.h/2, 'hsl('+br.hue+',90%,70%)', 46);
}
function sparkAt(x,y,color){
for(let i=0;i<5;i++) particles.push({x,y,vx:rand(-170,170),vy:rand(-170,170),grav:0,
life:0.25,max:0.25,size:2,rot:0,vr:0,color:color||'#aab4c8',shard:false});
}
/* ============================ GAME FLOW ============================ */
function buildLevel(idx){
const def = LEVELS[idx];
bricks = [];
const rows = def.layout.length;
let cols = 0;
for(const r of def.layout) cols = Math.max(cols, r.length);
const gap = 6, marginX = 64, topY = 112;
const bw = (LW - 2*marginX - (cols-1)*gap) / cols, bh = 26;
bricksLeft = 0;
def.layout.forEach((row,r)=>{
const hue = (idx*53 + r*30) % 360;
for(let c=0;c<row.length;c++){
const ch = row[c];
if(ch==='.'||ch===' ') continue;
const d = BRICK_DEFS[ch];
if(!d) continue;
bricks.push({
x:marginX + c*(bw+gap), y:topY + r*(bh+gap), w:bw, h:bh,
hp:d.hp, maxHp:d.hp, armor:!!d.armor, unbreakable:!!d.unbreakable,
hue, flash:0, delay:(r*cols+c)*0.014, alive:true, seed:Math.random()*1000
});
if(!d.unbreakable) bricksLeft++;
}
});
}
function makeBall(stuck){
return {x:paddle.x, y:paddle.y-paddle.h/2-10, vx:0, vy:0, r:8, speed:targetSpeed(), stuck:!!stuck, trail:[]};
}
function resetStuck(){ balls = [makeBall(true)]; }
function launchStuck(){
let launched = false;
for(const b of balls){
if(b.stuck){
b.stuck = false;
const a = rand(-0.35,0.35);
b.vx = Math.sin(a)*b.speed; b.vy = -Math.cos(a)*b.speed;
launched = true;
}
}
if(launched) sfx('launch');
return launched;
}
function startGame(){
score=0; lives=3; levelIndex=0; combo=0; bestCombo=0; bricksBroken=0;
prevHi = hiScore; newBest = false;
wideTimer=slowTimer=laserTimer=0; fireCD=0; speedBoost=1;
powerups=[]; lasers=[]; particles=[]; popups=[]; rings=[];
paddle = newPaddle();
buildLevel(0);
levelTime=0; bannerT=2.2;
resetStuck();
state='playing';
flash(0.5,'255,255,255');
sfx('click');
}
function levelCleared(){
clearBonus = 250 + 100*(levelIndex+1) + lives*50;
addScore(clearBonus);
state='clear'; clearTimer=2.6;
balls=[]; lasers=[]; powerups=[];
flash(0.7,'255,255,255');
shake(0.4,8);
sfx('clear');
addPopup(LW/2, LH*0.40, 'LEVEL CLEAR!', '#7dffb0', 44, 1.8);
addPopup(LW/2, LH*0.40+46, 'BONUS +'+clearBonus, '#ffd76a', 22, 1.8);
}
function advanceLevel(){
levelIndex++;
if(levelIndex >= LEVELS.length){
state='win';
newBest = score > prevHi && score > 0;
saveHi();
sfx('win');
for(let i=0;i<140;i++) confettiPiece();
return;
}
buildLevel(levelIndex);
levelTime=0; bannerT=2.2; speedBoost=1;
wideTimer=slowTimer=laserTimer=0; fireCD=0;
powerups=[]; lasers=[];
paddle = newPaddle();
resetStuck();
state='playing';
flash(0.45,'255,255,255');
}
function loseLife(){
lives--; combo=0; speedBoost=1;
wideTimer=slowTimer=laserTimer=0;
shake(0.5,12); flash(0.6,'255,60,80');
ring(paddle.x, paddle.y-20, '#ff5d7a', 90);
if(lives<=0){ gameOver(); }
else{
resetStuck();
sfx('lose');
addPopup(LW/2, LH*0.55, 'BALL LOST', '#ff8fa0', 30, 1.2);
}
}
function gameOver(){
state='gameover';
newBest = score > prevHi && score > 0;
saveHi();
sfx('over');
}
function confettiPiece(){
particles.push({x:rand(0,LW), y:-10, vx:rand(-30,30), vy:rand(50,170), grav:70,
life:rand(2,3.5), max:3.5, size:rand(3,6), rot:rand(0,TAU), vr:rand(-6,6),
color:'hsl('+(rand(0,360)|0)+',90%,65%)', shard:true});
}
/* ============================ BRICKS / POWERUPS ============================ */
function damageBrick(br){
if(br.unbreakable){ sfx('clank'); sparkAt(br.x+br.w/2, br.y+br.h/2, '#8b96ad'); return false; }
br.hp--; br.flash=1;
if(br.hp<=0){ destroyBrick(br); return true; }
addScore(5);
sfx(br.armor ? 'armor' : 'hit');
return false;
}
function destroyBrick(br){
br.alive=false;
bricksLeft--; bricksBroken++;
combo++;
bestCombo = Math.max(bestCombo, combo);
const mult = 1 + Math.min(combo-1,15)*0.1;
const pts = Math.round(50*br.maxHp*mult/5)*5;
addScore(pts);
addPopup(br.x+br.w/2, br.y+br.h/2, '+'+pts, 'hsl('+br.hue+',90%,75%)', combo>=5?20:15, 0.9);
if(combo>=5 && combo%5===0){
addPopup(LW/2, LH*0.38, 'COMBO x'+combo+'!', '#ffd76a', 34, 1.2);
sfx('combo');
}
speedBoost = Math.min(1.55, speedBoost + 0.004);
spawnShards(br);
shake(0.1, 3.5);
maybeDrop(br);
sfx('break');
if(bricksLeft<=0) levelCleared();
}
function maybeDrop(br){
if(powerups.length>=3) return;
if(Math.random() < 0.16){
const r = Math.random();
let t = r<0.28 ? 'W' : r<0.50 ? 'M' : r<0.70 ? 'S' : r<0.90 ? 'G' : 'L';
powerups.push({x:br.x+br.w/2, y:br.y+br.h/2, vy:135, type:t, pulse:rand(0,TAU)});
}
}
function applyPowerup(t){
sfx('power');
const px=paddle.x, py=paddle.y-34;
const st = POWER_STYLE[t];
if(t==='W'){ wideTimer=14; addPopup(px,py,st.name,st.c,20,1.1); }
if(t==='S'){ slowTimer=8; addPopup(px,py,st.name,st.c,20,1.1); }
if(t==='G'){ laserTimer=10; addPopup(px,py,st.name,st.c,22,1.2); }
if(t==='L'){ lives=Math.min(5,lives+1); addPopup(px,py,st.name,st.c,24,1.3); }
if(t==='M'){ addPopup(px,py,st.name+'!',st.c,24,1.2); multiball(); }
ring(paddle.x, paddle.y, st.c, 60);
}
function multiball(){
const src = balls.filter(b=>!b.stuck);
const list = src.length ? src : balls;
const add = [];
for(const b of list){
const base = (b.vx||b.vy) ? Math.atan2(b.vx,-b.vy) : rand(-0.3,0.3);
for(const da of [-0.55, 0.55]){
if(balls.length + add.length >= 6) break;
add.push({x:b.x, y:b.y, vx:Math.sin(base+da)*b.speed, vy:-Math.cos(base+da)*b.speed,
r:8, speed:b.speed, stuck:false, trail:[]});
}
if(balls.length + add.length >= 6) break;
}
for(const b of list){ if(b.stuck){ b.stuck=false; const a=rand(-0.3,0.3); b.vx=Math.sin(a)*b.speed; b.vy=-Math.cos(a)*b.speed; } }
balls.push(...add);
}
/* ============================ UPDATE ============================ */
function updatePaddle(dt){
const p = paddle;
const prev = p.x;
let dir = 0;
if(keys['arrowleft']||keys['a']) dir -= 1;
if(keys['arrowright']||keys['d']) dir += 1;
if(dir!==0){ p.x += dir*840*dt; mouseIgnore = 0.5; }
else if(mouseX!=null && mouseIgnore<=0){ p.x += (mouseX-p.x)*Math.min(1, dt*20); }
const targetW = p.baseW * (wideTimer>0 ? 1.55 : 1);
p.w = lerp(p.w, targetW, 1-Math.exp(-10*dt));
p.x = clamp(p.x, WALL+p.w/2, LW-WALL-p.w/2);
p.vx = (p.x-prev)/Math.max(dt,1e-4);
p.tilt = lerp(p.tilt, clamp(p.vx*0.00035,-0.16,0.16), 1-Math.exp(-12*dt));
p.squash = Math.max(0, p.squash - dt*5);
if(Math.abs(p.vx)>500 && Math.random()<dt*20){
particles.push({x:p.x-Math.sign(p.vx)*p.w/2, y:p.y+4, vx:-p.vx*0.15+rand(-20,20), vy:rand(20,80),
grav:0, life:0.3, max:0.3, size:rand(1.5,3), rot:0, vr:0, color:'#59d8ff', shard:false});
}
}
function ensureMinVy(b){
const sp = Math.hypot(b.vx,b.vy);
if(sp<1) return;
const minVy = sp*0.22;
if(Math.abs(b.vy) < minVy){
b.vy = (b.vy<0?-1:1)*minVy;
const sx = b.vx<0?-1:1;
b.vx = sx*Math.sqrt(Math.max(0, sp*sp - b.vy*b.vy));
}
}
function updateBall(b, dt){
b.trail.push({x:b.x, y:b.y});
if(b.trail.length>16) b.trail.shift();
const tgt = targetSpeed();
const cur = Math.hypot(b.vx,b.vy) || tgt;
const ns = lerp(cur, tgt, 1-Math.exp(-2.5*dt));
if(cur>0){ b.vx*=ns/cur; b.vy*=ns/cur; b.speed=ns; }
const steps = Math.max(1, Math.ceil(ns*dt/(b.r*0.8)));
const sdt = dt/steps;
for(let i=0;i<steps;i++){
b.x += b.vx*sdt; b.y += b.vy*sdt;
if(b.x < WALL+b.r){ b.x=WALL+b.r; b.vx=Math.abs(b.vx); sfx('wall'); }
else if(b.x > LW-WALL-b.r){ b.x=LW-WALL-b.r; b.vx=-Math.abs(b.vx); sfx('wall'); }
if(b.y < TOP+b.r){ b.y=TOP+b.r; b.vy=Math.abs(b.vy); sfx('wall'); }
// paddle
const p = paddle;
if(b.vy>0 && circleRect(b.x,b.y,b.r, p.x-p.w/2, p.y-p.h/2, p.w, p.h)){
const off = clamp((b.x-p.x)/(p.w/2), -1, 1);
const ang = off*(Math.PI*0.38);
const sp = b.speed;
b.vx = Math.sin(ang)*sp; b.vy = -Math.cos(ang)*sp;
b.y = p.y - p.h/2 - b.r - 0.5;
p.squash = 1; combo = 0;
sfx('paddle');
ring(b.x, p.y-p.h/2, '#7df9ff', 30);
shake(0.05, 2);
for(let k=0;k<4;k++) particles.push({x:b.x+rand(-6,6), y:p.y-p.h/2, vx:rand(-60,60), vy:rand(-140,-40),
grav:300, life:0.3, max:0.3, size:2, rot:0, vr:0, color:'#9fefff', shard:false});
}
// bricks
for(const br of bricks){
if(!br.alive) continue;
if(b.x+b.r < br.x || b.x-b.r > br.x+br.w || b.y+b.r < br.y || b.y-b.r > br.y+br.h) continue;
if(!circleRect(b.x,b.y,b.r, br.x,br.y,br.w,br.h)) continue;
const oL=(b.x+b.r)-br.x, oR=(br.x+br.w)-(b.x-b.r);
const oT=(b.y+b.r)-br.y, oB=(br.y+br.h)-(b.y-b.r);
const mX=Math.min(oL,oR), mY=Math.min(oT,oB);
if(mX<mY){
if(b.x < br.x+br.w/2){ b.vx=-Math.abs(b.vx); b.x=br.x-b.r-0.1; }
else { b.vx=Math.abs(b.vx); b.x=br.x+br.w+b.r+0.1; }
}else{
if(b.y < br.y+br.h/2){ b.vy=-Math.abs(b.vy); b.y=br.y-b.r-0.1; }
else { b.vy=Math.abs(b.vy); b.y=br.y+br.h+b.r+0.1; }
}
damageBrick(br);
break;
}
}
ensureMinVy(b);
}
function fireLasers(){
fireCD = 0.22;
const p = paddle;
lasers.push({x:p.x-p.w/2+10, y:p.y-12, vy:-980});
lasers.push({x:p.x+p.w/2-10, y:p.y-12, vy:-980});
sfx('laser');
}
function updateLasers(dt){
for(let i=lasers.length-1;i>=0;i--){
const l = lasers[i];
l.y += l.vy*dt;
if(l.y < TOP){ lasers.splice(i,1); continue; }
for(const br of bricks){
if(!br.alive) continue;
if(l.x>br.x-2 && l.x<br.x+br.w+2 && l.y>br.y-6 && l.y<br.y+br.h+2){
damageBrick(br);
sparkAt(l.x, l.y, '#c77dff');
lasers.splice(i,1);
break;
}
}
}
}
function updatePowerups(dt){
const p = paddle;
for(let i=powerups.length-1;i>=0;i--){
const u = powerups[i];
u.y += u.vy*dt; u.pulse += dt*6;
if(circleRect(u.x,u.y,16, p.x-p.w/2, p.y-p.h/2, p.w, p.h)){
applyPowerup(u.type);
powerups.splice(i,1);
continue;
}
if(u.y > LH+30) powerups.splice(i,1);
}
}
function updatePlaying(dt){
levelTime += dt;
if(bannerT>0) bannerT -= dt;
if(wideTimer>0){ wideTimer-=dt; if(wideTimer<=0){ wideTimer=0; sfx('powerdown'); } }
if(slowTimer>0){ slowTimer-=dt; if(slowTimer<=0){ slowTimer=0; sfx('powerdown'); } }
if(laserTimer>0){ laserTimer-=dt; if(laserTimer<=0){ laserTimer=0; sfx('powerdown'); } }
if(fireCD>0) fireCD -= dt;
updatePaddle(dt);
for(const b of balls) if(b.stuck){ b.x=paddle.x; b.y=paddle.y-paddle.h/2-b.r-2; }
const ballDt = dt * (slowTimer>0 ? 0.55 : 1);
for(let i=balls.length-1;i>=0;i--){
const b = balls[i];
if(!b) continue;
if(b.stuck) continue;
updateBall(b, ballDt);
if(b.y - b.r > LH+40) balls.splice(i,1);
}
if(state==='playing' && balls.length===0) loseLife();
updateLasers(dt);
updatePowerups(dt);
for(const br of bricks) if(br.flash>0) br.flash = Math.max(0, br.flash - dt*5);
if(laserTimer>0 && fireCD<=0 && (keys[' ']||mouseDown)) fireLasers();
}
function updateStars(dt){
for(const s of stars){
s.y += s.z*12*dt;
if(s.y > LH+2){ s.y=-2; s.x=Math.random()*LW; }
}
}
function updateParticles(dt){
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.grav||0)*dt;
p.x += p.vx*dt; p.y += p.vy*dt;
p.rot += (p.vr||0)*dt;
}
if(particles.length>600) particles.splice(0, particles.length-600);
}
function updatePopups(dt){
for(let i=popups.length-1;i>=0;i--){
const p = popups[i];
p.life -= dt; p.y += p.vy*dt;
if(p.life<=0) popups.splice(i,1);
}
}
function updateRings(dt){
for(let i=rings.length-1;i>=0;i--){
const r = rings[i];
r.life -= dt; r.r += r.vr*dt;
if(r.life<=0) rings.splice(i,1);
}
}
function update(dt){
time += dt;
updateStars(dt);
if(state==='paused') return;
updateParticles(dt); updatePopups(dt); updateRings(dt);
if(shakeT>0) shakeT -= dt;
if(flashT>0) flashT -= dt*2.4;
if(mouseIgnore>0) mouseIgnore -= dt;
switch(state){
case 'menu':
updatePaddle(dt);
if(Math.random()<dt*1.5){
particles.push({x:rand(0,LW), y:LH+12, vx:rand(-8,8), vy:rand(-45,-20), grav:0,
life:rand(2,4), max:4, size:rand(1,2.5), rot:0, vr:0,
color:'hsl('+(rand(180,280)|0)+',90%,70%)', shard:false});
}
break;
case 'playing': updatePlaying(dt); break;
case 'clear':
updatePaddle(dt);
clearTimer -= dt;
if(clearTimer<=0) advanceLevel();
break;
case 'win':
if(Math.random()<dt*30 && particles.length<350) confettiPiece();
break;
}
}
/* ============================ RENDER ============================ */
function drawStars(){
for(const s of stars){
const a = (0.25+0.55*s.z) * (0.6+0.4*Math.sin(time*2+s.tw));
ctx.globalAlpha = a;
ctx.fillStyle = '#b4c8ff';
ctx.fillRect(s.x, s.y, s.z*2, s.z*2);
}
ctx.globalAlpha = 1;
}
function drawFrame(){
ctx.save();
ctx.strokeStyle = 'rgba(80,130,255,0.55)';
ctx.lineWidth = 2;
ctx.shadowColor = '#3d6bff'; ctx.shadowBlur = 10;
ctx.beginPath();
ctx.moveTo(WALL, LH-4); ctx.lineTo(WALL, TOP);
ctx.lineTo(LW-WALL, TOP); ctx.lineTo(LW-WALL, LH-4);
ctx.stroke();
ctx.restore();
}
function rivets(x,y,w,h){
ctx.fillStyle = 'rgba(0,0,0,0.35)';
const o=5;
const pts=[[x+o,y+o],[x+w-o,y+o],[x+o,y+h-o],[x+w-o,y+h-o]];
for(const p of pts){ ctx.beginPath(); ctx.arc(p[0],p[1],1.6,0,TAU); ctx.fill(); }
}
function cracks(br,x,y,w,h){
const n = br.maxHp - br.hp;
ctx.strokeStyle = 'rgba(10,10,20,0.45)';
ctx.lineWidth = 1.2;
for(let i=0;i<n+1 && i<3;i++){
const x0 = x + w*(0.15 + 0.7*frac(Math.sin(br.seed+i*12.9)*43758.5));
ctx.beginPath();
ctx.moveTo(x0, y);
ctx.lineTo(x0 + (frac(Math.sin(br.seed+i*78.2)*12578.9)-0.5)*w*0.5, y+h*0.5);
ctx.lineTo(x0 + (frac(Math.sin(br.seed+i*31.7)*9741.2)-0.5)*w*0.6, y+h);
ctx.stroke();
}
}
function drawBricks(){
for(const br of bricks){
if(!br.alive) continue;
const t = clamp((levelTime-br.delay)/0.3, 0, 1);
if(t<=0) continue;
const s = easeOutBack(t);
const cx = br.x+br.w/2, cy = br.y+br.h/2;
ctx.save();
ctx.translate(cx,cy);
ctx.scale(s,s);
const x=-br.w/2, y=-br.h/2, w=br.w, h=br.h;
if(br.unbreakable){
ctx.fillStyle = '#232a3a'; rr(x,y,w,h,4); ctx.fill();
ctx.save(); rr(x,y,w,h,4); ctx.clip();
ctx.strokeStyle = 'rgba(255,255,255,0.06)'; ctx.lineWidth = 6;
for(let d=-h; d<w; d+=14){ ctx.beginPath(); ctx.moveTo(x+d,y+h); ctx.lineTo(x+d+h,y); ctx.stroke(); }
ctx.restore();
ctx.strokeStyle = '#46506a'; ctx.lineWidth = 1.5; rr(x+0.75,y+0.75,w-1.5,h-1.5,4); ctx.stroke();
rivets(x,y,w,h);
} else if(br.armor){
const light = br.hp<br.maxHp ? 42 : 58;
const g = ctx.createLinearGradient(0,y,0,y+h);
g.addColorStop(0,'hsl(215,18%,'+(light+12)+'%)');
g.addColorStop(0.5,'hsl(215,20%,'+light+'%)');
g.addColorStop(1,'hsl(215,22%,'+(light-14)+'%)');
ctx.fillStyle = g; rr(x,y,w,h,4); ctx.fill();
ctx.strokeStyle = 'rgba(200,220,255,0.35)'; ctx.lineWidth = 1; rr(x+0.5,y+0.5,w-1,h-1,4); ctx.stroke();
rivets(x,y,w,h);
cracks(br,x,y,w,h);
} else {
const g = ctx.createLinearGradient(0,y,0,y+h);
g.addColorStop(0,'hsl('+br.hue+',85%,64%)');
g.addColorStop(0.5,'hsl('+br.hue+',80%,54%)');
g.addColorStop(1,'hsl('+br.hue+',75%,44%)');
ctx.fillStyle = g; rr(x,y,w,h,4); ctx.fill();
ctx.fillStyle = 'rgba(255,255,255,0.28)'; rr(x+2,y+2,w-4,h*0.38,3); ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.35)'; ctx.lineWidth = 1; rr(x+0.5,y+0.5,w-1,h-1,4); ctx.stroke();
if(br.hp<br.maxHp) cracks(br,x,y,w,h);
}
if(br.flash>0){
ctx.fillStyle = 'rgba(255,255,255,'+(br.flash*0.75).toFixed(3)+')';
rr(x,y,w,h,4); ctx.fill();
}
ctx.restore();
}
}
function drawPaddle(){
const p = paddle;
ctx.save();
ctx.translate(p.x,p.y);
ctx.rotate(p.tilt);
ctx.scale(1+0.22*p.squash, 1-0.32*p.squash);
const w=p.w, h=p.h, x=-w/2, y=-h/2;
const glow = laserTimer>0 ? '#c77dff' : '#4dd0ff';
ctx.shadowColor = glow; ctx.shadowBlur = 16;
const g = ctx.createLinearGradient(0,y,0,y+h);
g.addColorStop(0,'#f2faff'); g.addColorStop(0.45,'#9fd8ff'); g.addColorStop(1,'#3f7de0');
ctx.fillStyle = g; rr(x,y,w,h,h/2); ctx.fill();
ctx.shadowBlur = 0;
ctx.fillStyle = 'rgba(255,255,255,0.75)'; rr(x+4,y+2,w-8,3,2); ctx.fill();
ctx.fillStyle = 'rgba(255,255,255,0.9)'; rr(x+w/2-10, y+h/2-2.5, 20, 5, 2.5); ctx.fill();
if(laserTimer>0){
ctx.fillStyle = '#c77dff';
ctx.shadowColor = '#c77dff'; ctx.shadowBlur = 10;
rr(x+2,y-8,8,10,3); ctx.fill();
rr(x+w-10,y-8,8,10,3); ctx.fill();
ctx.shadowBlur = 0;
}
if(wideTimer>0){
ctx.strokeStyle = 'rgba(46,213,115,'+(0.5+0.3*Math.sin(time*8)).toFixed(3)+')';
ctx.lineWidth = 2; rr(x-2,y-2,w+4,h+4,h/2+2); ctx.stroke();
}
ctx.restore();
}
function drawBalls(){
ctx.save();
ctx.globalCompositeOperation = 'lighter';
for(const b of balls){
const hue = 190 + clamp(b.speed/levelSpeed()-1, 0, 0.6)*160;
const col = 'hsl('+hue+',95%,70%)';
const n = b.trail.length;
for(let i=0;i<n;i++){
const t = b.trail[i], k = i/n;
ctx.globalAlpha = k*0.28;
ctx.fillStyle = col;
ctx.beginPath(); ctx.arc(t.x, t.y, b.r*k*0.9, 0, TAU); ctx.fill();
}
}
ctx.restore();
for(const b of balls){
const hue = 190 + clamp(b.speed/levelSpeed()-1, 0, 0.6)*160;
ctx.save();
ctx.shadowColor = 'hsl('+hue+',95%,70%)'; ctx.shadowBlur = 18;
const g = ctx.createRadialGradient(b.x-2,b.y-2,1, b.x,b.y,b.r);
g.addColorStop(0,'#ffffff');
g.addColorStop(0.5,'hsl('+hue+',100%,80%)');
g.addColorStop(1,'hsl('+hue+',95%,55%)');
ctx.fillStyle = g;
ctx.beginPath(); ctx.arc(b.x,b.y,b.r,0,TAU); ctx.fill();
ctx.restore();
}
}
function drawPowerups(){
for(const u of powerups){
const st = POWER_STYLE[u.type];
const s = 1 + 0.08*Math.sin(u.pulse);
ctx.save();
ctx.translate(u.x,u.y); ctx.scale(s,s);
ctx.shadowColor = st.c; ctx.shadowBlur = 14;
ctx.fillStyle = 'rgba(8,12,28,0.9)'; rr(-22,-11,44,22,11); ctx.fill();
ctx.shadowBlur = 0;
ctx.strokeStyle = st.c; ctx.lineWidth = 2; rr(-22,-11,44,22,11); ctx.stroke();
ctx.fillStyle = st.c;
ctx.font = '700 14px '+FONT;
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(st.t, 0, 1);
ctx.restore();
}
}
function drawLasers(){
for(const l of lasers){
ctx.save();
ctx.shadowColor = '#c77dff'; ctx.shadowBlur = 10;
ctx.strokeStyle = '#e9ccff'; ctx.lineWidth = 3;
ctx.beginPath(); ctx.moveTo(l.x,l.y); ctx.lineTo(l.x,l.y+14); ctx.stroke();
ctx.restore();
}
}
function drawParticles(){
for(const p of particles){
const a = clamp(p.life/p.max, 0, 1);
ctx.globalAlpha = a;
ctx.fillStyle = p.color;
if(p.shard){
ctx.save();
ctx.translate(p.x,p.y); ctx.rotate(p.rot);
ctx.fillRect(-p.size/2, -p.size/2, p.size, p.size*0.7);
ctx.restore();
} else {
ctx.beginPath(); ctx.arc(p.x,p.y, p.size*a+0.5, 0, TAU); ctx.fill();
}
}
ctx.globalAlpha = 1;
}
function drawRings(){
for(const r of rings){
const a = r.life/r.max;
ctx.globalAlpha = a*0.8;
ctx.strokeStyle = r.color;
ctx.lineWidth = 2.5;
ctx.beginPath(); ctx.arc(r.x,r.y,r.r,0,TAU); ctx.stroke();
}
ctx.globalAlpha = 1;
}
function drawPopups(){
for(const p of popups){
const a = clamp(p.life/p.max, 0, 1);
ctx.globalAlpha = Math.min(1, a*1.5);
ctx.font = '800 '+p.size+'px '+FONT;
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.lineWidth = 3; ctx.strokeStyle = 'rgba(0,0,10,0.7)';
ctx.strokeText(p.text, p.x, p.y);
ctx.fillStyle = p.color;
ctx.fillText(p.text, p.x, p.y);
}
ctx.globalAlpha = 1;
}
function drawMiniBall(x,y){
ctx.save();
ctx.shadowColor = '#7df9ff'; ctx.shadowBlur = 8;
ctx.fillStyle = '#bff3ff';
ctx.beginPath(); ctx.arc(x,y,6,0,TAU); ctx.fill();
ctx.restore();
}
function drawHUD(){
ctx.fillStyle = 'rgba(6,9,22,0.72)';
ctx.fillRect(0,0,LW,TOP-6);
ctx.strokeStyle = 'rgba(90,140,255,0.25)'; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(0,TOP-6.5); ctx.lineTo(LW,TOP-6.5); ctx.stroke();
ctx.textBaseline = 'middle';
// score
ctx.textAlign = 'left';
ctx.fillStyle = 'rgba(140,160,220,0.8)'; ctx.font = '600 11px '+FONT;
ctx.fillText('SCORE', 24, 15);
ctx.fillStyle = '#eaf2ff'; ctx.font = '800 22px '+FONT;
ctx.fillText(String(score).padStart(6,'0'), 24, 35);
if(combo>=2){
ctx.fillStyle = 'rgba(255,'+((180+Math.sin(time*10)*60)|0)+',80,0.95)';
ctx.font = '700 13px '+FONT;
ctx.fillText('COMBO x'+combo, 160, 35);
}
// level
const li = Math.min(levelIndex, LEVELS.length-1);
ctx.textAlign = 'center';
ctx.fillStyle = 'rgba(160,180,235,0.9)'; ctx.font = '600 12px '+FONT;
ctx.fillText('LEVEL '+(levelIndex+1)+'/'+LEVELS.length+' · '+LEVELS[li].name, LW/2, 27);
// best + lives
ctx.textAlign = 'right';
ctx.fillStyle = 'rgba(140,160,220,0.7)'; ctx.font = '600 10px '+FONT;
ctx.fillText('BEST '+String(hiScore).padStart(6,'0'), LW-24, 14);
const shown = Math.min(lives,5);
for(let i=0;i<shown;i++) drawMiniBall(LW-28-i*19, 33);
ctx.fillStyle = 'rgba(140,160,220,0.8)'; ctx.font = '600 10px '+FONT;
ctx.fillText('LIVES', LW-28-shown*19-10, 33);
// active effect pills
const pills = [];
if(wideTimer>0) pills.push({label:'WIDE', t:wideTimer, max:14, c:'#2ed573'});
if(slowTimer>0) pills.push({label:'SLOW', t:slowTimer, max:8, c:'#54a0ff'});
if(laserTimer>0) pills.push({label:'LASER', t:laserTimer, max:10, c:'#c77dff'});
let px = 24;
ctx.textAlign = 'center';
for(const pl of pills){
const w=116, h=16, y=LH-30;
ctx.fillStyle = 'rgba(6,10,24,0.7)'; rr(px,y,w,h,8); ctx.fill();
const f = clamp(pl.t/pl.max,0,1);
if(f>0){ ctx.fillStyle = pl.c; rr(px+2,y+2,Math.max(4,(w-4)*f),h-4,6); ctx.fill(); }
ctx.fillStyle = 'rgba(255,255,255,0.92)'; ctx.font = '700 10px '+FONT;
ctx.fillText(pl.label+' '+pl.t.toFixed(1), px+w/2, y+h/2+0.5);
px += w+10;
}
// sound hint
ctx.textAlign = 'right';
ctx.fillStyle = 'rgba(140,160,220,0.35)'; ctx.font = '600 10px '+FONT;
ctx.fillText(AudioSys.muted ? 'SOUND OFF [M]' : 'SOUND ON [M]', LW-20, LH-14);
}
function centerText(text,y,size,color,glow,weight){
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.font = (weight||800)+' '+size+'px '+FONT;
if(glow){ ctx.shadowColor = color; ctx.shadowBlur = glow; }
ctx.fillStyle = color;
ctx.fillText(text, LW/2, y);
ctx.shadowBlur = 0;
}
function dim(a){ ctx.fillStyle = 'rgba(3,5,16,'+a+')'; ctx.fillRect(0,0,LW,LH); }
function drawCapsule(x,y,t,c){
ctx.save();
ctx.translate(x,y);
ctx.shadowColor = c; ctx.shadowBlur = 12;
ctx.fillStyle = 'rgba(8,12,28,0.9)'; rr(-24,-13,48,26,13); ctx.fill();
ctx.shadowBlur = 0;
ctx.strokeStyle = c; ctx.lineWidth = 2; rr(-24,-13,48,26,13); ctx.stroke();
ctx.fillStyle = c; ctx.font = '800 15px '+FONT;
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(t, 0, 1);
ctx.restore();
}
function drawMenu(){
dim(0.45);
const pulse = 1 + 0.02*Math.sin(time*2.4);
ctx.save();
ctx.translate(LW/2, 190); ctx.scale(pulse,pulse);
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.font = '900 62px '+FONT;
ctx.shadowColor = '#4dd0ff'; ctx.shadowBlur = 30;
ctx.fillStyle = '#eafcff';
ctx.fillText('NEON BREAKOUT', 0, 0);
ctx.shadowBlur = 0;
ctx.restore();
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.font = '600 15px '+FONT;
ctx.fillStyle = 'rgba(160,190,255,0.75)';
ctx.fillText('SMASH BRICKS · CHAIN COMBOS · GRAB POWER-UPS', LW/2, 246);
const items = [['W','WIDE','#2ed573'],['M','MULTI','#ff6b81'],['S','SLOW','#54a0ff'],['G','LASER','#c77dff'],['+','LIFE','#ffd32a']];
let x = LW/2 - (items.length*118)/2 + 59;
for(const it of items){
drawCapsule(x, 312, it[0], it[2]);
ctx.font = '600 11px '+FONT;
ctx.fillStyle = 'rgba(200,215,255,0.8)';
ctx.fillText(it[1], x, 342);
x += 118;
}
ctx.font = '600 14px '+FONT;
ctx.fillStyle = 'rgba(190,205,240,0.9)';
ctx.fillText('MOVE: ← → / A D / MOUSE', LW/2, 418);
ctx.fillText('LAUNCH & FIRE LASERS: SPACE / CLICK', LW/2, 444);
ctx.fillText('P: PAUSE M: MUTE R: RESTART', LW/2, 470);
const a = 0.55 + 0.45*Math.sin(time*4);
ctx.globalAlpha = a;
centerText('CLICK OR PRESS SPACE TO START', 556, 22, '#8dffb0', 16);
ctx.globalAlpha = 1;
if(hiScore>0){
ctx.font = '700 14px '+FONT;
ctx.fillStyle = 'rgba(255,215,106,0.85)';
ctx.fillText('BEST '+String(hiScore).padStart(6,'0'), LW/2, 606);
}
}
function drawPause(){
dim(0.55);
centerText('PAUSED', LH*0.44, 52, '#eaf2ff', 24);
centerText('P / ESC / CLICK TO RESUME', LH*0.44+52, 16, 'rgba(170,190,240,0.9)');
centerText('M: MUTE · R: RESTART', LH*0.44+82, 13, 'rgba(140,160,220,0.7)');
}
function drawClear(){
dim(0.3);
centerText('LEVEL CLEAR!', LH*0.4, 52, '#7dffb0', 26);
centerText('BONUS +'+clearBonus, LH*0.4+48, 20, '#ffd76a');
const next = levelIndex+1 < LEVELS.length ? LEVELS[levelIndex+1].name : 'FINAL RESULTS';
centerText('NEXT: '+next, LH*0.4+86, 15, 'rgba(200,215,255,0.9)');
}
function drawGameOver(){
dim(0.72);
centerText('GAME OVER', 250, 62, '#ff5d7a', 30);
centerText('SCORE '+String(score).padStart(6,'0'), 322, 24, '#eaf2ff');
centerText('LEVEL REACHED '+(levelIndex+1)+' · BEST COMBO x'+bestCombo, 358, 15, 'rgba(170,190,240,0.9)');
if(newBest){
ctx.globalAlpha = 0.6+0.4*Math.sin(time*6);
centerText('★ NEW BEST! ★', 406, 22, '#ffd76a', 20);
ctx.globalAlpha = 1;
} else {
centerText('BEST '+String(hiScore).padStart(6,'0'), 406, 16, 'rgba(255,215,106,0.8)');
}
ctx.globalAlpha = 0.5+0.4*Math.sin(time*4);
centerText('CLICK OR PRESS R TO TRY AGAIN', 492, 17, '#9fefff');
ctx.globalAlpha = 1;
}
function drawWin(){
dim(0.6);
centerText('YOU WIN!', 240, 66, '#ffd76a', 34);
centerText('YOU CLEARED ALL '+LEVELS.length+' LEVELS', 306, 18, 'rgba(200,215,255,0.95)');
centerText('FINAL SCORE '+String(score).padStart(6,'0'), 356, 26, '#eaf2ff');
centerText('BRICKS SMASHED '+bricksBroken+' · BEST COMBO x'+bestCombo, 390, 15, 'rgba(170,190,240,0.9)');
if(newBest){
ctx.globalAlpha = 0.6+0.4*Math.sin(time*6);
centerText('★ NEW BEST! ★', 436, 22, '#ffd76a', 20);
ctx.globalAlpha = 1;
}
ctx.globalAlpha = 0.5+0.4*Math.sin(time*4);
centerText('CLICK OR PRESS R TO PLAY AGAIN', 500, 17, '#8dffb0');
ctx.globalAlpha = 1;
}
function drawPlayHints(){
if(bannerT>0){
const a = clamp(Math.min(bannerT/0.5, (2.2-bannerT)/0.25), 0, 1);
ctx.globalAlpha = a;
centerText('LEVEL '+(levelIndex+1), LH*0.4-46, 20, 'rgba(170,200,255,0.95)');
centerText(LEVELS[Math.min(levelIndex,LEVELS.length-1)].name, LH*0.4, 46, '#ffffff', 24);
ctx.globalAlpha = 1;
}
let stuck = false;
for(const b of balls) if(b.stuck){ stuck=true; break; }
if(stuck){
ctx.globalAlpha = 0.5+0.4*Math.sin(time*5);
centerText('CLICK OR PRESS SPACE TO LAUNCH', LH-150, 16, '#9fefff');
ctx.globalAlpha = 1;
}
}
function render(){
ctx.fillStyle = bgGrad;
ctx.fillRect(0,0,LW,LH);
drawStars();
ctx.fillStyle = dangerGrad;
ctx.fillRect(0,LH-70,LW,70);
ctx.save();
if(shakeT>0){
const k = shakeT/shakeDur;
ctx.translate(rand(-1,1)*shakeMag*k, rand(-1,1)*shakeMag*k);
}
drawFrame();
drawBricks();
drawPowerups();
drawLasers();
drawPaddle();
drawBalls();
drawParticles();
drawRings();
drawPopups();
ctx.restore();
ctx.fillStyle = vigGrad;
ctx.fillRect(0,0,LW,LH);
if(slowTimer>0 && state==='playing'){
const g = ctx.createRadialGradient(LW/2,LH/2,LH*0.3, LW/2,LH/2,LH*0.7);
g.addColorStop(0,'rgba(60,120,255,0)');
g.addColorStop(1,'rgba(60,120,255,0.18)');
ctx.fillStyle = g;
ctx.fillRect(0,0,LW,LH);
}
if(flashT>0){
ctx.fillStyle = 'rgba('+flashColor+','+(flashT*0.55).toFixed(3)+')';
ctx.fillRect(0,0,LW,LH);
}
if(state!=='menu') drawHUD();
if(state==='menu') drawMenu();
else if(state==='paused') drawPause();
else if(state==='clear') drawClear();
else if(state==='gameover') drawGameOver();
else if(state==='win') drawWin();
else if(state==='playing') drawPlayHints();
canvas.style.cursor = (state==='playing') ? 'none' : 'default';
}
/* ============================ INPUT ============================ */
const keys = {};
let mouseX = null, mouseDown = false;
function toLogical(e){
const r = canvas.getBoundingClientRect();
return { x:(e.clientX-r.left)/r.width*LW, y:(e.clientY-r.top)/r.height*LH };
}
function togglePause(){
if(state==='playing'){ state='paused'; sfx('click'); }
else if(state==='paused'){ state='playing'; sfx('click'); }
}
function tryFire(){ if(laserTimer>0 && fireCD<=0) fireLasers(); }
function action(){
if(state==='menu'){ startGame(); return; }
if(state==='gameover'||state==='win'){ startGame(); return; }
if(state==='paused'){ togglePause(); return; }
if(state==='clear'){ clearTimer = Math.min(clearTimer, 0.35); return; }
if(state==='playing'){ if(!launchStuck()) tryFire(); }
}
window.addEventListener('keydown', e=>{
const k = e.key.toLowerCase();
if(k==='arrowleft'||k==='arrowright'||k==='arrowup'||k==='arrowdown'||k===' ') e.preventDefault();
AudioSys.ensure();
if(!e.repeat){
if(k==='m') AudioSys.muted = !AudioSys.muted;
if(k==='p' || k==='escape') togglePause();
if(k===' ' || k==='enter' || k==='arrowup') action();
if(k==='r' && state!=='menu') startGame();
}
keys[k] = true;
});
window.addEventListener('keyup', e=>{ keys[e.key.toLowerCase()] = false; });
window.addEventListener('pointermove', e=>{ mouseX = toLogical(e).x; mouseIgnore = 0; });
window.addEventListener('pointerdown', e=>{
AudioSys.ensure();
mouseX = toLogical(e).x;
mouseDown = true;
mouseIgnore = 0;
action();
e.preventDefault();
});
window.addEventListener('pointerup', ()=>{ mouseDown = false; });
window.addEventListener('contextmenu', e=>e.preventDefault());
document.addEventListener('visibilitychange', ()=>{ if(document.hidden && state==='playing') state='paused'; });
window.addEventListener('blur', ()=>{ if(state==='playing') state='paused'; });
/* ============================ BOOT / LOOP ============================ */
buildLevel(0);
levelTime = 10; // show bricks behind the menu
let last = performance.now();
function frame(now){
requestAnimationFrame(frame);
let dt = (now-last)/1000;
last = now;
if(dt > 1/30) dt = 1/30;
update(dt);
render();
}
requestAnimationFrame(frame);
})();
</script>
</body>
</html>

