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