Update script.js
Browse files
script.js
CHANGED
@@ -2,6 +2,8 @@ const gameArea = document.getElementById('gameArea');
|
|
2 |
const paddleLeft = document.getElementById('paddleLeft');
|
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
|
@@ -9,6 +11,9 @@ 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;
|
@@ -26,40 +31,53 @@ gameArea.addEventListener('touchmove', (e) => {
|
|
26 |
paddleLeftY = touchY - paddleHeight / 2;
|
27 |
if (paddleLeftY < 0) paddleLeftY = 0;
|
28 |
if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
});
|
31 |
|
32 |
function update() {
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
}
|
64 |
|
65 |
// Bot logic for right paddle
|
@@ -84,10 +102,20 @@ function update() {
|
|
84 |
requestAnimationFrame(update);
|
85 |
}
|
86 |
|
87 |
-
function resetBall() {
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
}
|
92 |
|
93 |
// Start the game loop
|
|
|
2 |
const paddleLeft = document.getElementById('paddleLeft');
|
3 |
const paddleRight = document.getElementById('paddleRight');
|
4 |
const ball = document.getElementById('ball');
|
5 |
+
const playerScoreDisplay = document.getElementById('playerScore');
|
6 |
+
const botScoreDisplay = document.getElementById('botScore');
|
7 |
|
8 |
let paddleLeftY = window.innerHeight / 2 - 40; // Center the paddle
|
9 |
let paddleRightY = window.innerHeight / 2 - 40; // Center the paddle
|
|
|
11 |
let ballY = window.innerHeight / 2; // Center the ball
|
12 |
let ballSpeedX = 3; // Horizontal speed
|
13 |
let ballSpeedY = 3; // Vertical speed
|
14 |
+
let playerScore = 0;
|
15 |
+
let botScore = 0;
|
16 |
+
let isGamePaused = true; // Pause the game until the player moves
|
17 |
|
18 |
const paddleHeight = 80;
|
19 |
const paddleWidth = 10;
|
|
|
31 |
paddleLeftY = touchY - paddleHeight / 2;
|
32 |
if (paddleLeftY < 0) paddleLeftY = 0;
|
33 |
if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
|
34 |
+
|
35 |
+
// Start the game if it's paused
|
36 |
+
if (isGamePaused) {
|
37 |
+
isGamePaused = false;
|
38 |
+
}
|
39 |
}
|
40 |
});
|
41 |
|
42 |
function update() {
|
43 |
+
if (!isGamePaused) {
|
44 |
+
// Update ball position
|
45 |
+
ballX += ballSpeedX;
|
46 |
+
ballY += ballSpeedY;
|
47 |
+
|
48 |
+
// Ball collision with top and bottom walls
|
49 |
+
if (ballY <= 0 || ballY >= window.innerHeight - ballSize) {
|
50 |
+
ballSpeedY = -ballSpeedY;
|
51 |
+
}
|
52 |
|
53 |
+
// Ball collision with left paddle
|
54 |
+
if (
|
55 |
+
ballX <= paddleWidth + 10 && // Left paddle
|
56 |
+
ballY + ballSize >= paddleLeftY &&
|
57 |
+
ballY <= paddleLeftY + paddleHeight
|
58 |
+
) {
|
59 |
+
ballSpeedX = -ballSpeedX;
|
60 |
+
}
|
61 |
|
62 |
+
// Ball collision with right paddle
|
63 |
+
if (
|
64 |
+
ballX >= window.innerWidth - paddleWidth - 25 && // Right paddle
|
65 |
+
ballY + ballSize >= paddleRightY &&
|
66 |
+
ballY <= paddleRightY + paddleHeight
|
67 |
+
) {
|
68 |
+
ballSpeedX = -ballSpeedX;
|
69 |
+
}
|
70 |
|
71 |
+
// Ball out of bounds (left or right)
|
72 |
+
if (ballX <= 0) {
|
73 |
+
botScore++;
|
74 |
+
botScoreDisplay.textContent = botScore;
|
75 |
+
resetBall('right');
|
76 |
+
} else if (ballX >= window.innerWidth) {
|
77 |
+
playerScore++;
|
78 |
+
playerScoreDisplay.textContent = playerScore;
|
79 |
+
resetBall('left');
|
80 |
+
}
|
81 |
}
|
82 |
|
83 |
// Bot logic for right paddle
|
|
|
102 |
requestAnimationFrame(update);
|
103 |
}
|
104 |
|
105 |
+
function resetBall(side) {
|
106 |
+
isGamePaused = true; // Pause the game
|
107 |
+
|
108 |
+
if (side === 'left') {
|
109 |
+
ballX = paddleWidth + 10; // Place ball on the left paddle
|
110 |
+
ballY = paddleLeftY + paddleHeight / 2;
|
111 |
+
ballSpeedX = Math.abs(ballSpeedX); // Move ball to the right
|
112 |
+
} else if (side === 'right') {
|
113 |
+
ballX = window.innerWidth - paddleWidth - 25; // Place ball on the right paddle
|
114 |
+
ballY = paddleRightY + paddleHeight / 2;
|
115 |
+
ballSpeedX = -Math.abs(ballSpeedX); // Move ball to the left
|
116 |
+
}
|
117 |
+
|
118 |
+
ballSpeedY = (Math.random() - 0.5) * 6; // Randomize vertical speed
|
119 |
}
|
120 |
|
121 |
// Start the game loop
|