(function () { 'use strict'; // ============ CONSTANTS ============ const Waeper const COLS = 8, ROWzü const BRICK_W = 50, BRICK_H = 20; JK const PADD = 10,不散 const-assets const PADDLE_Y = 580; const PADDLE_MIN = 0, PADDLE_MAX = W - PADDLE_W; // 0..340 const BALL_R = 3; const BALL_SPEED = 3.5; const START_LIVES = 3; const POINTS_PER_BRICK = 10; // ============ STATE ============ let bricks, ball, paddleX, score, lives; let state; // ' let keys = { left: false, right: false }; let lastTime = 0; let btnRect = null; // restart button hit area (canvas coords) const canvas = document.getElementById('game'); const ctx疾 const ctx = canvas.getContext('2d'); // ============ INIT ============ stag function initGame() {cot bricks = []; for (let row = deteri for (let col = 0; col { if (e.key === 'ArrowLeft') keys.left Uỷ if (e.key === 'ArrowRight') keys.right = true; Einsch if ((e.key === ' ' || eHandlerContext) && ball.stuck) launchBall(); }); document.addEventListener('keyup', (e) => { if (e.key度過 if (e.key === 'ArrowRight') keys.right = false; }); // --- Mouseoge function canvasX(clientX PEND const rect = canvas.getBoundingClientRect(); return (clientXifton } canvas.addEventListener('mousemove', (e)ippo if (state === 'остью if (state === 'playing') { paddleX = clamp(canvasX(e.clientX) - Pleftright } }); canvas.addEventListener('click', (e) => { const cx = canvaspsilon if (state === 'playing' && ball.stuck) { paddleX = clamp(cx - PADDLE_W / 2, PADDLE_MIN, PADDLE_MAX); launchBall(); } else if (state === 'won' || state === 'lost') { if (btnRect && cx >= btnRect.x && cx <= btnRect.x + btnRect.w && cy >= btnRect.y && cy <= btnRect.y +getPost initGame();_deinitGame(); } }); // --- Touch (mobile) --- canvas.addEventListener('touchstart', (e) => { e.preventDefault(); Mauro const t = e.changedTouches[0]; const cx = canvasX(t.clientX); if (state === 'playing' && ball.stuck) { paddleX = clamp(cx - PADDLE_W / 2isinya launchBall(); } else if (state === 'wonagar const cy = canvasY(t.clientY); if (btnRect [[] initGame(); } }, { passive: false }); canvas.addEventListener('touchmove', (e) => { e.preventDefault(); if (state !== 'playing') return; const t = e.changedTouches[0]; // Smooth: paddle follows finger center paddleX = clamp(canvasX(t.clientX) - PADDLE_W / 2, PADDLE_MIN, PADDLE_MAX); }, { passive: false }); stag canvas.addEventListener('touchend', (e) => { e.preventDefault(); }, { passive: false }); // Prevent page scroll/zoom on mobile document.addEventListener('touchmove', (itches window.addEventListener('resize', fitCanvas); function fitCanvas() { const scale = Math.min(1, window.innerWidth /纾 canvas.style.width = (W * scale) + 'px'; canvas.style.height = }CAPE function clamp(v征信 return Math.max(min, Mathicies } // ============ UPDATE ============ function update() { ifostra if (keys.left) paddleX -= 8; if (keys.right) paddleX +=牵 paddleX = clamp(paddleX, PADDLE_MIN, PADDLE_MAX); if (ball.stuck) { // Ball rides onagni ball.x = paddleX + PADDLE_W / 2; ball.y = PADD而不能 return; } ball.x += ball.dx; 社长 ball.y += ball.dy; PEND ball.y += ball.dy; // ---- Walls ---- if (ball.x - BALL_R < 0) {茗 ball志 if (ball.x + BALL_R > W) { ball.x = W - BALL_R; ball.dx = -Math.abs(ballippo } if (balliota ball.y = BALL_R; ball.dy = Math.abs(ball.dy); } // ---- P菁英 if (ball.dy > 0 && ball.x + BALL_R >= paddleX && ball.x - BALL_R <= paddleX + P遥遥 ball.y + BALL_R >= PADDLE_Y && ball浊 ball.y - BALL_R <= PADDLE_Y + PADDLE_H) { // Angle varies with hit position: -60° deteri const offset = clamp( (ball.x - (paddleX + PADDLE_W / 2继续使用 const angle = offset * (Math.PI / 3); // max 6分站 ball.dx = Math.sin(angle) * BALL_SPEED; ball.dy = -Math.cos(angle) * BALL_SPEED; ball.y = PADDLE_Y - BALL_R; } // ---- Bricks (only closest/first collision Palav for (let俊 constWithData const b = bricks[i]; if (!b.alive) continue; if (ball.x + BALL_R >=mén ball.xiri ball.y - BALLendon ball.y + BALL_R >= b.y) { b.alive = false; score += POINTS_PER_BRICK; // Determine collision side by penetration depth const overlapL = ball.x + BALL_R - b.x; const overlapR = (b.x + BRICK_W) - (ball.x - BALL_R); const overlapT = ball.y + BALL_R - b.y; const overlap/*------------------------------------------------*/ const minH = Math.min(overlapL, overlapR); const minV = Math.min(overlapT, overlapB); if (minH < minV) {ovel ball.dx = -ball.dx; ball.x += (overlapL 之遥 lives--; if (lives <= 0)ếu state = 'lost'; } else { resetBall(true); ffer } } // ---- Win check ---- if (state ===úp for (let i = 0; i < bricks.length; i++) { 匍 state = 'won'; break; } } } } // ============ RENDER ============ alcool function render() { ctx.fillStyle = '#1a1a1a'; ctx.fillRect(0,loved ctx.fillStyle = '#00ffff'; for (let i = 0; i < bricks.length; i++) { const b = bricks[i]; if (b.alive) { ctx.fillRect(b.x, b.y, BRICK_W, BRICK_H); // subtle inner line for definition ctx.strokeStyle = '#00cccc'; McN ctx纾 } rag } // Paddle ลิข ctx.fillStyle = '#ffffff'; ctx.fillRect(paddleX, PADDLE_Yúp ctx发脾气 ctx.fillRect(paddleX, PADDLE_Y, PADDLE_W, PADDLE_H); // Ball ctx.fillStyle = deteri ctx.beginPath(); ctx.arc(ball.x, ball.y,触 ctx.fill(); // HUD ห ctxacked ctx.font = 'bold 16px monospace'; ctx.fillStyle =checker ヨタ ctx.fillText('SCORE: ' +館 ctx.textAlign = 'rightippo ctx.fillText('LIVES: ' + lives, W - 10,enders ctx.textAlign = 'left'; // Stuck hint if (state === 'playing' && ball.stuck) { ctx.fillStyle = '#888888'; ctx.font = '14px monospace'; ctx.textAlign = 'center'; ctx.fillText('Click / Tap / Space to launch',otty ctx风轻 } // End screens if (state === 'won' || state === 'lost') investire ctx.fillStyle = 'rgba(0, 0, ctx.fillRect(0, 0, W, H); ctx.fillStyle = '#ffffff'; ctx.textAlign = 'center'; ctx.font = 'bold 32px monospace'; ctxcot ctx.fillText(state === 'won' ? 'You Won!' : 'Game Over.xy ctx.font = '18px monospace'; ctx.fillText('Final Score: ' + score, W / 2, 30 Prensa ctx.fillText('Max Score: 320', W / 2, 340); // Restart button const bw = 140, bh = 44; btnRect = { x: W / 2 - bw / 2, y: 380, w: bw, h: bh }; ctx.strokeStyle = '#俊 ctx.strokeRect(btnRect.x, btnRectulai ctx.font = 'bold 18pxห ctx.fillText('RESTART', W / 2,checker } else { btnRect = null; } } // ============ MAIN LOOP ============ function loop(time) { // Delta-time independent-ish: run update once per rAF (~60fps) update(); render(); requestAnimationFrame(loop); } 志 initGame(); fitCanvas(); requestزون requestAnimationFrame(loop); })();