-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjogo.js
More file actions
123 lines (83 loc) · 2.82 KB
/
jogo.js
File metadata and controls
123 lines (83 loc) · 2.82 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var timerId = null; //váriavel que armazena a chamada da função timeout
function iniciaJogo(){
var url = window.location.search;
var nivel_jogo = url.replace("?", "");
var tempo_segundos = 0;
if (nivel_jogo == 1) {//1 easy -> 120seg
tempo_segundos = 120;
}
if (nivel_jogo == 2) {//2 medium -> 60seg
tempo_segundos = 60;
}
if (nivel_jogo == 3) {//3 insane -> 30seg
tempo_segundos = 30;
}
//inserindo segundos no span
document.getElementById('cronometro').innerHTML = tempo_segundos;
//quantidade de balões
var qtde_baloes = 80;
cria_baloes(qtde_baloes);
//imprimir qtde baloes inteiros
document.getElementById('baloes_inteiros').innerHTML = qtde_baloes;
document.getElementById('baloes_estourados').innerHTML = 0;
contagem_tempo(tempo_segundos + 1);
}
function contagem_tempo(segundos){
segundos = segundos - 1;
if(segundos == -1){
clearTimeout(timerId); //para a execução da função do settimeout
game_over();
return false;
}
document.getElementById('cronometro').innerHTML = segundos;
timerId = setTimeout("contagem_tempo("+segundos+")", 1000);
}
function remove_eventos_baloes() {
var i = 1; //contador para recuperar balões por id
//percorre o lementos de acordo com o id e só irá sair do laço quando não houver correspondência com elemento
while(document.getElementById('b'+i)) {
//retira o evento onclick do elemnto
document.getElementById('b'+i).onclick = '';
i++; //faz a iteração da variávei i
}
}
function game_over(){
remove_eventos_baloes();
alert('Game over!');
}situacao_jogo:
function cria_baloes(qtde_baloes){
for(var i = 1; i <= qtde_baloes; i++){
var balao = document.createElement('img');
balao.src = 'imagens/balao_azul_pequeno.png';
balao.style.margin = '10px';
balao.id = 'b'+i;
balao.onclick = function(){ estourar(this); };
document.getElementById('cenario').appendChild(balao);
}
}
function estourar (e){
var id_balao = e.id;
document.getElementById(id_balao).setAttribute("onclick", "");
document.getElementById(id_balao).src = 'imagens/balao_azul_pequeno_estourado.png';
pontuacao(-1);
}
function pontuacao(acao){
var baloes_inteiros = document.getElementById('baloes_inteiros').innerHTML;
var baloes_estourados = document.getElementById('baloes_estourados').innerHTML;
baloes_inteiros = parseInt(baloes_inteiros);
baloes_estourados = parseInt(baloes_estourados);
baloes_inteiros = baloes_inteiros + acao;
baloes_estourados = baloes_estourados - acao;
document.getElementById('baloes_inteiros').innerHTML = baloes_inteiros;
document.getElementById('baloes_estourados').innerHTML = baloes_estourados;
situacao_jogo(baloes_inteiros);
}
function situacao_jogo(baloes_inteiros){
if (baloes_inteiros == 0) {
alert('Parabéns');
parar_jogo();
}
}
function parar_jogo(){
clearTimeout(timerId);
}