forked from MuriloHideki/DinoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (57 loc) · 1.75 KB
/
script.js
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
const dino = document.querySelector('.dino');
const background = document.querySelector('.background');
let onfloor = true;
let position = 0;
function handleKeyDown(event) {
if (event.keyCode == 32) {
if (onfloor) {
jump();
}
}
}
function jump() {
onfloor = false;
let upinterval = setInterval(() => {
if (position >= 150) {
clearInterval(upinterval);
//Descendo
let downinterval = setInterval(() => {
if (position <= 0) {
clearInterval(downinterval);
onfloor = true;
} else {
position -= 20;
dino.style.bottom = position + 'px';
}
}, 20);
} else {
//Subindo
position += 20;
dino.style.bottom = position + 'px';
}
}, 20);
}
function createCactus() {
const cactus = document.createElement('div');
let cactusPosition = 1000;
let RNG = Math.random() * 6000;
cactus.classList.add('cactus');
cactus.style.left = 1000 + 'px';
background.appendChild(cactus);
let leftInterval = setInterval(() => {
if (cactusPosition < -60) {
clearInterval(leftInterval);
background.removeChild(cactus);
} else if (cactusPosition > 0 && cactusPosition < 60 && position < 60) {
//Game over
clearInterval(leftInterval);
document.body.innerHTML = '<h1 class="game-over">Fim de jogo</h1>';
} else {
cactusPosition -= 10;
cactus.style.left = cactusPosition + 'px';
}
}, 20);
setTimeout(createCactus, RNG);
}
createCactus();
document.addEventListener('keydown', handleKeyDown);