Changed around line 1
+ const paddle1 = document.getElementById('paddle1');
+ const paddle2 = document.getElementById('paddle2');
+ const ball = document.getElementById('ball');
+ const player1Score = document.getElementById('player1-score');
+ const player2Score = document.getElementById('player2-score');
+
+ let paddle1Y = 160;
+ let paddle2Y = 160;
+ let ballX = 392.5;
+ let ballY = 192.5;
+ let ballSpeedX = 4;
+ let ballSpeedY = 4;
+ let score1 = 0;
+ let score2 = 0;
+
+ document.addEventListener('keydown', (e) => {
+ if (e.key === 'w' && paddle1Y > 0) {
+ paddle1Y -= 20;
+ } else if (e.key === 's' && paddle1Y < 320) {
+ paddle1Y += 20;
+ } else if (e.key === 'o' && paddle2Y > 0) {
+ paddle2Y -= 20;
+ } else if (e.key === 'l' && paddle2Y < 320) {
+ paddle2Y += 20;
+ }
+ paddle1.style.top = paddle1Y + 'px';
+ paddle2.style.top = paddle2Y + 'px';
+ });
+
+ function updateBall() {
+ ballX += ballSpeedX;
+ ballY += ballSpeedY;
+
+ if (ballY <= 0 || ballY >= 385) {
+ ballSpeedY = -ballSpeedY;
+ }
+
+ if (ballX <= 20 && ballY >= paddle1Y && ballY <= paddle1Y + 80) {
+ ballSpeedX = -ballSpeedX;
+ }
+
+ if (ballX >= 765 && ballY >= paddle2Y && ballY <= paddle2Y + 80) {
+ ballSpeedX = -ballSpeedX;
+ }
+
+ if (ballX <= 0) {
+ score2++;
+ player2Score.textContent = score2;
+ resetBall();
+ }
+
+ if (ballX >= 785) {
+ score1++;
+ player1Score.textContent = score1;
+ resetBall();
+ }
+
+ ball.style.left = ballX + 'px';
+ ball.style.top = ballY + 'px';
+ }
+
+ function resetBall() {
+ ballX = 392.5;
+ ballY = 192.5;
+ ballSpeedX = -ballSpeedX;
+ }
+
+ setInterval(updateBall, 16);