File size: 1,669 Bytes
fe87710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b65b155
 
 
 
fe87710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const gameContainer = document.getElementById('game-container');
const dino = document.getElementById('dino');
const obstacle = document.getElementById('obstacle');
const scoreDisplay = document.getElementById('score');

let score = 0;
let isJumping = false;
let obstaclePosition = 600;

function jump() {
  if (isJumping) return;
  isJumping = true;
  let jumpCount = 0;
  const jumpInterval = setInterval(() => {
    const jumpHeight = jumpCount * 2;
    dino.style.bottom = `${40 + jumpHeight}px`;
    jumpCount++;
    if (jumpCount > 10) {
      clearInterval(jumpInterval);
      let fallCount = 0;
      const fallInterval = setInterval(() => {
        const fallHeight = fallCount * 2;
        dino.style.bottom = `${100 - fallHeight}px`;
        fallCount++;
        if (fallCount > 10) {
          clearInterval(fallInterval);
          isJumping = false;
        }
      }, 20);
    }
  }, 20);
}

// Listen for touch events
document.addEventListener('touchstart', (event) => {
  jump();
}, { passive: true });

function moveObstacle() {
  obstaclePosition -= 5;
  obstacle.style.left = `${obstaclePosition}px`;
  if (obstaclePosition < -20) {
    obstaclePosition = 600;
    score++;
    scoreDisplay.textContent = `Score: ${score}`;
  }
}

function checkCollision() {
  const dinoTop = parseInt(dino.style.bottom, 10);
  const obstacleLeft = parseInt(obstacle.style.left, 10);
  if (obstacleLeft < 40 && obstacleLeft > 0 && dinoTop <= 40) {
    alert(`Game Over! Your score was: ${score}`);
    obstaclePosition = 600;
    score = 0;
    scoreDisplay.textContent = `Score: ${score}`;
  }
}

setInterval(() => {
  moveObstacle();
  checkCollision();
}, 20);