Changed around line 1
+ let mario = document.getElementById('mario');
+ let obstacle = document.getElementById('obstacle');
+ let coin = document.getElementById('coin');
+ let startBtn = document.getElementById('start-btn');
+ let score = 0;
+ let gameInterval;
+
+ function jump() {
+ if (mario.classList != 'jump') {
+ mario.classList.add('jump');
+ setTimeout(() => {
+ mario.classList.remove('jump');
+ }, 500);
+ }
+ }
+
+ function startGame() {
+ score = 0;
+ document.getElementById('score').innerText = 'Score: ' + score;
+ obstacle.style.right = '-50px';
+ coin.style.right = '-50px';
+ gameInterval = setInterval(() => {
+ let obstaclePosition = obstacle.offsetRight;
+ let coinPosition = coin.offsetRight;
+ let marioPosition = mario.offsetBottom;
+
+ if (obstaclePosition < 100 && obstaclePosition > 50 && marioPosition <= 50) {
+ clearInterval(gameInterval);
+ alert('Game Over! Your score: ' + score);
+ }
+
+ if (coinPosition < 100 && coinPosition > 50 && marioPosition <= 50) {
+ score++;
+ document.getElementById('score').innerText = 'Score: ' + score;
+ coin.style.right = '-50px';
+ }
+
+ obstacle.style.right = (obstaclePosition + 5) + 'px';
+ coin.style.right = (coinPosition + 5) + 'px';
+ }, 20);
+ }
+
+ document.addEventListener('keydown', (event) => {
+ if (event.code === 'Space') {
+ jump();
+ }
+ });
+
+ startBtn.addEventListener('click', startGame);