Update script.js
Browse files
script.js
CHANGED
@@ -5,68 +5,59 @@ 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;
|
9 |
-
let paddleRightY = window.innerHeight / 2 - 40;
|
10 |
-
let ballX = window.innerWidth / 2;
|
11 |
-
let ballY = window.innerHeight / 2;
|
12 |
-
let ballSpeedX = 3;
|
13 |
-
let ballSpeedY = 0;
|
14 |
let playerScore = 0;
|
15 |
let botScore = 0;
|
16 |
-
let isGamePaused = true;
|
17 |
|
18 |
const paddleHeight = 80;
|
19 |
const paddleWidth = 10;
|
20 |
const ballSize = 15;
|
21 |
-
const maxAngle = 45;
|
22 |
-
|
23 |
-
// Touch controls
|
24 |
-
let touchY = 0;
|
25 |
|
26 |
gameArea.addEventListener('touchmove', (e) => {
|
27 |
e.preventDefault();
|
28 |
-
touchY = e.touches[0].clientY;
|
29 |
|
30 |
-
// Move left paddle based on touch position
|
31 |
if (e.touches[0].clientX < window.innerWidth / 2) {
|
32 |
paddleLeftY = touchY - paddleHeight / 2;
|
33 |
-
|
34 |
-
if (paddleLeftY > window.innerHeight - paddleHeight) paddleLeftY = window.innerHeight - paddleHeight;
|
35 |
|
36 |
-
// Start the game if it's paused
|
37 |
if (isGamePaused) {
|
38 |
isGamePaused = false;
|
39 |
}
|
40 |
}
|
41 |
});
|
42 |
|
43 |
-
function handlePaddleCollision(paddleY,
|
44 |
-
|
45 |
-
const angle = hitPosition * maxAngle * (Math.PI / 180); // Convert to radians
|
46 |
|
47 |
-
|
48 |
-
ballSpeedY = Math.sin(angle) * Math.abs(ballSpeedX); // Adjust vertical speed
|
49 |
-
|
50 |
-
// Reposition ball to ensure no clipping through paddle
|
51 |
-
if (isLeftPaddle) {
|
52 |
-
ballX = paddleWidth + 10; // Place ball just outside left paddle
|
53 |
-
} else {
|
54 |
-
ballX = window.innerWidth - paddleWidth - 25; // Place ball just outside right paddle
|
55 |
-
}
|
56 |
-
}
|
57 |
-
|
58 |
-
function checkPaddleCollision(paddleY, paddleX, isLeftPaddle) {
|
59 |
if (
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
) {
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
}
|
65 |
}
|
66 |
|
67 |
function update() {
|
68 |
if (!isGamePaused) {
|
69 |
-
|
70 |
ballX += ballSpeedX;
|
71 |
ballY += ballSpeedY;
|
72 |
|
@@ -75,11 +66,13 @@ function update() {
|
|
75 |
ballSpeedY = -ballSpeedY;
|
76 |
}
|
77 |
|
78 |
-
// Check collision with
|
79 |
-
|
80 |
-
checkPaddleCollision(paddleRightY, window.innerWidth - paddleWidth - 25, false); // Right paddle
|
81 |
|
82 |
-
//
|
|
|
|
|
|
|
83 |
if (ballX <= 0) {
|
84 |
botScore++;
|
85 |
botScoreDisplay.textContent = botScore;
|
@@ -91,20 +84,17 @@ function update() {
|
|
91 |
}
|
92 |
}
|
93 |
|
94 |
-
// Bot
|
95 |
-
if (ballSpeedX > 0) {
|
96 |
if (paddleRightY + paddleHeight / 2 < ballY) {
|
97 |
-
paddleRightY += 3;
|
98 |
} else if (paddleRightY + paddleHeight / 2 > ballY) {
|
99 |
-
paddleRightY -= 3;
|
100 |
}
|
101 |
}
|
102 |
|
103 |
-
|
104 |
-
if (paddleRightY < 0) paddleRightY = 0;
|
105 |
-
if (paddleRightY > window.innerHeight - paddleHeight) paddleRightY = window.innerHeight - paddleHeight;
|
106 |
|
107 |
-
// Update ball and paddle positions
|
108 |
ball.style.left = ballX + 'px';
|
109 |
ball.style.top = ballY + 'px';
|
110 |
paddleLeft.style.top = paddleLeftY + 'px';
|
@@ -114,19 +104,19 @@ function update() {
|
|
114 |
}
|
115 |
|
116 |
function resetBall(side) {
|
117 |
-
isGamePaused = true;
|
118 |
|
119 |
if (side === 'left') {
|
120 |
-
ballX = paddleWidth + 10;
|
121 |
ballY = paddleLeftY + paddleHeight / 2;
|
122 |
-
ballSpeedX = Math.abs(ballSpeedX);
|
123 |
-
} else
|
124 |
-
ballX = window.innerWidth - paddleWidth - 25;
|
125 |
ballY = paddleRightY + paddleHeight / 2;
|
126 |
-
ballSpeedX = -Math.abs(ballSpeedX);
|
127 |
}
|
128 |
|
129 |
-
ballSpeedY = 0;
|
130 |
}
|
131 |
|
132 |
// Start the game loop
|
|
|
5 |
const playerScoreDisplay = document.getElementById('playerScore');
|
6 |
const botScoreDisplay = document.getElementById('botScore');
|
7 |
|
8 |
+
let paddleLeftY = window.innerHeight / 2 - 40;
|
9 |
+
let paddleRightY = window.innerHeight / 2 - 40;
|
10 |
+
let ballX = window.innerWidth / 2;
|
11 |
+
let ballY = window.innerHeight / 2;
|
12 |
+
let ballSpeedX = 3;
|
13 |
+
let ballSpeedY = 0;
|
14 |
let playerScore = 0;
|
15 |
let botScore = 0;
|
16 |
+
let isGamePaused = true;
|
17 |
|
18 |
const paddleHeight = 80;
|
19 |
const paddleWidth = 10;
|
20 |
const ballSize = 15;
|
21 |
+
const maxAngle = 45;
|
|
|
|
|
|
|
22 |
|
23 |
gameArea.addEventListener('touchmove', (e) => {
|
24 |
e.preventDefault();
|
25 |
+
let touchY = e.touches[0].clientY;
|
26 |
|
|
|
27 |
if (e.touches[0].clientX < window.innerWidth / 2) {
|
28 |
paddleLeftY = touchY - paddleHeight / 2;
|
29 |
+
paddleLeftY = Math.max(0, Math.min(paddleLeftY, window.innerHeight - paddleHeight));
|
|
|
30 |
|
|
|
31 |
if (isGamePaused) {
|
32 |
isGamePaused = false;
|
33 |
}
|
34 |
}
|
35 |
});
|
36 |
|
37 |
+
function handlePaddleCollision(paddleY, paddleX, isLeftPaddle) {
|
38 |
+
let previousBallX = ballX - ballSpeedX;
|
|
|
39 |
|
40 |
+
// Swept collision detection: Check if the ball crossed the paddle between frames
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
if (
|
42 |
+
previousBallX < paddleX + paddleWidth &&
|
43 |
+
ballX > paddleX &&
|
44 |
+
ballY + ballSize > paddleY &&
|
45 |
+
ballY < paddleY + paddleHeight
|
46 |
) {
|
47 |
+
const hitPosition = (ballY - (paddleY + paddleHeight / 2)) / (paddleHeight / 2);
|
48 |
+
const angle = hitPosition * maxAngle * (Math.PI / 180);
|
49 |
+
|
50 |
+
ballSpeedX = (isLeftPaddle ? 1 : -1) * Math.abs(ballSpeedX);
|
51 |
+
ballSpeedY = Math.sin(angle) * Math.abs(ballSpeedX);
|
52 |
+
|
53 |
+
// Reposition the ball outside the paddle to avoid clipping
|
54 |
+
ballX = isLeftPaddle ? paddleX + paddleWidth + 1 : paddleX - ballSize - 1;
|
55 |
}
|
56 |
}
|
57 |
|
58 |
function update() {
|
59 |
if (!isGamePaused) {
|
60 |
+
let previousBallX = ballX;
|
61 |
ballX += ballSpeedX;
|
62 |
ballY += ballSpeedY;
|
63 |
|
|
|
66 |
ballSpeedY = -ballSpeedY;
|
67 |
}
|
68 |
|
69 |
+
// Check collision with left paddle using swept collision detection
|
70 |
+
handlePaddleCollision(paddleLeftY, paddleWidth, true);
|
|
|
71 |
|
72 |
+
// Check collision with right paddle using swept collision detection
|
73 |
+
handlePaddleCollision(paddleRightY, window.innerWidth - paddleWidth - 25, false);
|
74 |
+
|
75 |
+
// Ball out of bounds
|
76 |
if (ballX <= 0) {
|
77 |
botScore++;
|
78 |
botScoreDisplay.textContent = botScore;
|
|
|
84 |
}
|
85 |
}
|
86 |
|
87 |
+
// Bot AI movement
|
88 |
+
if (ballSpeedX > 0) {
|
89 |
if (paddleRightY + paddleHeight / 2 < ballY) {
|
90 |
+
paddleRightY += 3;
|
91 |
} else if (paddleRightY + paddleHeight / 2 > ballY) {
|
92 |
+
paddleRightY -= 3;
|
93 |
}
|
94 |
}
|
95 |
|
96 |
+
paddleRightY = Math.max(0, Math.min(paddleRightY, window.innerHeight - paddleHeight));
|
|
|
|
|
97 |
|
|
|
98 |
ball.style.left = ballX + 'px';
|
99 |
ball.style.top = ballY + 'px';
|
100 |
paddleLeft.style.top = paddleLeftY + 'px';
|
|
|
104 |
}
|
105 |
|
106 |
function resetBall(side) {
|
107 |
+
isGamePaused = true;
|
108 |
|
109 |
if (side === 'left') {
|
110 |
+
ballX = paddleWidth + 10;
|
111 |
ballY = paddleLeftY + paddleHeight / 2;
|
112 |
+
ballSpeedX = Math.abs(ballSpeedX);
|
113 |
+
} else {
|
114 |
+
ballX = window.innerWidth - paddleWidth - 25;
|
115 |
ballY = paddleRightY + paddleHeight / 2;
|
116 |
+
ballSpeedX = -Math.abs(ballSpeedX);
|
117 |
}
|
118 |
|
119 |
+
ballSpeedY = 0;
|
120 |
}
|
121 |
|
122 |
// Start the game loop
|