Score: 0
GAME OVER
const player = document.getElementById("player"); const obstacles = document.querySelectorAll(".obstacle"); const score = document.getElementById("score"); const gameOver = document.getElementById("game-over"); let playerBottom = 0; let playerSpeed = 0; let obstacleSpeed = 5; let gameScore = 0; let isGameOver = false; function gameLoop() { if (isGameOver) { return; } // Update player position playerBottom += playerSpeed; player.style.bottom = `${playerBottom}px`; // Update obstacle position obstacles.forEach((obstacle) => { const obstacleLeft = parseInt(obstacle.style.left, 10); obstacle.style.left = `${obstacleLeft - obstacleSpeed}px`; // Check for collision with player if ( obstacleLeft <= 50 && obstacleLeft >= -50 && playerBottom <= 50 && playerBottom >= -50 ) { gameOverScreen(); } // Check for obstacle going off screen if (obstacleLeft < -100) { obstacle.style.left = `${Math.floor(Math.random() * 300) + 600}px`; gameScore++; score.innerHTML = `Score: ${gameScore}`; } }); // Call gameLoop again after 16ms (60fps) requestAnimationFrame(gameLoop); } function startGame() { // Reset game state playerBottom = 0; playerSpeed = 0; obstacleSpeed = 5;