|
| 1 | +const character = document.getElementById('character'); |
| 2 | +const block = document.getElementById('block'); |
| 3 | +const block2 = document.getElementById('block2'); |
| 4 | +let counter = 0; |
| 5 | + |
| 6 | +const jump = () => { |
| 7 | + if (character.classList.contains('animate')) return; |
| 8 | + character.classList.add('animate'); |
| 9 | + setTimeout(() => { |
| 10 | + character.classList.remove('animate'); |
| 11 | + }, 300); |
| 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, |
| 19 | + ); |
| 20 | + const blockLeft = parseInt( |
| 21 | + window.getComputedStyle(block).getPropertyValue('left'), |
| 22 | + 10, |
| 23 | + ); |
| 24 | + if (blockLeft < 20 && blockLeft > -20 && characterTop >= 130) { |
| 25 | + block.style.animation = 'none'; |
| 26 | + alert(`Game Over. Score: ${Math.floor(counter / 100)}`); |
| 27 | + counter = 0; |
| 28 | + block.style.animation = 'block 1s infinite linear'; |
| 29 | + } else { |
| 30 | + counter += 1; |
| 31 | + document.getElementById('scoreSpan').innerText = Math.floor(counter / 100); |
| 32 | + } |
| 33 | +}, 10); |
| 34 | + |
| 35 | +const add = () => { |
| 36 | + block2.classList.add('animate1'); |
| 37 | + setTimeout(() => { |
| 38 | + block2.classList.remove('animate1'); |
| 39 | + }, 9000); |
| 40 | +}; |
| 41 | + |
| 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, |
| 50 | + ); |
| 51 | + const blockTop = parseInt( |
| 52 | + window.getComputedStyle(block2).getPropertyValue('left'), |
| 53 | + 10, |
| 54 | + ); |
| 55 | + if (blockTop < 20 && characterTop === 100) { |
| 56 | + block2.classList.remove('animate1'); |
| 57 | + alert(`Game Over. Score: ${Math.floor(counter / 100)}`); |
| 58 | + counter = 0; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +window.addEventListener('keydown', (event) => { |
| 63 | + if (event.keyCode === 32) { |
| 64 | + jump(); |
| 65 | + } |
| 66 | +}); |
0 commit comments