-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
227 lines (198 loc) · 6.83 KB
/
script.js
File metadata and controls
227 lines (198 loc) · 6.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const localPokemons = document.querySelector('.local-pokemon');
const localLoading = document.querySelector('.local-loading');
const localChances = document.querySelector('.container-chances');
const localRespostas = document.querySelector('.container-resposta');
const localTimer = document.querySelector('.local-timer');
const localInicio = document.querySelector('.local-inicio');
const localFooter = document.querySelector('#local-footer');
const localBtnInicio = document.querySelector('.local-buttons-inicio');
const comoJogar = document.querySelector('.local-como-jogar');
const localCreditos = document.querySelector('.local-creditos');
//Btns de inicio
const btnIniciarJogo = document.querySelector('#btnModal');
const logoPokemon = document.querySelector('#logo-pokemon');
const btnInicio = document.querySelector('#btn-iniciar');
const btnFaq = document.querySelector('#btn-faq');
const btnCreditos = document.querySelector('#btn-creditos');
//Area Crediso
const iconeLinkedin = document.querySelector('#icon-linkendin');
const iconeGithub = document.querySelector('#icon-github');
const timerP = document.querySelector('#timer')
const localPrimeira = document.querySelector('.local-primeira');
const localSegunda = document.querySelector('.local-segunda');
const localErros = document.querySelector('#local-erros');
const localAcertos = document.querySelector('#local-acertos');
let v = '', chancesV = [], posi = 3, acertos = 0, erros = 0, check = false;
const criarPokemons = (number, chances) => {
const api = `https://pokeapi.co/api/v2/pokemon/${number}`;
fetch(api)
.then((response) => {
return response.json();
})
.then((data) => {
const imagem = data['sprites']['other']['official-artwork']['front_default'];
const nome = data['name'];
chancesV.push(nome);
if (chances !== 1) { criarPokemonUx(imagem, nome); }
if (chancesV.length === 4) { criarChances(); }
})
.catch(() => {
resetarPartida();
localInicio.classList.add('class-hide');
localLoading.classList.remove('class-hide');
localFooter.classList.remove('class-hide');
})
};
// Cria as chances
const criarChances = () => {
for (let i = 1; i <= 4; i += 1) {
const name = Math.trunc(Math.random() * chancesV.length);
const chance = document.createElement('button');
chance.className = 'btn btn-outline-light chances';
chance.innerText = chancesV[name].toUpperCase();
(i > 2) ? localPrimeira.appendChild(chance) : localSegunda.appendChild(chance);
chance.addEventListener('click', clickBtn);
chancesV.splice(name, 1);
}
};
// Cria a imagem
const criarPokemonUx = (imagem, nome) => {
v = nome;
const imgE = document.createElement('img');
imgE.className = 'pokemons pokemon-filter'
imgE.setAttribute('src', imagem);
imgE.setAttribute('draggable', 'false')
localPokemons.appendChild(imgE);
};
// Gera a batalha , dos dois
const gerarBatalha = () => {
let conta = 0;
const c = Math.trunc(Math.random() * 898);
criarPokemons(c, 0);
for (let i = 0; i < posi; i += 1) {
const c = Math.trunc(Math.random() * 898);
criarPokemons(c, 1);
conta += 1;
}
if (conta === 3) { loadingEfect(); }
};
// Reseta partida
const resetarPartida = () => {
localPokemons.innerHTML = '';
localPrimeira.innerHTML = '';
localSegunda.innerHTML = '';
localRespostas.innerHTML = '';
addHide();
gerarBatalha();
};
// Dar Hide nas propriedades
const addHide = () => {
localTimer.classList.add('class-hide');
localPokemons.classList.add('class-hide');
localChances.classList.add('class-hide');
localRespostas.classList.add('class-hide');
localLoading.classList.remove('class-hide');
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
// Timer do loading
async function loadingEfect() {
await sleep(2000);
localTimer.classList.remove('class-hide');
localPokemons.classList.remove('class-hide');
localChances.classList.remove('class-hide');
localRespostas.classList.remove('class-hide');
localLoading.classList.add('class-hide');
timerZ();
};
// Efeito de opacidade do pokemon
async function efect(verificar) {
check = true;
timerP.innerHTML = ``;
localPrimeira.innerHTML = '';
localSegunda.innerHTML = '';
const imgPoke = document.querySelector('.pokemons');
for (let i = 10; i <= 100; i += 10) {
imgPoke.style.opacity = `${i}%`
await sleep(150);
}
const nameDoCerto = document.createElement('p');
if (verificar === 'acertou') {
nameDoCerto.style.backgroundColor = 'rgba(9, 255, 0, 0.300)';
localAcertos.innerText = acertos;
} else {
nameDoCerto.style.backgroundColor = 'rgba(255, 0, 0, 0.3)';
localErros.innerText = erros;
}
nameDoCerto.innerText = v.toUpperCase();
nameDoCerto.className = 'text-container';
localRespostas.appendChild(nameDoCerto);
await sleep(2000);
resetarPartida();
check = false;
};
// Aqui e o tempo
async function timerZ() {
timerP.innerHTML = `00:20`;
await sleep(1000);
for (let i = 19; i >= 0; i -= 1) {
if (check === true) { return; }
(i < 10) ? timerP.innerHTML = `00:0${i}` : timerP.innerHTML = `00:${i}`;
await sleep(1000);
}
const imgPoke = document.querySelector('.pokemons');
imgPoke.classList.remove('pokemon-filter');
efect();
erros += 1;
localErros.innerText = erros;
timerP.innerHTML = ``;
};
const clickBtn = (event) => {
const element = event.target;
const imgPoke = document.querySelector('.pokemons');
imgPoke.classList.remove('pokemon-filter');
if (element.innerText.toLowerCase() === v) {
efect('acertou');
acertos += 1;
return;
}
efect('errou');
erros += 1;
};
// Inicio do jogo
let myModal = new bootstrap.Modal(document.getElementById('menuInicio'), { backdrop: 'static', keyboard: false });
logoPokemon.addEventListener('click', () => {
window.location.href = 'index.html';
});
btnInicio.addEventListener('click', () => {
myModal.hide();
gerarBatalha();
localLoading.classList.remove('class-hide');
localFooter.classList.remove('class-hide');
localInicio.classList.add('class-hide');
comoJogar.classList.add('class-hide');
});
btnFaq.addEventListener('click', () => {
comoJogar.classList.remove('class-hide');
localBtnInicio.classList.add('class-hide');
localBtnInicio.classList.add('class-hide');
});
btnCreditos.addEventListener('click', () => {
localCreditos.classList.remove('class-hide');
localBtnInicio.classList.add('class-hide');
comoJogar.classList.add('class-hide');
});
const pausarJogo = () => {
window.location.href = './index.html';
};
document.addEventListener('keypress', (e) => (e.key === 'p' || e.key === 'P') ?
pausarJogo() : 1);
// Area creditos
iconeLinkedin.addEventListener('click', () => {
window.location.href = 'https://www.linkedin.com/in/luizwanderson/';
});
iconeGithub.addEventListener('click', () => {
window.location.href = 'https://github.com/ProHatp';
});
myModal.show();