Update script.js
Browse files
script.js
CHANGED
@@ -3,17 +3,37 @@ const paddleLeft = document.getElementById('paddleLeft');
|
|
3 |
const paddleRight = document.getElementById('paddleRight');
|
4 |
const ball = document.getElementById('ball');
|
5 |
|
6 |
-
let paddleLeftY =
|
7 |
-
let paddleRightY =
|
8 |
-
let ballX =
|
9 |
-
let ballY =
|
10 |
-
let ballSpeedX =
|
11 |
-
let ballSpeedY =
|
12 |
|
13 |
-
const paddleSpeed = 10;
|
14 |
const paddleHeight = 80;
|
15 |
-
const
|
16 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
function update() {
|
19 |
// Update ball position
|
@@ -21,28 +41,35 @@ function update() {
|
|
21 |
ballY += ballSpeedY;
|
22 |
|
23 |
// Ball collision with top and bottom walls
|
24 |
-
if (ballY <= 0 || ballY >=
|
25 |
ballSpeedY = -ballSpeedY;
|
26 |
}
|
27 |
|
28 |
// Ball collision with paddles
|
29 |
-
if (
|
|
|
|
|
|
|
|
|
30 |
ballSpeedX = -ballSpeedX;
|
31 |
}
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
ballSpeedX = -ballSpeedX;
|
34 |
}
|
35 |
|
36 |
-
// Ball out of bounds
|
37 |
-
if (ballX <= 0 || ballX >=
|
38 |
resetBall();
|
39 |
}
|
40 |
|
41 |
-
// Update ball
|
42 |
ball.style.left = ballX + 'px';
|
43 |
ball.style.top = ballY + 'px';
|
44 |
-
|
45 |
-
// Update paddle positions on screen
|
46 |
paddleLeft.style.top = paddleLeftY + 'px';
|
47 |
paddleRight.style.top = paddleRightY + 'px';
|
48 |
|
@@ -50,28 +77,10 @@ function update() {
|
|
50 |
}
|
51 |
|
52 |
function resetBall() {
|
53 |
-
ballX =
|
54 |
-
ballY =
|
55 |
-
ballSpeedX = -ballSpeedX;
|
56 |
}
|
57 |
|
58 |
-
//
|
59 |
-
gameArea.addEventListener('touchmove', (e) => {
|
60 |
-
const touchY = e.touches[0].clientY - gameArea.getBoundingClientRect().top;
|
61 |
-
|
62 |
-
// Move left paddle
|
63 |
-
if (touchY < gameWidth / 2) {
|
64 |
-
paddleLeftY = touchY - paddleHeight / 2;
|
65 |
-
if (paddleLeftY < 0) paddleLeftY = 0;
|
66 |
-
if (paddleLeftY > gameHeight - paddleHeight) paddleLeftY = gameHeight - paddleHeight;
|
67 |
-
}
|
68 |
-
|
69 |
-
// Move right paddle
|
70 |
-
else {
|
71 |
-
paddleRightY = touchY - paddleHeight / 2;
|
72 |
-
if (paddleRightY < 0) paddleRightY = 0;
|
73 |
-
if (paddleRightY > gameHeight - paddleHeight) paddleRightY = gameHeight - paddleHeight;
|
74 |
-
}
|
75 |
-
});
|
76 |
-
|
77 |
update();
|
|
|
3 |
const paddleRight = document.getElementById('paddleRight');
|
4 |
const ball = document.getElementById('ball');
|
5 |
|
6 |
+
let paddleLeftY = window.innerHeight / 2 - 40; // Center the paddle
|
7 |
+
let paddleRightY = window.innerHeight / 2 - 40; // Center the paddle
|
8 |
+
let ballX = window.innerWidth / 2; // Center the ball
|
9 |
+
let ballY = window.innerHeight / 2; // Center the ball
|
10 |
+
let ballSpeedX = 3; // Horizontal speed
|
11 |
+
let ballSpeedY = 3; // Vertical speed
|
12 |
|
|
|
13 |
const paddleHeight = 80;
|
14 |
+
const paddleWidth = 10;
|
15 |
+
const ballSize = 15;
|
16 |
+
|
17 |
+
// Touch controls
|
18 |
+
let touchY = 0;
|
19 |
+
|
20 |
+
gameArea.addEventListener('touchmove', (e) => {
|
21 |
+
e.preventDefault();
|
22 |
+
touchY = e.touches[0].clientY;
|
23 |
+
|
24 |
+
// Move paddles based on touch position
|
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 |
|
38 |
function update() {
|
39 |
// Update ball position
|
|
|
41 |
ballY += ballSpeedY;
|
42 |
|
43 |
// Ball collision with top and bottom walls
|
44 |
+
if (ballY <= 0 || ballY >= window.innerHeight - ballSize) {
|
45 |
ballSpeedY = -ballSpeedY;
|
46 |
}
|
47 |
|
48 |
// Ball collision with paddles
|
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 - 10 && // Right paddle
|
59 |
+
ballY >= paddleRightY &&
|
60 |
+
ballY <= paddleRightY + paddleHeight
|
61 |
+
) {
|
62 |
ballSpeedX = -ballSpeedX;
|
63 |
}
|
64 |
|
65 |
+
// Ball out of bounds (left or right)
|
66 |
+
if (ballX <= 0 || ballX >= window.innerWidth) {
|
67 |
resetBall();
|
68 |
}
|
69 |
|
70 |
+
// Update ball and paddle positions
|
71 |
ball.style.left = ballX + 'px';
|
72 |
ball.style.top = ballY + 'px';
|
|
|
|
|
73 |
paddleLeft.style.top = paddleLeftY + 'px';
|
74 |
paddleRight.style.top = paddleRightY + 'px';
|
75 |
|
|
|
77 |
}
|
78 |
|
79 |
function resetBall() {
|
80 |
+
ballX = window.innerWidth / 2;
|
81 |
+
ballY = window.innerHeight / 2;
|
82 |
+
ballSpeedX = -ballSpeedX; // Reverse direction
|
83 |
}
|
84 |
|
85 |
+
// Start the game loop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
update();
|