-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
38 lines (33 loc) · 916 Bytes
/
scripts.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
37
38
const dino = document.getElementById("dino");
const cactus = document.getElementById("cactus");
cactus.classList.add("block");
const jump = () => {
if (dino.classList !== "jump") {
dino.classList.add("jump");
setTimeout(() => {
dino.classList.remove("jump");
}, 300);
}
};
document.addEventListener("keydown", (event) => {
if (event.code === "Space") {
jump();
}
});
setInterval(() => {
// Get current dino Y position
let dinoTop = parseInt(window.getComputedStyle(dino).getPropertyValue("top"));
// Get current cactus X position
let cactusLeft = parseInt(
window.getComputedStyle(cactus).getPropertyValue("left")
);
// Detect collision
if (cactusLeft < 50 && cactusLeft > 0 && dinoTop >= 130) {
cactus.classList.remove("block");
if (window.confirm("Game over")) {
cactus.classList.add("block");
} else {
window.close();
}
}
}, 10);