File size: 2,008 Bytes
e762863 5e8e113 2fe2a19 e762863 5e8e113 e762863 5e8e113 e762863 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Futuristic Catch Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="game-container">
<div class="ball"></div>
<div class="platform"></div>
</div>
<script>
const platform = document.querySelector('.platform');
const ball = document.querySelector('.ball');
const gameContainer = document.querySelector('.game-container');
let platformPosition = 50; // Initial position of the platform (centered).
let ballPosition = { top: 0, left: Math.random() * 90 }; // Random horizontal start for the ball.
// Move platform
window.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft' && platformPosition > 0) {
platformPosition -= 5; // Move left
}
if (e.key === 'ArrowRight' && platformPosition < 90) {
platformPosition += 5; // Move right
}
platform.style.left = platformPosition + '%';
});
// Move ball
function moveBall() {
ballPosition.top += 2; // Ball falls down
ball.style.top = ballPosition.top + '%';
ball.style.left = ballPosition.left + '%';
// Check for collision with platform
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) {
alert('You caught the ball!');
resetGame();
}
// Check if ball missed
if (ballPosition.top >= 100) {
alert('Game Over!');
resetGame();
}
requestAnimationFrame(moveBall);
}
// Reset game
function resetGame() {
ballPosition = { top: 0, left: Math.random() * 90 };
}
moveBall();
</script>
</body>
</html> |