-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (36 loc) · 1.1 KB
/
Copy pathscript.js
File metadata and controls
41 lines (36 loc) · 1.1 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
document.addEventListener('DOMContentLoaded', () => {
const character = document.querySelector('.dino');
const block = document.querySelector('.cactus');
const jump = () => {
// Add class to initiate jump
character.classList.add('animate');
// Remove class after animation duration (500ms)
setTimeout(() => {
character.classList.remove('animate');
}, 500);
};
// Trigger jump on spacebar press
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
jump();
}
});
// Check for collision
const checkDead = setInterval(() => {
const blockLeft = parseInt(
window.getComputedStyle(block).getPropertyValue('left'),
10,
);
const characterTop = parseInt(
window.getComputedStyle(character).getPropertyValue('top'),
10,
);
// Check for collision
if (blockLeft < 20 && blockLeft > 0 && characterTop >= 178) {
block.style.animation = 'none';
block.style.display = 'none';
alert('Uh..Oh, you lose.');
clearInterval(checkDead); // Stop checking for collisions
}
}, 100);
});