Update script.js
Browse files
script.js
CHANGED
@@ -21,17 +21,11 @@ gameArea.addEventListener('touchmove', (e) => {
|
|
21 |
e.preventDefault();
|
22 |
touchY = e.touches[0].clientY;
|
23 |
|
24 |
-
// Move
|
25 |
if (e.touches[0].clientX < window.innerWidth / 2) {
|
26 |
-
// Left paddle
|
27 |
paddleLeftY = touchY - paddleHeight / 2;
|
28 |
if (paddleLeftY < 0) paddleLeftY = 0;
|
29 |
if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
|
30 |
-
} else {
|
31 |
-
// Right paddle
|
32 |
-
paddleRightY = touchY - paddleHeight / 2;
|
33 |
-
if (paddleRightY < 0) paddleRightY = 0;
|
34 |
-
if (paddleRightY > window.innerHeight - paddleHeight) paddleRightY = window.innerHeight - paddleHeight;
|
35 |
}
|
36 |
});
|
37 |
|
@@ -45,18 +39,19 @@ function update() {
|
|
45 |
ballSpeedY = -ballSpeedY;
|
46 |
}
|
47 |
|
48 |
-
// Ball collision with
|
49 |
if (
|
50 |
ballX <= paddleWidth + 10 && // Left paddle
|
51 |
-
ballY >= paddleLeftY &&
|
52 |
ballY <= paddleLeftY + paddleHeight
|
53 |
) {
|
54 |
ballSpeedX = -ballSpeedX;
|
55 |
}
|
56 |
|
|
|
57 |
if (
|
58 |
-
ballX >= window.innerWidth - paddleWidth -
|
59 |
-
ballY >= paddleRightY &&
|
60 |
ballY <= paddleRightY + paddleHeight
|
61 |
) {
|
62 |
ballSpeedX = -ballSpeedX;
|
@@ -67,6 +62,19 @@ function update() {
|
|
67 |
resetBall();
|
68 |
}
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
// Update ball and paddle positions
|
71 |
ball.style.left = ballX + 'px';
|
72 |
ball.style.top = ballY + 'px';
|
|
|
21 |
e.preventDefault();
|
22 |
touchY = e.touches[0].clientY;
|
23 |
|
24 |
+
// Move left paddle based on touch position
|
25 |
if (e.touches[0].clientX < window.innerWidth / 2) {
|
|
|
26 |
paddleLeftY = touchY - paddleHeight / 2;
|
27 |
if (paddleLeftY < 0) paddleLeftY = 0;
|
28 |
if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
});
|
31 |
|
|
|
39 |
ballSpeedY = -ballSpeedY;
|
40 |
}
|
41 |
|
42 |
+
// Ball collision with left paddle
|
43 |
if (
|
44 |
ballX <= paddleWidth + 10 && // Left paddle
|
45 |
+
ballY + ballSize >= paddleLeftY &&
|
46 |
ballY <= paddleLeftY + paddleHeight
|
47 |
) {
|
48 |
ballSpeedX = -ballSpeedX;
|
49 |
}
|
50 |
|
51 |
+
// Ball collision with right paddle
|
52 |
if (
|
53 |
+
ballX >= window.innerWidth - paddleWidth - 25 && // Right paddle
|
54 |
+
ballY + ballSize >= paddleRightY &&
|
55 |
ballY <= paddleRightY + paddleHeight
|
56 |
) {
|
57 |
ballSpeedX = -ballSpeedX;
|
|
|
62 |
resetBall();
|
63 |
}
|
64 |
|
65 |
+
// Bot logic for right paddle
|
66 |
+
if (ballSpeedX > 0) { // Ball is moving towards the right paddle
|
67 |
+
if (paddleRightY + paddleHeight / 2 < ballY) {
|
68 |
+
paddleRightY += 3; // Move paddle down
|
69 |
+
} else if (paddleRightY + paddleHeight / 2 > ballY) {
|
70 |
+
paddleRightY -= 3; // Move paddle up
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
// Ensure the right paddle stays within bounds
|
75 |
+
if (paddleRightY < 0) paddleRightY = 0;
|
76 |
+
if (paddleRightY > window.innerHeight - paddleHeight) paddleRightY = window.innerHeight - paddleHeight;
|
77 |
+
|
78 |
// Update ball and paddle positions
|
79 |
ball.style.left = ballX + 'px';
|
80 |
ball.style.top = ballY + 'px';
|