-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
144 lines (119 loc) · 4.59 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
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
// Axel Cotón Gutiérrez Copyright 2023
// Cargar archivos de audio desde la carpeta adecuada
const preguntaAudio = new Audio('https://raw.githubusercontent.com/AxelCotonGutierrez/Contarhasta3recta/master/audio/Pregunta.mp3');
const correctoAudio = new Audio('https://raw.githubusercontent.com/AxelCotonGutierrez/Contarhasta3recta/master/audio/Correcto.mp3');
const incorrectoAudio = new Audio('https://raw.githubusercontent.com/AxelCotonGutierrez/Contarhasta3recta/master/audio/Incorrecto.mp3');
const felicidadesAudio = new Audio('https://raw.githubusercontent.com/AxelCotonGutierrez/Contarhasta3recta/master/audio/Felicidades.mp3');
const intentarAudio = new Audio('https://raw.githubusercontent.com/AxelCotonGutierrez/Contarhasta3recta/master/audio/Intentar.mp3');
// Acceder al botón de silencio y al icono del megáfono en el DOM
const soundControl = document.querySelector('#sound-control');
const megaphoneIcon = document.querySelector('#megaphone-icon');
// Función para reproducir audio si el sonido está activado
function playAudio(audioElement) {
if (soundControl.checked) {
audioElement.play();
}
}
// Evento clic para el icono del megáfono para reproducir la pregunta en audio
megaphoneIcon.addEventListener('click', () => playAudio(preguntaAudio));
document.addEventListener('DOMContentLoaded', function() {
const numbers = document.querySelectorAll('.number');
const startButton = document.getElementById('start-button');
const questionElement = document.getElementById('question');
const resultElement = document.getElementById('result');
const playAgainButton = document.getElementById('play-again-button');
const scoreElement = document.getElementById('score'); // Asegúrate de que este elemento exista en tu HTML
let randomNumber;
let previousRandomNumber;
let score = 0;
let questionsCount = 0;
let isGameRunning = false;
let isGameStarted = false;
function resetGame() {
numbers.forEach(number => {
number.classList.remove('selected');
});
resultElement.innerHTML = '';
scoreElement.textContent = ''; // Actualizar el texto de scoreElement
score = 0;
questionsCount = 0;
isGameRunning = false;
playAgainButton.style.display = 'none';
}
function getRandomNumber() {
let newRandomNumber = Math.floor(Math.random() * 3) + 1;
while (newRandomNumber === previousRandomNumber) {
newRandomNumber = Math.floor(Math.random() * 3) + 1;
}
previousRandomNumber = newRandomNumber;
return newRandomNumber;
}
function handleClick() {
if (!isGameRunning) {
return;
}
const selectedNumber = parseInt(this.textContent);
if (selectedNumber === randomNumber) {
this.classList.add('selected');
resultElement.innerHTML = '<div class="message-container"><span class="msj correcto">¡Correcto!</span></div>';
playAudio(correctoAudio);
score++;
} else {
this.classList.add('selected');
resultElement.innerHTML = '<div class="message-container"><span class="msj incorrecto">Incorrecto</span></div>';
playAudio(incorrectoAudio);
}
questionsCount++;
if (questionsCount === 5) {
questionElement.textContent = `¡Juego completado! Preguntas acertadas: ${score} de ${questionsCount}.`;
if (score === 5) {
playAudio(felicidadesAudio);
} else {
playAudio(intentarAudio);
}
isGameRunning = false;
playAgainButton.style.display = 'block';
} else {
setTimeout(() => {
resultElement.innerHTML = '';
generateQuestion();
}, 1000);
}
}
function handleStart() {
if (!isGameStarted) {
isGameStarted = true;
startButton.style.display = 'none';
isGameRunning = true;
generateQuestion();
}
}
function generateQuestion() {
numbers.forEach(number => {
number.classList.remove('selected');
});
randomNumber = getRandomNumber();
questionElement.textContent = `¿${randomNumber}?`;
}
function handlePlayAgain() {
resetGame();
isGameStarted = true;
isGameRunning = true;
generateQuestion();
}
numbers.forEach(number => {
number.addEventListener('click', handleClick);
});
startButton.addEventListener('click', handleStart);
playAgainButton.addEventListener('click', handlePlayAgain);
resetGame();
startButton.style.display = 'block';
});
// Navegaciòn"
document.addEventListener('DOMContentLoaded', function() {
var menuToggle = document.querySelector('.menu-toggle');
var menu = document.querySelector('.menu');
menuToggle.addEventListener('click', function() {
menu.classList.toggle('active');
});
});