|
<!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; |
|
let ballPosition = { top: 0, left: Math.random() * 90 }; |
|
|
|
|
|
window.addEventListener('keydown', (e) => { |
|
if (e.key === 'ArrowLeft' && platformPosition > 0) { |
|
platformPosition -= 5; |
|
} |
|
if (e.key === 'ArrowRight' && platformPosition < 90) { |
|
platformPosition += 5; |
|
} |
|
platform.style.left = platformPosition + '%'; |
|
}); |
|
|
|
|
|
function moveBall() { |
|
ballPosition.top += 2; |
|
ball.style.top = ballPosition.top + '%'; |
|
ball.style.left = ballPosition.left + '%'; |
|
|
|
|
|
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) { |
|
alert('You caught the ball!'); |
|
resetGame(); |
|
} |
|
|
|
|
|
if (ballPosition.top >= 100) { |
|
alert('Game Over!'); |
|
resetGame(); |
|
} |
|
|
|
requestAnimationFrame(moveBall); |
|
} |
|
|
|
|
|
function resetGame() { |
|
ballPosition = { top: 0, left: Math.random() * 90 }; |
|
} |
|
|
|
moveBall(); |
|
</script> |
|
</body> |
|
</html> |