forked from tajulafreen/50Projects-HTML-CSS-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (60 loc) · 1.75 KB
/
script.js
File metadata and controls
66 lines (60 loc) · 1.75 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const character = document.getElementById('character');
const block = document.getElementById('block');
const block2 = document.getElementById('block2');
let counter = 0;
const jump = () => {
if (character.classList.contains('animate')) return;
character.classList.add('animate');
setTimeout(() => {
character.classList.remove('animate');
}, 300);
};
// eslint-disable-next-line no-unused-vars
const checkDead = setInterval(() => {
const characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue('top'),
10,
);
const blockLeft = parseInt(
window.getComputedStyle(block).getPropertyValue('left'),
10,
);
if (blockLeft < 20 && blockLeft > -20 && characterTop >= 130) {
block.style.animation = 'none';
alert(`Game Over. Score: ${Math.floor(counter / 100)}`);
counter = 0;
block.style.animation = 'block 1s infinite linear';
} else {
counter += 1;
document.getElementById('scoreSpan').innerText = Math.floor(counter / 100);
}
}, 10);
const add = () => {
block2.classList.add('animate1');
setTimeout(() => {
block2.classList.remove('animate1');
}, 9000);
};
// Call the `add` function at regular intervals to animate block2
setInterval(add, 7000);
// eslint-disable-next-line no-unused-vars
const ka = () => {
const characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue('top'),
10,
);
const blockTop = parseInt(
window.getComputedStyle(block2).getPropertyValue('left'),
10,
);
if (blockTop < 20 && characterTop === 100) {
block2.classList.remove('animate1');
alert(`Game Over. Score: ${Math.floor(counter / 100)}`);
counter = 0;
}
};
window.addEventListener('keydown', (event) => {
if (event.keyCode === 32) {
jump();
}
});