-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
80 lines (72 loc) · 2.01 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
//Definindo o Mario e Background
const mario = document.querySelector('.mario');
const backg = document.querySelector('.backg');
let isJumping = false;
let isGameOver = false;
let position = 0;
//Atribuindo o controle do Game, atraves de uma tecla "Space"
function handleKeyUp(event) {
if (event.keyCode === 87) {
if (!isJumping) {
jump();
}
}
}
//Configurando a movimentação do Mario
function jump() {
isJumping = true;
let upInterval = setInterval(() => {
if (position >= 150) {
clearInterval(upInterval)
//Descer
let downInterval = setInterval(() => {
if (position <= 0) {
clearInterval(downInterval);
isJumping = false;
} else {
position -= 20;
mario.style.bottom = position + 'px';
}
}, 20);
} else {
//Subir
position += 20;
mario.style.bottom = position + 'px';
}
}, 20)
}
//Configurando a movimentação da Tortuga
function createTortuga() {
const tortuga = document.createElement('div');
let tortugaPosition = 1000;
let randomTime = Math.random() * 6000;
//Condição de GameOver
if (isGameOver) return;
tortuga.classList.add('tortuga');
backg.appendChild(tortuga);
tortuga.style.left = tortugaPosition + 'px';
let leftInterval = setInterval(() => {
//Removendo a Tortuga da Tela
if (tortugaPosition < -60) {
clearInterval(leftInterval);
backg.removeChild(tortuga);
} else if (tortugaPosition > 0 && tortugaPosition < 60 && position < 60) {
//GameOver
clearInterval(leftInterval);
document.body.innerHTML = '<h1 class="game-over">!💀GAME OVER💀!</h1>';
} else {
tortugaPosition -= 10;
tortuga.style.left = tortugaPosition + 'px';
}
}, 20);
setTimeout(createTortuga, randomTime);
}
/* Sistema de Pontuação
function score() {
let date = new Date();
let time = date.getSeconds();
point += time
}
const createClock = setInterval(score, 1000);*/
createTortuga();
document.addEventListener('keyup', handleKeyUp)