|
<!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 class="score-board">Score: <span id="score">0</span></div> |
|
<div class="lives">Lives: <span id="lives">3</span></div> |
|
</div> |
|
<div class="controls"> |
|
<button id="left-button">◀</button> |
|
<button id="right-button">▶</button> |
|
<button id="pause-button">Pause</button> |
|
</div> |
|
<script> |
|
const platform = document.querySelector('.platform'); |
|
const ball = document.querySelector('.ball'); |
|
const leftButton = document.getElementById('left-button'); |
|
const rightButton = document.getElementById('right-button'); |
|
const scoreDisplay = document.getElementById('score'); |
|
const livesDisplay = document.getElementById('lives'); |
|
const pauseButton = document.getElementById('pause-button'); |
|
const gameContainer = document.querySelector('.game-container'); |
|
|
|
let platformPosition = 50; |
|
let ballPosition = { top: 0, left: Math.random() * 90 }; |
|
let score = 0; |
|
let lives = 3; |
|
let isCaught = false; |
|
let gamePaused = false; |
|
let ballSpeed = 2; |
|
let level = 1; |
|
let timer = 30; |
|
|
|
|
|
const levelDisplay = document.createElement('div'); |
|
levelDisplay.textContent = `Level: ${level}`; |
|
levelDisplay.style.position = 'absolute'; |
|
levelDisplay.style.top = '10px'; |
|
levelDisplay.style.right = '10px'; |
|
levelDisplay.style.fontSize = '1.5em'; |
|
levelDisplay.style.color = '#ffffff'; |
|
levelDisplay.style.background = 'rgba(0, 0, 0, 0.5)'; |
|
levelDisplay.style.padding = '10px'; |
|
document.body.appendChild(levelDisplay); |
|
|
|
const timerDisplay = document.createElement('div'); |
|
timerDisplay.textContent = `Time: ${timer}s`; |
|
timerDisplay.style.position = 'absolute'; |
|
timerDisplay.style.top = '40px'; |
|
timerDisplay.style.right = '10px'; |
|
timerDisplay.style.fontSize = '1.5em'; |
|
timerDisplay.style.color = '#ffffff'; |
|
timerDisplay.style.background = 'rgba(0, 0, 0, 0.5)'; |
|
timerDisplay.style.padding = '10px'; |
|
document.body.appendChild(timerDisplay); |
|
|
|
|
|
pauseButton.addEventListener('click', () => { |
|
gamePaused = !gamePaused; |
|
if (gamePaused) { |
|
pauseButton.textContent = 'Resume'; |
|
} else { |
|
pauseButton.textContent = 'Pause'; |
|
moveBall(); |
|
} |
|
}); |
|
|
|
|
|
function movePlatformLeft() { |
|
if (platformPosition > 0) { |
|
platformPosition -= 5; |
|
} |
|
platform.style.left = platformPosition + '%'; |
|
} |
|
|
|
|
|
function movePlatformRight() { |
|
if (platformPosition < 90) { |
|
platformPosition += 5; |
|
} |
|
platform.style.left = platformPosition + '%'; |
|
} |
|
|
|
|
|
window.addEventListener('keydown', (e) => { |
|
if (e.key === 'ArrowLeft') movePlatformLeft(); |
|
if (e.key === 'ArrowRight') movePlatformRight(); |
|
}); |
|
|
|
|
|
leftButton.addEventListener('click', movePlatformLeft); |
|
rightButton.addEventListener('click', movePlatformRight); |
|
|
|
|
|
function moveBall() { |
|
if (gamePaused) return; |
|
|
|
ballPosition.top += ballSpeed; |
|
ball.style.top = ballPosition.top + '%'; |
|
ball.style.left = ballPosition.left + '%'; |
|
|
|
|
|
if (ballPosition.top >= 90 && Math.abs(ballPosition.left - platformPosition) < 10) { |
|
isCaught = true; |
|
score++; |
|
scoreDisplay.textContent = score; |
|
resetBall(); |
|
} |
|
|
|
|
|
if (ballPosition.top >= 100) { |
|
if (!isCaught) { |
|
lives--; |
|
livesDisplay.textContent = `Lives: ${lives}`; |
|
if (lives <= 0) { |
|
alert(`Game Over! Final Score: ${score}`); |
|
resetGame(); |
|
} |
|
} |
|
resetBall(); |
|
} |
|
|
|
updateLevel(); |
|
requestAnimationFrame(moveBall); |
|
} |
|
|
|
|
|
function resetBall() { |
|
isCaught = false; |
|
ballPosition = { top: 0, left: Math.random() * 90 }; |
|
} |
|
|
|
|
|
function updateLevel() { |
|
if (score >= level * 10) { |
|
level++; |
|
levelDisplay.textContent = `Level: ${level}`; |
|
ballSpeed += 0.5; |
|
} |
|
} |
|
|
|
|
|
function resetGame() { |
|
score = 0; |
|
lives = 3; |
|
level = 1; |
|
ballSpeed = 2; |
|
timer = 30; |
|
scoreDisplay.textContent = score; |
|
livesDisplay.textContent = `Lives: ${lives}`; |
|
levelDisplay.textContent = `Level: ${level}`; |
|
timerDisplay.textContent = `Time: ${timer}s`; |
|
resetBall(); |
|
} |
|
|
|
|
|
function countdownTimer() { |
|
if (!gamePaused && timer > 0) { |
|
timer--; |
|
timerDisplay.textContent = `Time: ${timer}s`; |
|
} else if (timer <= 0) { |
|
alert('Game Over! Time is up.'); |
|
resetGame(); |
|
} |
|
} |
|
|
|
|
|
setInterval(countdownTimer, 1000); |
|
|
|
moveBall(); |
|
</script> |
|
</body> |
|
</html> |