Spaces:
Running
Running
Add 1 files
Browse files- index.html +16 -13
index.html
CHANGED
@@ -146,6 +146,10 @@
|
|
146 |
|
147 |
// Update element positions
|
148 |
function updatePositions() {
|
|
|
|
|
|
|
|
|
149 |
paddlePlayer.style.top = `${playerY}px`;
|
150 |
paddleCPU.style.top = `${cpuY}px`;
|
151 |
ball.style.left = `${ballX - BALL_SIZE/2}px`;
|
@@ -212,16 +216,15 @@
|
|
212 |
resetBall();
|
213 |
}
|
214 |
|
215 |
-
// AI for CPU paddle
|
216 |
const cpuPaddleCenter = cpuY + PADDLE_HEIGHT / 2;
|
217 |
-
|
218 |
-
cpuY += PADDLE_SPEED * 0.7;
|
219 |
-
} else if (cpuPaddleCenter > ballY + 15) {
|
220 |
-
cpuY -= PADDLE_SPEED * 0.7;
|
221 |
-
}
|
222 |
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
225 |
|
226 |
// Check game over
|
227 |
if (playerScore >= 5 || cpuScore >= 5) {
|
@@ -343,16 +346,16 @@
|
|
343 |
}
|
344 |
});
|
345 |
|
346 |
-
// Handle paddle movement
|
347 |
function handleInput() {
|
348 |
if (!gameRunning) return;
|
349 |
|
350 |
-
if ((keys.w || keys.ArrowUp)
|
351 |
-
playerY
|
352 |
}
|
353 |
|
354 |
-
if ((keys.s || keys.ArrowDown)
|
355 |
-
playerY
|
356 |
}
|
357 |
|
358 |
updatePositions();
|
|
|
146 |
|
147 |
// Update element positions
|
148 |
function updatePositions() {
|
149 |
+
// Ensure paddles stay within bounds
|
150 |
+
playerY = Math.max(0, Math.min(GAME_HEIGHT - PADDLE_HEIGHT, playerY));
|
151 |
+
cpuY = Math.max(0, Math.min(GAME_HEIGHT - PADDLE_HEIGHT, cpuY));
|
152 |
+
|
153 |
paddlePlayer.style.top = `${playerY}px`;
|
154 |
paddleCPU.style.top = `${cpuY}px`;
|
155 |
ball.style.left = `${ballX - BALL_SIZE/2}px`;
|
|
|
216 |
resetBall();
|
217 |
}
|
218 |
|
219 |
+
// AI for CPU paddle - improved to stay in bounds
|
220 |
const cpuPaddleCenter = cpuY + PADDLE_HEIGHT / 2;
|
221 |
+
const ballCenter = ballY;
|
|
|
|
|
|
|
|
|
222 |
|
223 |
+
if (cpuPaddleCenter < ballCenter - 15) {
|
224 |
+
cpuY = Math.min(cpuY + PADDLE_SPEED * 0.7, GAME_HEIGHT - PADDLE_HEIGHT);
|
225 |
+
} else if (cpuPaddleCenter > ballCenter + 15) {
|
226 |
+
cpuY = Math.max(cpuY - PADDLE_SPEED * 0.7, 0);
|
227 |
+
}
|
228 |
|
229 |
// Check game over
|
230 |
if (playerScore >= 5 || cpuScore >= 5) {
|
|
|
346 |
}
|
347 |
});
|
348 |
|
349 |
+
// Handle paddle movement with boundary checks
|
350 |
function handleInput() {
|
351 |
if (!gameRunning) return;
|
352 |
|
353 |
+
if ((keys.w || keys.ArrowUp)) {
|
354 |
+
playerY = Math.max(0, playerY - PADDLE_SPEED);
|
355 |
}
|
356 |
|
357 |
+
if ((keys.s || keys.ArrowDown)) {
|
358 |
+
playerY = Math.min(GAME_HEIGHT - PADDLE_HEIGHT, playerY + PADDLE_SPEED);
|
359 |
}
|
360 |
|
361 |
updatePositions();
|