Changed around line 0
- let player = document.getElementById('player');
- let timer = document.getElementById('timer');
- let deaths = document.getElementById('deaths');
- let stars = document.getElementById('stars');
- let restartButton = document.getElementById('restart');
- let pauseButton = document.getElementById('pause');
-
- let startTime = Date.now();
- let deathCount = 0;
- let starCount = 0;
- let isPaused = false;
-
- function updateTimer() {
- if (!isPaused) {
- let elapsedTime = Date.now() - startTime;
- let minutes = Math.floor(elapsedTime / 60000);
- let seconds = Math.floor((elapsedTime % 60000) / 1000);
- timer.textContent = `${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
- }
- requestAnimationFrame(updateTimer);
- }
-
- function checkCollision() {
- let hazards = document.querySelectorAll('.hazard');
- hazards.forEach(hazard => {
- let playerRect = player.getBoundingClientRect();
- let hazardRect = hazard.getBoundingClientRect();
- if (playerRect.left < hazardRect.right &&
- playerRect.right > hazardRect.left &&
- playerRect.top < hazardRect.bottom &&
- playerRect.bottom > hazardRect.top) {
- deathCount++;
- deaths.textContent = deathCount;
- player.style.backgroundColor = '#ff0000';
- setTimeout(() => {
- player.style.backgroundColor = '#00ff00';
- player.style.top = '50%';
- player.style.left = '50%';
- }, 500);
- }
- });
- }
-
- function movePlayer(event) {
- if (!isPaused) {
- let key = event.key;
- let playerRect = player.getBoundingClientRect();
- let gameBoardRect = document.querySelector('.game-board').getBoundingClientRect();
- if (key === 'ArrowUp' && playerRect.top > gameBoardRect.top) {
- player.style.top = `${player.offsetTop - 10}px`;
- } else if (key === 'ArrowDown' && playerRect.bottom < gameBoardRect.bottom) {
- player.style.top = `${player.offsetTop + 10}px`;
- } else if (key === 'ArrowLeft' && playerRect.left > gameBoardRect.left) {
- player.style.left = `${player.offsetLeft - 10}px`;
- } else if (key === 'ArrowRight' && playerRect.right < gameBoardRect.right) {
- player.style.left = `${player.offsetLeft + 10}px`;
- }
- checkCollision();
- }
- }
-
- restartButton.addEventListener('click', () => {
- player.style.top = '50%';
- player.style.left = '50%';
- deathCount = 0;
- deaths.textContent = deathCount;
- startTime = Date.now();
- });
-
- pauseButton.addEventListener('click', () => {
- isPaused = !isPaused;
- pauseButton.textContent = isPaused ? 'Resume' : 'Pause';
- });
-
- document.addEventListener('keydown', movePlayer);
- updateTimer();