|
1 | | -const character = document.getElementById("character"); |
2 | | -const block = document.getElementById("block"); |
3 | | -const block2 = document.getElementById("block2"); |
4 | | -const co = document.getElementById("co"); |
5 | | -var counter = 0; |
| 1 | +const character = document.getElementById('character'); |
| 2 | +const block = document.getElementById('block'); |
| 3 | +const block2 = document.getElementById('block2'); |
| 4 | +let counter = 0; |
6 | 5 |
|
7 | | -var je = setInterval(() => { |
8 | | - co.classList.add("animate2"); |
| 6 | +const jump = () => { |
| 7 | + if (character.classList.contains('animate')) return; |
| 8 | + character.classList.add('animate'); |
9 | 9 | setTimeout(() => { |
10 | | - co.classList.add("show"); |
11 | | - }, 2000); |
12 | | -}, 1000); |
13 | | -function jump() { |
14 | | - if (character.classList === "animate") { |
15 | | - return; |
16 | | - } |
17 | | - character.classList.add("animate"); |
18 | | - setTimeout(function () { |
19 | | - character.classList.remove("animate"); |
| 10 | + character.classList.remove('animate'); |
20 | 11 | }, 300); |
21 | | -} |
22 | | -function myFunction(event) { |
23 | | - var x = event.keyCode; |
24 | | - if (x == 32) { |
25 | | - jump(); |
26 | | - } |
27 | | -} |
28 | | -var checkDead = setInterval(function () { |
29 | | - let characterTop = parseInt( |
30 | | - window.getComputedStyle(character).getPropertyValue("top") |
| 12 | +}; |
| 13 | + |
| 14 | +// eslint-disable-next-line no-unused-vars |
| 15 | +const checkDead = setInterval(() => { |
| 16 | + const characterTop = parseInt( |
| 17 | + window.getComputedStyle(character).getPropertyValue('top'), |
| 18 | + 10, |
31 | 19 | ); |
32 | | - let blockLeft = parseInt( |
33 | | - window.getComputedStyle(block).getPropertyValue("left") |
| 20 | + const blockLeft = parseInt( |
| 21 | + window.getComputedStyle(block).getPropertyValue('left'), |
| 22 | + 10, |
34 | 23 | ); |
35 | 24 | if (blockLeft < 20 && blockLeft > -20 && characterTop >= 130) { |
36 | | - block.style.animation = "none"; |
37 | | - alert("Game Over. score: " + Math.floor(counter / 100)); |
| 25 | + block.style.animation = 'none'; |
| 26 | + alert(`Game Over. Score: ${Math.floor(counter / 100)}`); |
38 | 27 | counter = 0; |
39 | | - block.style.animation = "block 1s infinite linear"; |
40 | | - } else if (ka()) { |
| 28 | + block.style.animation = 'block 1s infinite linear'; |
41 | 29 | } else { |
42 | | - counter++; |
43 | | - document.getElementById("scoreSpan").innerHTML = Math.floor(counter / 100); |
| 30 | + counter += 1; |
| 31 | + document.getElementById('scoreSpan').innerText = Math.floor(counter / 100); |
44 | 32 | } |
45 | 33 | }, 10); |
46 | | -var add = setInterval(() => { |
47 | | - block2.classList.add("animate1"); |
| 34 | + |
| 35 | +const add = () => { |
| 36 | + block2.classList.add('animate1'); |
48 | 37 | setTimeout(() => { |
49 | | - block2.classList.remove("animate1"); |
| 38 | + block2.classList.remove('animate1'); |
50 | 39 | }, 9000); |
51 | | -}, 7000); |
| 40 | +}; |
52 | 41 |
|
53 | | -function ka() { |
54 | | - let characterTop = parseInt( |
55 | | - window.getComputedStyle(character).getPropertyValue("top") |
| 42 | +// Call the `add` function at regular intervals to animate block2 |
| 43 | +setInterval(add, 7000); |
| 44 | + |
| 45 | +// eslint-disable-next-line no-unused-vars |
| 46 | +const ka = () => { |
| 47 | + const characterTop = parseInt( |
| 48 | + window.getComputedStyle(character).getPropertyValue('top'), |
| 49 | + 10, |
56 | 50 | ); |
57 | | - let blockTop = parseInt( |
58 | | - window.getComputedStyle(block2).getPropertyValue("left") |
| 51 | + const blockTop = parseInt( |
| 52 | + window.getComputedStyle(block2).getPropertyValue('left'), |
| 53 | + 10, |
59 | 54 | ); |
60 | | - if (blockTop < 20 && characterTop == 100) { |
61 | | - block2.classList.remove("animate1"); |
62 | | - alert("Game Over. score: " + Math.floor(counter / 100)); |
| 55 | + if (blockTop < 20 && characterTop === 100) { |
| 56 | + block2.classList.remove('animate1'); |
| 57 | + alert(`Game Over. Score: ${Math.floor(counter / 100)}`); |
63 | 58 | counter = 0; |
64 | 59 | } |
65 | | -} |
66 | | -window.addEventListener("keydown", myFunction); |
| 60 | +}; |
| 61 | + |
| 62 | +window.addEventListener('keydown', (event) => { |
| 63 | + if (event.keyCode === 32) { |
| 64 | + jump(); |
| 65 | + } |
| 66 | +}); |
0 commit comments