api-ix commited on
Commit
fe87710
·
verified ·
1 Parent(s): 484dc03

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +64 -0
script.js ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const gameContainer = document.getElementById('game-container');
2
+ const dino = document.getElementById('dino');
3
+ const obstacle = document.getElementById('obstacle');
4
+ const scoreDisplay = document.getElementById('score');
5
+
6
+ let score = 0;
7
+ let isJumping = false;
8
+ let obstaclePosition = 600;
9
+
10
+ function jump() {
11
+ if (isJumping) return;
12
+ isJumping = true;
13
+ let jumpCount = 0;
14
+ const jumpInterval = setInterval(() => {
15
+ const jumpHeight = jumpCount * 2;
16
+ dino.style.bottom = `${40 + jumpHeight}px`;
17
+ jumpCount++;
18
+ if (jumpCount > 10) {
19
+ clearInterval(jumpInterval);
20
+ let fallCount = 0;
21
+ const fallInterval = setInterval(() => {
22
+ const fallHeight = fallCount * 2;
23
+ dino.style.bottom = `${100 - fallHeight}px`;
24
+ fallCount++;
25
+ if (fallCount > 10) {
26
+ clearInterval(fallInterval);
27
+ isJumping = false;
28
+ }
29
+ }, 20);
30
+ }
31
+ }, 20);
32
+ }
33
+
34
+ document.addEventListener('keydown', (event) => {
35
+ if (event.code === 'Space') {
36
+ jump();
37
+ }
38
+ });
39
+
40
+ function moveObstacle() {
41
+ obstaclePosition -= 5;
42
+ obstacle.style.left = `${obstaclePosition}px`;
43
+ if (obstaclePosition < -20) {
44
+ obstaclePosition = 600;
45
+ score++;
46
+ scoreDisplay.textContent = `Score: ${score}`;
47
+ }
48
+ }
49
+
50
+ function checkCollision() {
51
+ const dinoTop = parseInt(dino.style.bottom, 10);
52
+ const obstacleLeft = parseInt(obstacle.style.left, 10);
53
+ if (obstacleLeft < 40 && obstacleLeft > 0 && dinoTop <= 40) {
54
+ alert(`Game Over! Your score was: ${score}`);
55
+ obstaclePosition = 600;
56
+ score = 0;
57
+ scoreDisplay.textContent = `Score: ${score}`;
58
+ }
59
+ }
60
+
61
+ setInterval(() => {
62
+ moveObstacle();
63
+ checkCollision();
64
+ }, 20);