-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (31 loc) · 922 Bytes
/
script.js
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
const dino = document.getElementById("dino");
const rock = document.getElementById("rock");
const score = document.getElementById("score");
function jump() {
dino.classList.add("jump-animation");
setTimeout(() => {
dino.classList.remove("jump-animation");
}, 500);
}
document.addEventListener("keypress", () => {
if (!dino.classList.contains("jump-animation")) {
jump();
}
});
setInterval(() => {
const dinoTop = parseInt(window.getComputedStyle(dino)
.getPropertyValue('top'));
const rockLeft = parseInt(window.getComputedStyle(rock)
.getPropertyValue('left'));
score.innerText++;
if (rockLeft < 0) {
rock.style.display = 'none';
} else {
rock.style.display = ''
}
if (rockLeft < 50 && rockLeft > 0 && dinoTop > 150) {
alert("You got a score of: " + score.innerText +
"\n\nPlay again?");
location.reload();
}
}, 50);