-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.js
More file actions
207 lines (172 loc) · 5.94 KB
/
app.js
File metadata and controls
207 lines (172 loc) · 5.94 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
let listaDeNumerosSorteados = [];
let numeroLimite = parseInt(localStorage.getItem("dificuldade")) || 10;
let numeroSecreto = gerarNumeroAleatorio();
let tentativas = 1;
function exibirTextoNaTela(selector, message) {
let campo = document.querySelector(selector);
campo.innerHTML = message;
if ("speechSynthesis" in window) {
let utterance = new SpeechSynthesisUtterance(message);
// Ajusta a linguagem da voz de acordo com o idioma atual
utterance.lang = idiomaAtual;
utterance.rate = 1.2;
window.speechSynthesis.speak(utterance);
} else {
console.log("Web Speech API não suportada neste navegador.");
}
}
exibirMensagemInicial();
function verificarChute() {
let chute = parseInt(document.querySelector("#input-numero").value);
if (chute === numeroSecreto) {
let palavraTentativas =
tentativas === 1
? idiomaAtual === "pt-BR"
? "tentativa"
: "attempt"
: idiomaAtual === "pt-BR"
? "tentativas"
: "attempts";
exibirTextoNaTela("h1", t("acertou"));
exibirTextoNaTela(
"p",
t("tentativas", {
tentativas: tentativas,
palavraTentativas,
})
);
// --- Corrected, Combined Code ---
// High score logic
let recorde = localStorage.getItem('recordeTentativas');
if (!recorde || tentativas < recorde) {
localStorage.setItem('recordeTentativas', tentativas);
exibirRecorde(); // Update the record display immediately
}
// Twitter share logic
let mensagemTweet = `I hit the secret number in ${tentativas} attempts! Play also at #SecretNumberGame.`;
let urlTwitter = `https://twitter.com/intent/tweet?text=${encodeURIComponent(mensagemTweet)}&url=https://github.com/adalbertobrant/secretgamenumber`;
let shareButton = document.getElementById('share-twitter');
shareButton.href = urlTwitter;
shareButton.style.display = 'inline-flex';
document.getElementById("reiniciar").removeAttribute("disabled");
} else {
exibirTextoNaTela(
"p",
chute > numeroSecreto ? t("numeroMenor") : t("numeroMaior")
);
tentativas++;
limparCampo();
}
}
function gerarNumeroAleatorio() {
let numeroEscolhido = parseInt(Math.random() * maxnumero + 1);
let tamanhoDaLista = listaDeNumerosSorteados.length;
if (tamanhoDaLista === maxnumero)
listaDeNumerosSorteados = [];
if (listaDeNumerosSorteados.includes(numeroEscolhido)) {
return gerarNumeroAleatorio();
}
listaDeNumerosSorteados.push(numeroEscolhido);
return numeroEscolhido;
}
function limparCampo() {
document.querySelector("#input-numero").value = "";
}
function reiniciarJogo() {
numeroSecreto = gerarNumeroAleatorio();
limparCampo();
tentativas = 1;
exibirMensagemInicial();
document.getElementById("reiniciar").setAttribute("disabled", true);
document.getElementById('share-twitter').style.display = 'none';
}
function exibirRecorde() {
let recorde = localStorage.getItem("recordeTentativas");
if (recorde) {
let palavraTentativas =
recorde == 1
? idiomaAtual === "pt-BR"
? "tentativa"
: "attempt"
: idiomaAtual === "pt-BR"
? "tentativas"
: "attempts";
// This now only targets the <span> to update just the number
document.getElementById(
"record"
).innerHTML = `${recorde} ${palavraTentativas}`;
}
}
function exibirMensagemInicial() {
exibirTextoNaTela("h1", t("titulo"));
exibirTextoNaTela(
"p",
t("escolhaNumero", {
limite: maxnumero,
})
);
document.getElementById("btn-chutar").innerText = t("btnChutar");
document.getElementById("reiniciar").innerText = t("btnNovoJogo");
// Atualizar o máximo do input
document.getElementById("input-numero").setAttribute("max", numeroLimite);
// Definir o valor correto no select de dificuldade
document.getElementById("select-dificuldade").value = numeroLimite;
exibirRecorde();
}
function mudarDificuldade(novaDificuldade) {
numeroLimite = parseInt(novaDificuldade);
localStorage.setItem("dificuldade", numeroLimite);
// Reiniciar o jogo com a nova dificuldade
listaDeNumerosSorteados = [];
numeroSecreto = gerarNumeroAleatorio();
tentativas = 1;
// Atualizar a interface
exibirMensagemInicial();
limparCampo();
document.getElementById("reiniciar").setAttribute("disabled", true);
}
// Função para alternar a visibilidade do modal de créditos
function toggleModalCreditos() {
const modal = document.getElementById("modal-creditos");
if (modal.style.display === "flex") {
modal.style.display = "none";
} else {
modal.style.display = "flex";
}
}
// Fechar modal ao clicar fora do conteúdo
window.onclick = function (event) {
const modal = document.getElementById("modal-creditos");
if (event.target === modal) {
modal.style.display = "none";
}
}
// Listen for any key press on the whole page
document.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
const restartButton = document.getElementById('reiniciar');
const inputField = document.querySelector('input');
// If the restart button is enabled → restart the game
if (!restartButton.disabled) {
reiniciarJogo();
}
// Otherwise, if input has focus make a guess
else if (document.activeElement === inputField) {
verificarChute();
}
}
});
// --- Parallax Effect Logic ---
document.addEventListener('mousemove', function(e) {
const background = document.getElementById('background-parallax');
// Calculate movement strength (lower number = more movement)
const moveStrength = 50;
// Get screen center coordinates
const centerX = window.innerWidth / 2;
const centerY = window.innerHeight / 2;
// Calculate mouse position offset from the center
const moveX = (e.clientX - centerX) / moveStrength;
const moveY = (e.clientY - centerY) / moveStrength;
// Apply the transformation
background.style.transform = `translate(${moveX}px, ${moveY}px)`;
});