const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreSpan = document.getElementById('score-value'); const restartBtn = document.getElementById('restart-btn'); const gameOverDiv = document.getElementById('game-over-message'); const ballRadius = 8; let x, y, dx, dy; const paddleHeight = 12, paddleWidth = 75; let paddleX; const brickRowCount = 5, brickColumnCount = 7; const brickWidth = 60, brickHeight = 20, brickPadding = 10, brickOffsetTop = 30, brickOffsetLeft = 30; let bricks = []; let score, gameOver; function setupBricks() { bricks = []; for(let c=0; c 0 && relativeX < canvas.width) { paddleX = relativeX - paddleWidth/2; if(paddleX < 0) paddleX = 0; if(paddleX + paddleWidth > canvas.width) paddleX = canvas.width - paddleWidth; } } function collisionDetection() { for(let c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { dy = -dy; b.status = 0; score++; scoreSpan.textContent = score; if(score == brickRowCount*brickColumnCount) { gameOver = true; gameOverDiv.textContent = "¡Ganaste! 🏆"; return; } } } } } } function drawBall() { ctx.beginPath(); ctx.arc(x, y, ballRadius, 0, Math.PI*2); ctx.fillStyle = "#f9d923"; ctx.fill(); ctx.closePath(); } function drawPaddle() { ctx.beginPath(); ctx.rect(paddleX, canvas.height-paddleHeight-5, paddleWidth, paddleHeight); ctx.fillStyle = "#e94560"; ctx.fill(); ctx.closePath(); } function drawBricks() { for(let c=0; c canvas.width-ballRadius || x + dx < ballRadius) { dx = -dx; } if(y + dy < ballRadius) { dy = -dy; } else if(y + dy > canvas.height-ballRadius-paddleHeight-5) { // Rebote en la paleta if(x > paddleX && x < paddleX + paddleWidth) { dy = -dy; } else if (y + dy > canvas.height-ballRadius) { // GAME OVER gameOver = true; gameOverDiv.textContent = "¡Game Over! 😢"; return; } } x += dx; y += dy; if(!gameOver) { requestAnimationFrame(draw); } } restartBtn.onclick = function() { document.removeEventListener("mousemove", mouseMoveHandler); startGame(); }; // Inicio startGame();