Changed around line 1
+ const canvas = document.getElementById('gameCanvas');
+ const ctx = canvas.getContext('2d');
+
+ const paddleWidth = 100;
+ const paddleHeight = 10;
+ const paddleSpeed = 8;
+ 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 paddleX = (canvas.width - paddleWidth) / 2;
+ let ballX = canvas.width / 2;
+ let ballY = canvas.height - 30;
+ let ballDX = 4;
+ let ballDY = -4;
+ let rightPressed = false;
+ let leftPressed = false;
+ let score = 0;
+ let 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', (e) => {
+ const relativeX = e.clientX - canvas.offsetLeft;
+ if (relativeX > 0 && relativeX < canvas.width) {
+ paddleX = relativeX - paddleWidth / 2;
+ }
+ });
+
+ function drawBall() {
+ ctx.beginPath();
+ ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2);
+ ctx.fillStyle = '#ff4757';
+ ctx.fill();
+ ctx.closePath();
+ }
+
+ function drawPaddle() {
+ ctx.beginPath();
+ ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
+ ctx.fillStyle = '#2ed573';
+ 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 = '#ffa502';
+ 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 (ballX > b.x && ballX < b.x + brickWidth && ballY > b.y && ballY < b.y + brickHeight) {
+ ballDY = -ballDY;
+ 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 draw() {
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ drawBricks();
+ drawBall();
+ drawPaddle();
+ drawScore();
+ collisionDetection();
+
+ if (ballX + ballDX > canvas.width - ballRadius || ballX + ballDX < ballRadius) {
+ ballDX = -ballDX;
+ }
+ if (ballY + ballDY < ballRadius) {
+ ballDY = -ballDY;
+ } else if (ballY + ballDY > canvas.height - ballRadius) {
+ if (ballX > paddleX && ballX < paddleX + paddleWidth) {
+ ballDY = -ballDY;
+ } else {
+ alert('GAME OVER');
+ document.location.reload();
+ }
+ }
+
+ ballX += ballDX;
+ ballY += ballDY;
+ requestAnimationFrame(draw);
+ }
+
+ draw();