Changed around line 1
+ const paddle = document.getElementById('paddle');
+ const ball = document.getElementById('ball');
+ const bricks = document.getElementById('bricks');
+ const startButton = document.getElementById('startButton');
+ const scoreDisplay = document.getElementById('score');
+
+ let paddleX = 350;
+ let ballX = 400;
+ let ballY = 300;
+ let ballDX = 4;
+ let ballDY = -4;
+ let score = 0;
+ let gameInterval;
+
+ const brickRowCount = 5;
+ const brickColumnCount = 10;
+ const brickWidth = 75;
+ const brickHeight = 20;
+ const brickPadding = 10;
+ const brickOffsetTop = 30;
+ const brickOffsetLeft = 30;
+
+ const brickLayout = [];
+
+ for (let c = 0; c < brickColumnCount; c++) {
+ brickLayout[c] = [];
+ for (let r = 0; r < brickRowCount; r++) {
+ brickLayout[c][r] = { x: 0, y: 0, status: 1 };
+ }
+ }
+
+ function drawBricks() {
+ bricks.innerHTML = '';
+ for (let c = 0; c < brickColumnCount; c++) {
+ for (let r = 0; r < brickRowCount; r++) {
+ if (brickLayout[c][r].status === 1) {
+ const brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
+ const brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
+ brickLayout[c][r].x = brickX;
+ brickLayout[c][r].y = brickY;
+ const brick = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
+ brick.setAttribute('x', brickX);
+ brick.setAttribute('y', brickY);
+ brick.setAttribute('width', brickWidth);
+ brick.setAttribute('height', brickHeight);
+ bricks.appendChild(brick);
+ }
+ }
+ }
+ }
+
+ function movePaddle(event) {
+ const gameArea = document.getElementById('gameArea');
+ const rect = gameArea.getBoundingClientRect();
+ paddleX = event.clientX - rect.left - paddle.getAttribute('width') / 2;
+ if (paddleX < 0) paddleX = 0;
+ if (paddleX > 700) paddleX = 700;
+ paddle.setAttribute('x', paddleX);
+ }
+
+ function moveBall() {
+ ballX += ballDX;
+ ballY += ballDY;
+
+ if (ballX < 10 || ballX > 790) ballDX = -ballDX;
+ if (ballY < 10) ballDY = -ballDY;
+
+ if (ballY > 540) {
+ if (ballX > paddleX && ballX < paddleX + 100) {
+ ballDY = -ballDY;
+ } else {
+ clearInterval(gameInterval);
+ alert('Game Over! Your score: ' + score);
+ resetGame();
+ }
+ }
+
+ ball.setAttribute('cx', ballX);
+ ball.setAttribute('cy', ballY);
+
+ collisionDetection();
+ }
+
+ function collisionDetection() {
+ for (let c = 0; c < brickColumnCount; c++) {
+ for (let r = 0; r < brickRowCount; r++) {
+ const b = brickLayout[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++;
+ scoreDisplay.textContent = 'Score: ' + score;
+ if (score === brickRowCount * brickColumnCount) {
+ alert('You Win!');
+ resetGame();
+ }
+ }
+ }
+ }
+ }
+ }
+
+ function resetGame() {
+ ballX = 400;
+ ballY = 300;
+ ballDX = 4;
+ ballDY = -4;
+ score = 0;
+ scoreDisplay.textContent = 'Score: 0';
+ for (let c = 0; c < brickColumnCount; c++) {
+ for (let r = 0; r < brickRowCount; r++) {
+ brickLayout[c][r].status = 1;
+ }
+ }
+ drawBricks();
+ }
+
+ startButton.addEventListener('click', () => {
+ resetGame();
+ gameInterval = setInterval(moveBall, 10);
+ });
+
+ document.addEventListener('mousemove', movePaddle);
+
+ drawBricks();