-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (72 loc) · 2.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const dino = document.querySelector( '.dino' );
const bg = document.querySelector( '.bg' );
let isJumping = false;
let isGameOver = false;
let position = 0;
let score = 0;
function handleKeyUp(event)
{
if( event.keyCode === 32){
if( !isJumping ){
pular();
}
}
}
function pular()
{
isJumping = true;
let upInterval = setInterval(() => {
if( position >= 150 ){
setScore();
clearInterval(upInterval);
let downInterval = setInterval(() => {
if( position <= 0){
clearInterval(downInterval);
isJumping = false;
}else{
position -= 20;
dino.style.bottom = position+'px';
}
}, 20);
}else{
position += 20;
dino.style.bottom = position+'px';
}
}, 20);
}
function criarCacto()
{
const cactus = document.createElement('div');
let cactusPosition = 1000;
let randomTime = Math.random() * 6000;
if (isGameOver) return;
cactus.classList.add('cacto');
bg.appendChild(cactus);
cactus.style.left = cactusPosition + 'px';
let leftTimer = setInterval(() => {
if (cactusPosition < -60){
clearInterval(leftTimer);
background.removeChild(cactus);
}else
if(cactusPosition > 0 && cactusPosition < 60 && position < 60){
clearInterval(leftTimer);
isGameOver = true;
document.body.innerHTML = '<h1 class="game-over">Fim de jogo <br> Você fez '+score+' pontos</h1> <a onclick="restart()" class="button"> Reiniciar o Jogo</a>';
}else{
cactusPosition -= 10;
cactus.style.left = cactusPosition + 'px';
}
}, 30);
setTimeout(criarCacto, randomTime);
}
function setScore()
{
score += 1;
document.querySelector( '.pontuacao' ).innerHTML = score;
}
function restart()
{
window.location.reload();
}
criarCacto();
document.addEventListener('keyup', handleKeyUp);