Run details
- Model
- google/gemini-3.8-flash
- Route
- OpenRouter API
- Served by
- Effort
- medium (OpenRouter default)
- Limits
- 60 min · 65,536 output tokens (model max)
- Date
- 24 Oct 2026
- Time
- 81 s
- Size
- 32 kB
- Tokens
- 78 in, 13,864 out (1,868 thinking)
- List price
- $0.057 at today's price ($0.825 in / $4.13 out per M)
- Paid
- $0.052
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
32 kB 1,222 lines Raw
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NEON BREAKER</title>
<style>
* {
box-sizing: border-box;
user-select: none;
-webkit-user-select: none;
margin: 0;
padding: 0;
}
body {
background: #090a10;
color: #fff;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow: hidden;
}
#game-container {
position: relative;
box-shadow: 0 0 50px rgba(0, 255, 200, 0.15), 0 0 100px rgba(255, 0, 128, 0.1);
border-radius: 12px;
overflow: hidden;
border: 2px solid #222538;
background: #0d0f18;
}
canvas {
display: block;
cursor: none;
}
.hud {
position: absolute;
top: 0;
left: 0;
width: 100%;
padding: 14px 20px;
display: flex;
justify-content: space-between;
align-items: center;
pointer-events: none;
font-weight: 700;
letter-spacing: 1px;
text-transform: uppercase;
font-size: 14px;
text-shadow: 0 2px 4px rgba(0,0,0,0.8);
z-index: 5;
}
.hud-left, .hud-center, .hud-right {
display: flex;
gap: 18px;
align-items: center;
}
.hud span {
color: #00ffcc;
margin-left: 4px;
font-size: 16px;
}
.hud .lives-icon {
color: #ff2a6d;
font-size: 16px;
filter: drop-shadow(0 0 6px #ff2a6d);
}
.overlay {
position: absolute;
inset: 0;
background: rgba(8, 10, 18, 0.85);
backdrop-filter: blur(6px);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 10;
transition: opacity 0.25s ease;
}
.overlay.hidden {
opacity: 0;
pointer-events: none;
}
.overlay h1 {
font-size: 48px;
font-weight: 900;
margin-bottom: 8px;
background: linear-gradient(45deg, #ff007f, #00f0ff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
letter-spacing: 4px;
text-shadow: 0 0 30px rgba(0, 240, 255, 0.4);
}
.overlay p {
color: #9ab;
font-size: 16px;
margin-bottom: 24px;
letter-spacing: 0.5px;
}
.btn {
background: linear-gradient(135deg, #00f0ff, #7000ff);
border: none;
color: #fff;
font-weight: 800;
padding: 14px 34px;
border-radius: 30px;
font-size: 15px;
letter-spacing: 2px;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 0 25px rgba(0, 240, 255, 0.5);
transition: transform 0.15s, box-shadow 0.15s;
}
.btn:hover {
transform: scale(1.05);
box-shadow: 0 0 35px rgba(112, 0, 255, 0.8);
}
.controls-hint {
margin-top: 18px;
font-size: 12px;
color: #556;
letter-spacing: 1px;
}
</style>
</head>
<body>
<div id="game-container">
<div class="hud">
<div class="hud-left">
<div>SCORE <span id="score-val">0</span></div>
<div>HIGH <span id="high-val">0</span></div>
</div>
<div class="hud-center">
<div>STAGE <span id="level-val">1</span></div>
</div>
<div class="hud-right">
<div id="lives-val"></div>
</div>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="screen-overlay" class="overlay">
<h1 id="overlay-title">NEON BREAKER</h1>
<p id="overlay-msg">Destroy all bricks. Chain hits for massive combos!</p>
<button id="start-btn" class="btn">PLAY</button>
<div class="controls-hint">MOUSE or ARROW / A-D KEYS • SPACE / CLICK TO LAUNCH</div>
</div>
</div>
<script>
(() => {
// --- SOUND SYNTHESIS ENGINE (Web Audio API) ---
const AudioCtx = window.AudioContext || window.webkitAudioContext;
let audioCtx = null;
function initAudio() {
if (!audioCtx) audioCtx = new AudioCtx();
if (audioCtx.state === 'suspended') audioCtx.resume();
}
function playSound(type, freq = 440, length = 0.1, slideFreq = null) {
if (!audioCtx) return;
try {
const now = audioCtx.currentTime;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, now);
if (slideFreq !== null) {
osc.frequency.exponentialRampToValueAtTime(Math.max(10, slideFreq), now + length);
}
gain.gain.setValueAtTime(0.2, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + length);
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.start(now);
osc.stop(now + length);
} catch (e) {}
}
function soundPaddleHit(speedFactor = 1) {
playSound('triangle', 220 + 80 * speedFactor, 0.08, 140);
}
function soundBrickHit(combo = 0) {
const base = 350;
const pitch = base * Math.pow(1.05, Math.min(combo, 25));
playSound('square', pitch, 0.06, pitch * 0.7);
}
function soundExplosion() {
if (!audioCtx) return;
try {
const now = audioCtx.currentTime;
const bufferSize = audioCtx.sampleRate * 0.3;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = (Math.random() * 2 - 1) * Math.exp(-i / (audioCtx.sampleRate * 0.08));
}
const noise = audioCtx.createBufferSource();
noise.buffer = buffer;
const filter = audioCtx.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.setValueAtTime(400, now);
filter.frequency.linearRampToValueAtTime(60, now + 0.3);
const gain = audioCtx.createGain();
gain.gain.setValueAtTime(0.4, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.3);
noise.connect(filter);
filter.connect(gain);
gain.connect(audioCtx.destination);
noise.start(now);
} catch(e) {}
}
function soundPowerup() {
playSound('sine', 520, 0.1, 880);
setTimeout(() => playSound('sine', 880, 0.15, 1320), 70);
}
function soundLaser() {
playSound('sawtooth', 880, 0.08, 150);
}
function soundLoseLife() {
playSound('sawtooth', 280, 0.4, 50);
}
function soundLevelWin() {
const notes = [440, 554, 659, 880];
notes.forEach((n, i) => {
setTimeout(() => playSound('triangle', n, 0.2, n * 1.05), i * 110);
});
}
// --- GAME CONSTANTS & STATE ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const W = canvas.width;
const H = canvas.height;
const scoreEl = document.getElementById('score-val');
const highEl = document.getElementById('high-val');
const levelEl = document.getElementById('level-val');
const livesEl = document.getElementById('lives-val');
const overlay = document.getElementById('screen-overlay');
const overlayTitle = document.getElementById('overlay-title');
const overlayMsg = document.getElementById('overlay-msg');
const startBtn = document.getElementById('start-btn');
let highScore = parseInt(localStorage.getItem('neon_breaker_hi') || '0', 10);
highEl.textContent = highScore;
const STATE_MENU = 0;
const STATE_PLAYING = 1;
const STATE_GAMEOVER = 2;
const STATE_WIN = 3;
let gameState = STATE_MENU;
let score = 0;
let lives = 3;
let currentLevel = 0;
let combo = 0;
let maxCombo = 0;
let screenShake = 0;
// Key controls
const keys = { left: false, right: false, space: false };
let mouseActive = false;
let mouseX = W / 2;
// --- LEVEL LAYOUTS ---
// Brick types:
// 1: Cyan (1 hit)
// 2: Pink (2 hits)
// 3: Gold (3 hits, heavy)
// E: Explosive (detonates neighbors)
// P: Powerup guaranteed
const LEVELS = [
// Level 1: Classic Rainbow
[
"............",
" 11111111 ",
" 11111111 ",
" 22222222 ",
" 22222222 ",
" 11111111 "
],
// Level 2: Castle & Explosives
[
" 2 22 2 ",
" 2E111111E2 ",
" 2111PP1112 ",
" 2221111222 ",
" 11111111 ",
" 1 1 "
],
// Level 3: Space Invader
[
" 1 1 ",
" 1 1 ",
" 111111 ",
" 11211211 ",
" 1111111111 ",
" 1 111111 1 ",
" 1 1 1 1 ",
" 1111 "
],
// Level 4: The Diamond Vault
[
" 33 ",
" 3EE3 ",
" 31PP13 ",
" 32111123 ",
" 321123 ",
" 3223 ",
" 33 "
],
// Level 5: The Master Grid
[
" 3213213213 ",
" 2E21P12E21 ",
" 1231231231 ",
" 3123123123 ",
" 2E21P12E21 ",
" 1231231231 "
]
];
// --- ENTITIES ---
const paddle = {
x: W / 2 - 50,
y: H - 36,
w: 100,
baseW: 100,
h: 12,
speed: 9,
targetX: W / 2 - 50,
laserTimer: 0,
wideTimer: 0,
vx: 0,
lastX: W / 2 - 50,
color: '#00f0ff'
};
let balls = [];
let bricks = [];
let particles = [];
let powerups = [];
let lasers = [];
let floatingTexts = [];
class Ball {
constructor(x, y, vx, vy) {
this.x = x;
this.y = y;
this.r = 6.5;
this.vx = vx || 0;
this.vy = vy || 0;
this.baseSpeed = 7.0;
this.speed = this.baseSpeed;
this.stuck = true;
this.fireballTimer = 0;
this.history = [];
}
launch() {
if (!this.stuck) return;
this.stuck = false;
const angle = -Math.PI / 2 + (Math.random() * 0.4 - 0.2);
this.speed = this.baseSpeed + currentLevel * 0.35;
this.vx = Math.sin(angle) * this.speed;
this.vy = -Math.cos(angle) * this.speed;
playSound('triangle', 360, 0.1, 520);
}
update() {
if (this.stuck) {
this.x = paddle.x + paddle.w / 2;
this.y = paddle.y - this.r - 1;
return;
}
this.history.unshift({ x: this.x, y: this.y });
if (this.history.length > 8) this.history.pop();
this.x += this.vx;
this.y += this.vy;
// Wall bounds
if (this.x - this.r < 0) {
this.x = this.r;
this.vx = Math.abs(this.vx);
soundPaddleHit(0.5);
addImpactSparks(this.x, this.y, '#00f0ff');
} else if (this.x + this.r > W) {
this.x = W - this.r;
this.vx = -Math.abs(this.vx);
soundPaddleHit(0.5);
addImpactSparks(this.x, this.y, '#00f0ff');
}
if (this.y - this.r < 0) {
this.y = this.r;
this.vy = Math.abs(this.vy);
soundPaddleHit(0.5);
addImpactSparks(this.x, this.y, '#00f0ff');
}
// Fireball timer
if (this.fireballTimer > 0) this.fireballTimer--;
}
draw() {
// Trail
for (let i = 0; i < this.history.length; i++) {
const pt = this.history[i];
const alpha = (1 - i / this.history.length) * 0.35;
ctx.beginPath();
ctx.arc(pt.x, pt.y, this.r * (1 - i / 12), 0, Math.PI * 2);
ctx.fillStyle = this.fireballTimer > 0 ? `rgba(255, 60, 0, ${alpha})` : `rgba(0, 240, 255, ${alpha})`;
ctx.fill();
}
// Body & Glow
ctx.save();
ctx.shadowBlur = 14;
ctx.shadowColor = this.fireballTimer > 0 ? '#ff4800' : '#00ffff';
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.fireballTimer > 0 ? '#fff055' : '#ffffff';
ctx.fill();
ctx.restore();
}
}
class Brick {
constructor(x, y, w, h, type) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.type = type; // 1, 2, 3, 'E', 'P'
this.hp = type === '3' ? 3 : (type === '2' ? 2 : 1);
this.maxHp = this.hp;
this.flash = 0;
this.destroyed = false;
}
getColor() {
if (this.type === 'E') return '#ff1744'; // Explosive red
if (this.type === 'P') return '#d500f9'; // Powerup purple
if (this.hp === 3) return '#ffea00'; // Gold
if (this.hp === 2) return '#ff007f'; // Neon pink
return '#00e5ff'; // Neon cyan
}
hit(isFireball = false) {
if (this.destroyed) return;
this.flash = 1.0;
const damage = isFireball ? 3 : 1;
this.hp -= damage;
if (this.hp <= 0) {
this.destroyed = true;
this.onBreak();
} else {
soundBrickHit(combo);
addImpactSparks(this.x + this.w / 2, this.y + this.h / 2, this.getColor(), 6);
}
}
onBreak() {
score += (this.maxHp * 50) + (combo * 15);
combo++;
if (combo > maxCombo) maxCombo = combo;
createExplosion(this.x + this.w / 2, this.y + this.h / 2, this.getColor(), 18);
addFloatingText(this.x + this.w / 2, this.y, `+${(this.maxHp * 50) + (combo * 15)}`, this.getColor());
if (this.type === 'E') {
soundExplosion();
triggerScreenShake(8);
detonateNeighbors(this);
} else {
soundBrickHit(combo);
}
// Drop powerup
const dropChance = this.type === 'P' ? 1.0 : 0.16;
if (Math.random() < dropChance) {
spawnPowerup(this.x + this.w / 2, this.y + this.h / 2);
}
}
draw() {
if (this.destroyed) return;
ctx.save();
const col = this.getColor();
ctx.shadowBlur = this.flash > 0 ? 16 : 6;
ctx.shadowColor = col;
if (this.flash > 0) {
ctx.fillStyle = '#ffffff';
this.flash -= 0.12;
} else {
ctx.fillStyle = col;
}
// Beveled rounded rectangle
const r = 4;
ctx.beginPath();
ctx.roundRect(this.x, this.y, this.w, this.h, r);
ctx.fill();
// Inner highlight
ctx.fillStyle = 'rgba(255, 255, 255, 0.22)';
ctx.fillRect(this.x + 2, this.y + 2, this.w - 4, 3);
// Icon for specials
if (this.type === 'E') {
ctx.fillStyle = '#fff';
ctx.font = 'bold 11px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('💣', this.x + this.w / 2, this.y + this.h / 2);
} else if (this.type === 'P') {
ctx.fillStyle = '#fff';
ctx.font = 'bold 12px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('★', this.x + this.w / 2, this.y + this.h / 2);
} else if (this.hp > 1) {
ctx.fillStyle = 'rgba(0,0,0,0.4)';
ctx.font = 'bold 10px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(this.hp, this.x + this.w / 2, this.y + this.h / 2);
}
ctx.restore();
}
}
function detonateNeighbors(brick) {
const range = 70;
const cx = brick.x + brick.w / 2;
const cy = brick.y + brick.h / 2;
for (let b of bricks) {
if (!b.destroyed && b !== brick) {
const bcx = b.x + b.w / 2;
const bcy = b.y + b.h / 2;
const d = Math.hypot(cx - bcx, cy - bcy);
if (d < range) {
setTimeout(() => {
if (!b.destroyed) b.hit(true);
}, 40);
}
}
}
}
class PowerUp {
constructor(x, y, type) {
this.x = x;
this.y = y;
this.w = 26;
this.h = 16;
this.vy = 2.4;
this.type = type; // 'WIDE', 'MULTI', 'FIRE', 'LASER', 'LIFE'
}
update() {
this.y += this.vy;
}
draw() {
ctx.save();
let color = '#00f0ff';
let label = 'W';
if (this.type === 'MULTI') { color = '#39ff14'; label = '3x'; }
if (this.type === 'FIRE') { color = '#ff3300'; label = '🔥'; }
if (this.type === 'LASER') { color = '#ff0055'; label = '⚡'; }
if (this.type === 'LIFE') { color = '#ff00aa'; label = '♥'; }
ctx.shadowBlur = 10;
ctx.shadowColor = color;
ctx.fillStyle = color;
ctx.beginPath();
ctx.roundRect(this.x - this.w / 2, this.y - this.h / 2, this.w, this.h, 6);
ctx.fill();
ctx.fillStyle = '#0a0d18';
ctx.font = 'bold 11px sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(label, this.x, this.y);
ctx.restore();
}
}
class Laser {
constructor(x, y) {
this.x = x;
this.y = y;
this.vy = -12;
this.w = 3;
this.h = 14;
}
update() {
this.y += this.vy;
}
draw() {
ctx.save();
ctx.shadowBlur = 8;
ctx.shadowColor = '#ff0055';
ctx.fillStyle = '#ff77aa';
ctx.fillRect(this.x - this.w / 2, this.y, this.w, this.h);
ctx.restore();
}
}
class Particle {
constructor(x, y, color, vx, vy, size, life) {
this.x = x;
this.y = y;
this.color = color;
this.vx = vx;
this.vy = vy;
this.size = size;
this.maxLife = life || 35;
this.life = this.maxLife;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.vy += 0.08; // gravity
this.vx *= 0.98;
this.life--;
}
draw() {
const alpha = Math.max(0, this.life / this.maxLife);
ctx.save();
ctx.globalAlpha = alpha;
ctx.fillStyle = this.color;
ctx.shadowBlur = 6;
ctx.shadowColor = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, Math.max(0.5, this.size * alpha), 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
class FloatingText {
constructor(x, y, text, color) {
this.x = x;
this.y = y;
this.text = text;
this.color = color;
this.life = 45;
}
update() {
this.y -= 0.8;
this.life--;
}
draw() {
const alpha = Math.min(1, this.life / 30);
ctx.save();
ctx.globalAlpha = alpha;
ctx.font = 'bold 13px sans-serif';
ctx.textAlign = 'center';
ctx.fillStyle = this.color;
ctx.shadowBlur = 6;
ctx.shadowColor = this.color;
ctx.fillText(this.text, this.x, this.y);
ctx.restore();
}
}
// --- SPAWNERS & FX ---
function triggerScreenShake(amount) {
screenShake = Math.max(screenShake, amount);
}
function createExplosion(x, y, color, count = 16) {
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const spd = 1.5 + Math.random() * 5.5;
particles.push(new Particle(
x, y, color,
Math.cos(angle) * spd,
Math.sin(angle) * spd,
2 + Math.random() * 2.5,
25 + Math.random() * 20
));
}
}
function addImpactSparks(x, y, color, count = 4) {
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const spd = 1 + Math.random() * 3;
particles.push(new Particle(
x, y, color,
Math.cos(angle) * spd,
Math.sin(angle) * spd,
1.5,
15 + Math.random() * 10
));
}
}
function addFloatingText(x, y, text, color = '#fff') {
floatingTexts.push(new FloatingText(x, y, text, color));
}
function spawnPowerup(x, y) {
const roll = Math.random();
let type = 'WIDE';
if (roll < 0.25) type = 'MULTI';
else if (roll < 0.50) type = 'FIRE';
else if (roll < 0.75) type = 'LASER';
else if (roll < 0.88) type = 'WIDE';
else type = 'LIFE';
powerups.push(new PowerUp(x, y, type));
}
function applyPowerup(type) {
soundPowerup();
triggerScreenShake(3);
if (type === 'WIDE') {
paddle.wideTimer = 600; // ~10s
paddle.w = paddle.baseW * 1.45;
addFloatingText(paddle.x + paddle.w / 2, paddle.y - 20, 'EXPANDED!', '#00f0ff');
} else if (type === 'MULTI') {
const newBalls = [];
balls.forEach(b => {
if (!b.stuck) {
for (let k = -1; k <= 1; k += 2) {
const nb = new Ball(b.x, b.y, b.vx * 0.9 + k * 2, b.vy * 0.9);
nb.stuck = false;
nb.speed = b.speed;
newBalls.push(nb);
}
}
});
balls.push(...newBalls);
addFloatingText(paddle.x + paddle.w / 2, paddle.y - 20, 'MULTI-BALL!', '#39ff14');
} else if (type === 'FIRE') {
balls.forEach(b => b.fireballTimer = 480);
addFloatingText(paddle.x + paddle.w / 2, paddle.y - 20, 'FIREBALL!', '#ff3300');
} else if (type === 'LASER') {
paddle.laserTimer = 500;
addFloatingText(paddle.x + paddle.w / 2, paddle.y - 20, 'LASERS ACTIVE!', '#ff0055');
} else if (type === 'LIFE') {
lives++;
updateLivesDisplay();
addFloatingText(paddle.x + paddle.w / 2, paddle.y - 20, '+1 LIFE!', '#ff00aa');
}
}
// --- LEVEL SETUP ---
function loadLevel(idx) {
currentLevel = idx;
levelEl.textContent = (currentLevel + 1);
bricks = [];
powerups = [];
lasers = [];
combo = 0;
const layout = LEVELS[currentLevel % LEVELS.length];
const rows = layout.length;
const cols = layout[0].length;
const padding = 6;
const marginTop = 65;
const availableW = W - 60;
const brickW = (availableW - (cols - 1) * padding) / cols;
const brickH = 20;
const startX = 30;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const char = layout[r][c];
if (char !== ' ' && char !== '.') {
const bx = startX + c * (brickW + padding);
const by = marginTop + r * (brickH + padding);
bricks.push(new Brick(bx, by, brickW, brickH, char));
}
}
}
// Reset paddle & spawn initial ball
paddle.x = W / 2 - paddle.w / 2;
paddle.targetX = paddle.x;
paddle.wideTimer = 0;
paddle.laserTimer = 0;
paddle.w = paddle.baseW;
balls = [new Ball(paddle.x + paddle.w / 2, paddle.y - 8, 0, 0)];
}
function updateLivesDisplay() {
livesEl.innerHTML = '';
for (let i = 0; i < lives; i++) {
const heart = document.createElement('span');
heart.className = 'lives-icon';
heart.textContent = '♥';
livesEl.appendChild(heart);
}
}
function startGame() {
initAudio();
score = 0;
lives = 3;
maxCombo = 0;
scoreEl.textContent = '0';
updateLivesDisplay();
overlay.classList.add('hidden');
loadLevel(0);
gameState = STATE_PLAYING;
}
function gameOver() {
gameState = STATE_GAMEOVER;
if (score > highScore) {
highScore = score;
localStorage.setItem('neon_breaker_hi', highScore);
highEl.textContent = highScore;
}
overlayTitle.textContent = 'GAME OVER';
overlayMsg.textContent = `FINAL SCORE: ${score} • MAX COMBO: ${maxCombo}x`;
startBtn.textContent = 'PLAY AGAIN';
overlay.classList.remove('hidden');
}
function gameWin() {
gameState = STATE_WIN;
soundLevelWin();
if (score > highScore) {
highScore = score;
localStorage.setItem('neon_breaker_hi', highScore);
highEl.textContent = highScore;
}
overlayTitle.textContent = 'VICTORY!';
overlayMsg.textContent = `YOU CLEARED ALL SECTORS! SCORE: ${score}`;
startBtn.textContent = 'PLAY AGAIN';
overlay.classList.remove('hidden');
}
// --- INPUT LISTENERS ---
window.addEventListener('keydown', (e) => {
initAudio();
if (e.code === 'ArrowLeft' || e.code === 'KeyA') keys.left = true;
if (e.code === 'ArrowRight' || e.code === 'KeyD') keys.right = true;
if (e.code === 'Space') {
keys.space = true;
if (gameState === STATE_PLAYING) {
balls.forEach(b => { if (b.stuck) b.launch(); });
}
}
mouseActive = false; // Keyboard overrides mouse until moved
});
window.addEventListener('keyup', (e) => {
if (e.code === 'ArrowLeft' || e.code === 'KeyA') keys.left = false;
if (e.code === 'ArrowRight' || e.code === 'KeyD') keys.right = false;
if (e.code === 'Space') keys.space = false;
});
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
const scaleX = canvas.width / rect.width;
mouseX = (e.clientX - rect.left) * scaleX;
mouseActive = true;
});
canvas.addEventListener('mousedown', () => {
initAudio();
if (gameState === STATE_PLAYING) {
balls.forEach(b => { if (b.stuck) b.launch(); });
}
});
startBtn.addEventListener('click', () => {
startGame();
});
// --- LASER FIRING TICK ---
let laserCooldown = 0;
// --- UPDATE & DRAW ---
function update() {
if (gameState !== STATE_PLAYING) return;
// Paddle Movement
paddle.lastX = paddle.x;
if (mouseActive) {
paddle.targetX = mouseX - paddle.w / 2;
paddle.x += (paddle.targetX - paddle.x) * 0.35;
} else {
if (keys.left) paddle.x -= paddle.speed;
if (keys.right) paddle.x += paddle.speed;
}
// Clamp paddle
paddle.x = Math.max(8, Math.min(W - paddle.w - 8, paddle.x));
paddle.vx = paddle.x - paddle.lastX;
// Powerup Timers on Paddle
if (paddle.wideTimer > 0) {
paddle.wideTimer--;
if (paddle.wideTimer === 0) paddle.w = paddle.baseW;
}
if (paddle.laserTimer > 0) {
paddle.laserTimer--;
laserCooldown--;
if (laserCooldown <= 0) {
soundLaser();
lasers.push(new Laser(paddle.x + 10, paddle.y));
lasers.push(new Laser(paddle.x + paddle.w - 10, paddle.y));
laserCooldown = 18; // Fire interval
}
}
// Update Lasers
for (let i = lasers.length - 1; i >= 0; i--) {
const l = lasers[i];
l.update();
if (l.y < 0) {
lasers.splice(i, 1);
continue;
}
// Check brick collision
for (let b of bricks) {
if (!b.destroyed && l.x > b.x && l.x < b.x + b.w && l.y > b.y && l.y < b.y + b.h) {
b.hit(false);
lasers.splice(i, 1);
break;
}
}
}
// Update Powerup Drops
for (let i = powerups.length - 1; i >= 0; i--) {
const p = powerups[i];
p.update();
// Catch by paddle
if (p.y + p.h / 2 >= paddle.y && p.y - p.h / 2 <= paddle.y + paddle.h &&
p.x + p.w / 2 >= paddle.x && p.x - p.w / 2 <= paddle.x + paddle.w) {
applyPowerup(p.type);
powerups.splice(i, 1);
continue;
}
if (p.y > H + 20) powerups.splice(i, 1);
}
// Update Balls
for (let i = balls.length - 1; i >= 0; i--) {
const b = balls[i];
b.update();
if (b.stuck) continue;
// Ball / Paddle Collision
if (b.vy > 0 &&
b.y + b.r >= paddle.y &&
b.y - b.r <= paddle.y + paddle.h &&
b.x >= paddle.x - 4 &&
b.x <= paddle.x + paddle.w + 4) {
// Hit pos ratio from -1 to 1
const hitOffset = (b.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2);
const maxAngle = Math.PI * 0.40; // ~72 degrees
const newAngle = hitOffset * maxAngle;
// Add a slight speed increase on good paddle bounce
b.speed = Math.min(13.5, b.speed + 0.05);
b.vx = Math.sin(newAngle) * b.speed + paddle.vx * 0.15;
b.vy = -Math.cos(newAngle) * b.speed;
b.y = paddle.y - b.r - 0.5;
// Reset combo counter when hitting paddle
combo = 0;
soundPaddleHit(1.2);
addImpactSparks(b.x, b.y, '#00f0ff', 5);
triggerScreenShake(2);
}
// Ball / Brick Collision
let hitAny = false;
for (let br of bricks) {
if (br.destroyed) continue;
// Nearest point on rect
const nearestX = Math.max(br.x, Math.min(b.x, br.x + br.w));
const nearestY = Math.max(br.y, Math.min(b.y, br.y + br.h));
const distSq = (b.x - nearestX) ** 2 + (b.y - nearestY) ** 2;
if (distSq < b.r * b.r) {
const isFire = b.fireballTimer > 0;
br.hit(isFire);
if (!isFire) {
// Determine collision edge
const overlapLeft = (b.x + b.r) - br.x;
const overlapRight = (br.x + br.w) - (b.x - b.r);
const overlapTop = (b.y + b.r) - br.y;
const overlapBottom = (br.y + br.h) - (b.y - b.r);
const minOverlapX = Math.min(overlapLeft, overlapRight);
const minOverlapY = Math.min(overlapTop, overlapBottom);
if (minOverlapX < minOverlapY) {
b.vx = -b.vx;
b.x += b.vx > 0 ? minOverlapX : -minOverlapX;
} else {
b.vy = -b.vy;
b.y += b.vy > 0 ? minOverlapY : -minOverlapY;
}
}
hitAny = true;
break; // Avoid multi-penetration anomalies per frame
}
}
// Ball falls below screen
if (b.y - b.r > H) {
balls.splice(i, 1);
}
}
// Check ball drain
if (balls.length === 0) {
lives--;
combo = 0;
updateLivesDisplay();
soundLoseLife();
triggerScreenShake(8);
if (lives <= 0) {
gameOver();
} else {
// Reset single ball on paddle
paddle.wideTimer = 0;
paddle.laserTimer = 0;
paddle.w = paddle.baseW;
balls = [new Ball(paddle.x + paddle.w / 2, paddle.y - 8, 0, 0)];
}
}
// Level completion check
const remainingBricks = bricks.filter(b => !b.destroyed);
if (remainingBricks.length === 0) {
soundLevelWin();
if (currentLevel + 1 < LEVELS.length) {
loadLevel(currentLevel + 1);
} else {
gameWin();
}
}
// Update Particles
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].update();
if (particles[i].life <= 0) particles.splice(i, 1);
}
// Update Floating Texts
for (let i = floatingTexts.length - 1; i >= 0; i--) {
floatingTexts[i].update();
if (floatingTexts[i].life <= 0) floatingTexts.splice(i, 1);
}
// Score DOM update
scoreEl.textContent = score;
}
// --- DRAW SYSTEM ---
function drawBackground() {
// Background gradient
const bgGrad = ctx.createLinearGradient(0, 0, 0, H);
bgGrad.addColorStop(0, '#0d0f1a');
bgGrad.addColorStop(1, '#05060b');
ctx.fillStyle = bgGrad;
ctx.fillRect(0, 0, W, H);
// Subtle neon grid
ctx.strokeStyle = 'rgba(0, 240, 255, 0.025)';
ctx.lineWidth = 1;
const gridSize = 40;
for (let x = 0; x < W; x += gridSize) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, H);
ctx.stroke();
}
for (let y = 0; y < H; y += gridSize) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(W, y);
ctx.stroke();
}
}
function drawPaddle() {
ctx.save();
const pCol = paddle.laserTimer > 0 ? '#ff0055' : (paddle.wideTimer > 0 ? '#00ffcc' : '#00bfff');
ctx.shadowBlur = 15;
ctx.shadowColor = pCol;
// Body
ctx.fillStyle = pCol;
ctx.beginPath();
ctx.roundRect(paddle.x, paddle.y, paddle.w, paddle.h, 6);
ctx.fill();
// Metallic center core
ctx.fillStyle = '#ffffff';
ctx.beginPath();
ctx.roundRect(paddle.x + paddle.w / 2 - 16, paddle.y + 2, 32, paddle.h - 4, 3);
ctx.fill();
// Laser cannons
if (paddle.laserTimer > 0) {
ctx.fillStyle = '#ff0055';
ctx.fillRect(paddle.x + 4, paddle.y - 4, 6, 6);
ctx.fillRect(paddle.x + paddle.w - 10, paddle.y - 4, 6, 6);
}
ctx.restore();
}
function drawCombo() {
if (combo > 1) {
ctx.save();
ctx.font = '900 22px sans-serif';
ctx.textAlign = 'right';
ctx.fillStyle = combo > 5 ? '#ff007f' : '#00f0ff';
ctx.shadowBlur = 10;
ctx.shadowColor = ctx.fillStyle;
ctx.fillText(`${combo}x COMBO`, W - 25, H - 25);
ctx.restore();
}
}
function render() {
ctx.save();
// Screen Shake Offset
if (screenShake > 0) {
const shakeX = (Math.random() - 0.5) * screenShake * 2;
const shakeY = (Math.random() - 0.5) * screenShake * 2;
ctx.translate(shakeX, shakeY);
screenShake *= 0.88;
if (screenShake < 0.2) screenShake = 0;
}
drawBackground();
// Bricks
for (let b of bricks) b.draw();
// Powerups
for (let p of powerups) p.draw();
// Lasers
for (let l of lasers) l.draw();
// Paddle
drawPaddle();
// Balls
for (let b of balls) b.draw();
// Particles
for (let pt of particles) pt.draw();
// Floating text
for (let ft of floatingTexts) ft.draw();
// Combo display
drawCombo();
ctx.restore();
}
// --- GAME LOOP ---
function loop() {
update();
render();
requestAnimationFrame(loop);
}
// Initial draw
drawBackground();
updateLivesDisplay();
requestAnimationFrame(loop);
})();
</script>
</body>
</html>

