Update script.js
Browse files
script.js
CHANGED
@@ -10,7 +10,7 @@ let paddleRightY = window.innerHeight / 2 - 40; // Center the paddle
|
|
10 |
let ballX = window.innerWidth / 2; // Center the ball
|
11 |
let ballY = window.innerHeight / 2; // Center the ball
|
12 |
let ballSpeedX = 3; // Horizontal speed
|
13 |
-
let ballSpeedY =
|
14 |
let playerScore = 0;
|
15 |
let botScore = 0;
|
16 |
let isGamePaused = true; // Pause the game until the player moves
|
@@ -18,6 +18,7 @@ let isGamePaused = true; // Pause the game until the player moves
|
|
18 |
const paddleHeight = 80;
|
19 |
const paddleWidth = 10;
|
20 |
const ballSize = 15;
|
|
|
21 |
|
22 |
// Touch controls
|
23 |
let touchY = 0;
|
@@ -58,9 +59,11 @@ function update() {
|
|
58 |
) {
|
59 |
// Calculate hit position (how far from the center of the paddle)
|
60 |
const hitPosition = (ballY - (paddleLeftY + paddleHeight / 2)) / (paddleHeight / 2);
|
61 |
-
//
|
|
|
|
|
62 |
ballSpeedX = Math.abs(ballSpeedX); // Ensure ball moves right
|
63 |
-
ballSpeedY =
|
64 |
}
|
65 |
|
66 |
// Ball collision with right paddle
|
@@ -71,9 +74,11 @@ function update() {
|
|
71 |
) {
|
72 |
// Calculate hit position (how far from the center of the paddle)
|
73 |
const hitPosition = (ballY - (paddleRightY + paddleHeight / 2)) / (paddleHeight / 2);
|
74 |
-
//
|
|
|
|
|
75 |
ballSpeedX = -Math.abs(ballSpeedX); // Ensure ball moves left
|
76 |
-
ballSpeedY =
|
77 |
}
|
78 |
|
79 |
// Ball out of bounds (left or right)
|
@@ -123,7 +128,7 @@ function resetBall(side) {
|
|
123 |
ballSpeedX = -Math.abs(ballSpeedX); // Move ball to the left
|
124 |
}
|
125 |
|
126 |
-
ballSpeedY =
|
127 |
}
|
128 |
|
129 |
// Start the game loop
|
|
|
10 |
let ballX = window.innerWidth / 2; // Center the ball
|
11 |
let ballY = window.innerHeight / 2; // Center the ball
|
12 |
let ballSpeedX = 3; // Horizontal speed
|
13 |
+
let ballSpeedY = 0; // Vertical speed (starts with no vertical movement)
|
14 |
let playerScore = 0;
|
15 |
let botScore = 0;
|
16 |
let isGamePaused = true; // Pause the game until the player moves
|
|
|
18 |
const paddleHeight = 80;
|
19 |
const paddleWidth = 10;
|
20 |
const ballSize = 15;
|
21 |
+
const maxAngle = 45; // Maximum angle in degrees
|
22 |
|
23 |
// Touch controls
|
24 |
let touchY = 0;
|
|
|
59 |
) {
|
60 |
// Calculate hit position (how far from the center of the paddle)
|
61 |
const hitPosition = (ballY - (paddleLeftY + paddleHeight / 2)) / (paddleHeight / 2);
|
62 |
+
// Calculate angle based on hit position (in radians)
|
63 |
+
const angle = (hitPosition * maxAngle * Math.PI) / 180;
|
64 |
+
// Adjust ball speed based on angle
|
65 |
ballSpeedX = Math.abs(ballSpeedX); // Ensure ball moves right
|
66 |
+
ballSpeedY = Math.tan(angle) * Math.abs(ballSpeedX); // Set vertical speed based on angle
|
67 |
}
|
68 |
|
69 |
// Ball collision with right paddle
|
|
|
74 |
) {
|
75 |
// Calculate hit position (how far from the center of the paddle)
|
76 |
const hitPosition = (ballY - (paddleRightY + paddleHeight / 2)) / (paddleHeight / 2);
|
77 |
+
// Calculate angle based on hit position (in radians)
|
78 |
+
const angle = (hitPosition * maxAngle * Math.PI) / 180;
|
79 |
+
// Adjust ball speed based on angle
|
80 |
ballSpeedX = -Math.abs(ballSpeedX); // Ensure ball moves left
|
81 |
+
ballSpeedY = Math.tan(angle) * Math.abs(ballSpeedX); // Set vertical speed based on angle
|
82 |
}
|
83 |
|
84 |
// Ball out of bounds (left or right)
|
|
|
128 |
ballSpeedX = -Math.abs(ballSpeedX); // Move ball to the left
|
129 |
}
|
130 |
|
131 |
+
ballSpeedY = 0; // Reset vertical speed
|
132 |
}
|
133 |
|
134 |
// Start the game loop
|