Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+
+ const paddleWidth = 100;
+ const paddleHeight = 10;
+ const ballRadius = 10;
+ const brickRowCount = 5;
+ const brickColumnCount = 7;
+ const brickWidth = 75;
+ const brickHeight = 20;
+ const brickPadding = 10;
+ const brickOffsetTop = 30;
+ const brickOffsetLeft = 30;
+
+ let x = canvas.width / 2;
+ let y = canvas.height - 30;
+ let dx = 2;
+ let dy = -2;
+ let paddleX = (canvas.width - paddleWidth) / 2;
+ let rightPressed = false;
+ let leftPressed = false;
+ let score = 0;
+ let lives = 3;
+
+ const bricks = [];
+ for (let c = 0; c < brickColumnCount; c++) {
+ bricks[c] = [];
+ for (let r = 0; r < brickRowCount; r++) {
+ bricks[c][r] = { x: 0, y: 0, status: 1 };
+ }
+ }
+
+ document.addEventListener('mousemove', mouseMoveHandler);
+
+ function mouseMoveHandler(e) {
+ const relativeX = e.clientX - canvas.offsetLeft;
+ if (relativeX > 0 && relativeX < canvas.width) {
+ paddleX = relativeX - paddleWidth / 2;
+ }
+ }
+
+ function drawBall() {
+ ctx.beginPath();
+ ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
+ ctx.fillStyle = '#fff';
+ ctx.fill();
+ ctx.closePath();
+ }
+
+ function drawPaddle() {
+ ctx.beginPath();
+ ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
+ ctx.fillStyle = '#fff';
+ ctx.fill();
+ ctx.closePath();
+ }
+
+ function drawBricks() {
+ for (let c = 0; c < brickColumnCount; c++) {
+ for (let r = 0; r < brickRowCount; r++) {
+ if (bricks[c][r].status === 1) {
+ const brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
+ const brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
+ bricks[c][r].x = brickX;
+ bricks[c][r].y = brickY;
+ ctx.beginPath();
+ ctx.rect(brickX, brickY, brickWidth, brickHeight);
+ ctx.fillStyle = `hsl(${c * 50}, 100%, 50%)`;
+ ctx.fill();
+ ctx.closePath();
+ }
+ }
+ }
+ }
+
+ function collisionDetection() {
+ for (let c = 0; c < brickColumnCount; c++) {
+ for (let r = 0; r < brickRowCount; r++) {
+ const b = bricks[c][r];
+ if (b.status === 1) {
+ if (x > b.x && x < b.x + brickWidth && y > b.y && y < b.y + brickHeight) {
+ dy = -dy;
+ b.status = 0;
+ score++;
+ if (score === brickRowCount * brickColumnCount) {
+ alert('YOU WIN, CONGRATULATIONS!');
+ document.location.reload();
+ }
+ }
+ }
+ }
+ }
+ }
+
+ function drawScore() {
+ ctx.font = '16px Arial';
+ ctx.fillStyle = '#fff';
+ ctx.fillText(`Score: ${score}`, 8, 20);
+ }
+
+ function drawLives() {
+ ctx.font = '16px Arial';
+ ctx.fillStyle = '#fff';
+ ctx.fillText(`Lives: ${lives}`, canvas.width - 65, 20);
+ }
+
+ function draw() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ drawBricks();
+ drawBall();
+ drawPaddle();
+ drawScore();
+ drawLives();
+ collisionDetection();
+
+ if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
+ dx = -dx;
+ }
+ if (y + dy < ballRadius) {
+ dy = -dy;
+ } else if (y + dy > canvas.height - ballRadius) {
+ if (x > paddleX && x < paddleX + paddleWidth) {
+ dy = -dy;
+ } else {
+ lives--;
+ if (!lives) {
+ alert('GAME OVER');
+ document.location.reload();
+ } else {
+ x = canvas.width / 2;
+ y = canvas.height - 30;
+ dx = 2;
+ dy = -2;
+ paddleX = (canvas.width - paddleWidth) / 2;
+ }
+ }
+ }
+
+ x += dx;
+ y += dy;
+ requestAnimationFrame(draw);
+ }
+
+ draw();