Skip to content

Commit 1b22edf

Browse files
committed
add functionality
1 parent e49206f commit 1b22edf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Source-Code/DinosaurGame/script.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const character = document.querySelector(".dino");
2+
const block = document.querySelector(".cactus");
3+
4+
const jump = () => {
5+
// Add class to initiate jump
6+
character.classList.add("animate");
7+
8+
// Remove class after animation duration (500ms)
9+
setTimeout(() => {
10+
character.classList.remove("animate");
11+
}, 500);
12+
};
13+
14+
// Trigger jump on spacebar press
15+
document.addEventListener("keydown", (event) => {
16+
if (event.code === "Space") {
17+
jump();
18+
}
19+
});
20+
21+
// Check for collision
22+
const checkDead = setInterval(() => {
23+
const blockLeft = parseInt(
24+
window.getComputedStyle(block).getPropertyValue("left")
25+
);
26+
const characterTop = parseInt(
27+
window.getComputedStyle(character).getPropertyValue("top")
28+
);
29+
30+
// Check for collision
31+
if (blockLeft < 20 && blockLeft > 0 && characterTop >= 178) {
32+
block.style.animation = "none";
33+
block.style.display = "none";
34+
alert("Uh..Oh, you lose.");
35+
clearInterval(checkDead); // Stop checking for collisions
36+
}
37+
}, 100);

0 commit comments

Comments
 (0)