-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (58 loc) · 1.65 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
65
66
67
const dino = document.querySelector('.dino');
const background = document.querySelector('.background');
let isJumping = false;
let position = 0;
function handleKeyup(event) {
if(event.keyCode === 32) {
if (!isJumping) {
jump();
}
}
}
function jump() {
isJumping = true;
let upInterval = setInterval(() => {
if (position >= 500) {
clearInterval(upInterval);
//Descendo
let downInterval = setInterval(() => {
if (position <= 150) {
clearInterval(downInterval);
isJumping = false;
} else {
position -= 20;
dino.style.bottom = position + 'px';
}
}, 20);
}else {
//Subindo
position += 40;
dino.style.bottom = position + 'px';
}
}, 40);
}
function createPtero () {
const ptero = document.createElement('div');
let pteroPosition = 1000;
let randomTime = Math.random() * 10000;
ptero.classList.add('ptero');
ptero.style.left = 1000 + 'px';
background.appendChild(ptero);
let leftInterval = setInterval(() => {
if (pteroPosition < -150) {
clearInterval(leftInterval);
background.removeChild(ptero);
} else if (pteroPosition > 0 && pteroPosition < 150 && position < 150){
// Game Over
clearInterval(leftInterval);
//document.body.innerHTML = '<h1 class="game-over">Game Over</h1>';
document.body.innerHTML = '<img class="game-over" src="img/background-gameover.png"/>';
} else {
pteroPosition -= 40;
ptero.style.left = pteroPosition + 'px';
}
}, 40);
setTimeout(createPtero, randomTime);
}
createPtero();
document.addEventListener('keyup', handleKeyup);