-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquizzer.js
225 lines (200 loc) · 5.95 KB
/
quizzer.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
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
// inicializamos un array de arrays con la preguntas del juego.
var questions = [
[
"¿Que se utiliza para estilizar un sitio web?",
"Javascript",
"CSS",
"PHP",
"AngularJS",
1
],
[
"¿Qué tipo de lenguaje es PHP?",
"Interpretado",
"Compilado",
"Los 2 anteriores",
"Ninguno de los anteriores",
0
],
[
"¿jQuery es una biblioteca para qué lenguaje?",
"Python",
"PHP",
"Java",
"Ninguno de los anteriores",
3
],
[
"¿Cómo se marca el inicio de código PHP?",
"<?php",
"<?",
"Los 2 anteriores",
"Ninguno de los anteriores",
2
],
[
"¿Quién diseño Javascript?",
"Mark Zuckerberg",
"Bill Gates",
"Brendan Eich",
"Rasmus Lerdorf",
2
],
];
// Aquí utilizamos UnderscoreJS para generar un template de pregunta.
var questionTemplate = _.template(" \
<div class='card question'><span class='question'><%= question %></span> \
<ul class='options'> \
<li> \
<input type='radio' name='question[<%= index %>]' value='0' id='q<%= index %>o1'> \
<label for='q<%= index %>o1'><%= a %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='1' id='q<%= index %>o2'> \
<label for='q<%= index %>o2'><%= b %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='2' id='q<%= index %>o3'> \
<label for='q<%= index %>o3'><%= c %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='3' id='q<%= index %>o4'> \
<label for='q<%= index %>o4'><%= d %></label> \
</li> \
</ul> \
</div> \
");
// Definimos las variables de estado del juego y los valores iniciales (como el tiempo de respuesta de cada pregunta).
var points,
pointsPerQuestion,
currentQuestion,
questionTimer,
timeForQuestion = 8, // segundos
timeLeftForQuestion;
// Manipulacion de elementos con JQuery.
$(function() {
// Uso de jQuery para escuchar el evento click del botón de Comenzar y Volver a jugar.
$('button.start').click(start);
$('.play_again button').click(restart);
// La función restart inicializa los valores de las variables de estado del juego.
function restart() {
points = 0;
pointsPerQuestion = 10;
currentQuestion = 0;
timeLeftForQuestion = timeForQuestion;
// Se oculta la pantalla de finalizar y un mensaje que dice "Se acabó el tiempo".
$('.finish.card').hide();
$('div.start').show();
$('.times_up').hide();
generateCards();
updateTime();
updatePoints();
}
// La función start se ejecuta cuando el jugador hace click en comenzar.
function start() {
$('div.start').fadeOut(200, function() {
moveToNextQuestion();
});
}
// Esta es una de las funciones clave del juego, encargada de generar las preguntas.
function generateCards() {
$('.questions').html('');
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
var html = questionTemplate({
question: q[0],
index: i,
a: q[1],
b: q[2],
c: q[3],
d: q[4]
});
$('.questions').append(html);
};
// Indicamos que nos interesa el evento change de los inputs dentro de los elementos con clase question y card (cada una de las preguntas).
$('.question.card input').change(optionSelected);
}
// Esta función cambia el estado del juego para pasar de una pregunta a la siguiente.
function moveToNextQuestion() {
currentQuestion += 1;
if (currentQuestion > 1) {
$('.question.card:nth-child(' + (currentQuestion-1) + ')').hide();
}
// Se muestra la siguiente pregunta.
showQuestionCardAtIndex(currentQuestion);
setupQuestionTimer();
}
// Esta función inicializa el temporizador para responder una pregunta.
function setupQuestionTimer() {
if (currentQuestion > 1) {
clearTimeout(questionTimer);
}
timeLeftForQuestion = timeForQuestion;
// Cada 1 segundo, nuestro temporizador llamará a la función countdownTick().
questionTimer = setTimeout(countdownTick, 1000);
}
// Mostramos la tarjeta de pregunta correspondiente al índice que la función recibe por parámetro.
function showQuestionCardAtIndex(index) { // staring at 1
var $card = $('.question.card:nth-child(' + index + ')').show();
}
// La función countdownTick() se ejecuta cada un segundo, y actualiza el tiempo restante para responder en la pantalla del jugador.
function countdownTick() {
timeLeftForQuestion -= 1;
updateTime();
if (timeLeftForQuestion == 0) {
return finish();
}
questionTimer = setTimeout(countdownTick, 1000);
}
// Actualiza el tiempo restante en pantalla, utilizando la función html().
function updateTime() {
$('.countdown .time_left').html(timeLeftForQuestion + 's');
}
// Actualiza los puntos en pantalla.
function updatePoints() {
$('.points span.points').html(points + ' puntos');
}
// Esta función se ejecuta cuando el jugador escoge una respuesta.
function optionSelected() {
var selected = parseInt(this.value);
var correct = questions[currentQuestion-1][5];
if (selected == correct) {
points += pointsPerQuestion;
updatePoints();
correctAnimation();
} else {
wrongAnimation();
}
if (currentQuestion == questions.length) {
clearTimeout(questionTimer);
return finish();
}
moveToNextQuestion();
}
// Animación de respuesta correcta e incorrecta.
function correctAnimation() {
animatePoints('right');
}
// Animación de respuesta correcta e incorrecta.
function wrongAnimation() {
animatePoints('wrong');
}
// Esta función anima el puntaje en pantalla.
function animatePoints(cls) {
$('header .points').addClass('animate ' + cls);
setTimeout(function() {
$('header .points').removeClass('animate ' + cls);
}, 500);
}
// Cuando el juego termina, esta función es ejecutada.
function finish() {
if (timeLeftForQuestion == 0) {
$('.times_up').show();
}
$('p.final_points').html(points + ' puntos');
$('.question.card:visible').hide();
$('.finish.card').show();
}
// 24
restart();
});