-
Notifications
You must be signed in to change notification settings - Fork 4
/
wtp_gameplay.js
284 lines (256 loc) · 6.06 KB
/
wtp_gameplay.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Sets default scores and start time
let playerScore = 0;
let finalScore = 0;
let timeLeft = 30;
// Array of Petr names and their images
const quiz_bank = [
{
image: "0.jpg",
name: "Baker Petr"
},
{
image: "1.jpg",
name: "Dark Kermit Petr"
},
{
image: "2.jpg",
name: "Sally Petr"
},
{
image: "3.jpg",
name: "Oogie Boogie Petr"
},
{
image: "4.jpg",
name: "Snorlax Petr"
},
{
image: "5.jpg",
name: "Seaside Petr"
},
{
image: "6.jpg",
name: "Homer Petr"
},
{
image: "7.jpg",
name: "Snoopy Petr"
},
{
image: "8.jpg",
name: "Abducted Petr"
},
{
image: "9.jpg",
name: "Spring Day Petr"
},
{
image: "10.jpg",
name: "Graduation Petr"
},
{
image: "11.jpg",
name: "Thanos Petr"
},
{
image: "12.jpg",
name: "Light-Mode Discord Petr"
},
{
image:"13.jpg",
name: "Family Petr"
},
{
image: "14.jpg",
name: "Junkrat Petr"
},
{
image: "15.jpg",
name: "Boots Petr"
},
{
image: "16.jpg",
name: "Squidward Petr"
},
{
image: "17.jpg",
name: "SpongePetr SquarePants"
},
{
image: "18.jpg",
name: "KitKat Petr"
},
{
image: "19.jpg",
name: "Vanellope Von Schweetz Petr"
},
{
image: "20.jpg",
name: "Kaito Kid Petr"
},
{
image: "21.jpg",
name: "Tiny Headed Petr"
},
{
image: "22.jpg",
name: "Autumn Leaves Petr"
},
{
image: "23.jpg",
name: "Night Owl Petr"
},
{
image: "24.jpg",
name: "Goombas Petr"
},
{
image: "25.jpg",
name: "Kirby Petr"
},
{
image: "26.jpg",
name: "Grimace Shake Petr"
},
{
image: "27.jpg",
name: "Olivia Rodrigo Petr"
},
{
image: "28.jpg",
name: "Kuromi Petr"
},
{
image: "29.jpg",
name: "Hello Kitty Petr"
},
{
image: "30.jpg",
name: "Link Petr"
},
{
image: "31.jpg",
name: "South Park Petr"
},
{
image: "32.jpg",
name: "McDonalds Happy Meal Petrs"
},
{
image: "33.jpg",
name: "Barbie Petr"
},
{
image: "34.jpg",
name: "Hannie Petr"
},
{
image: "35.jpg",
name: "Engineering Tower Petr"
},
{
image: "36.jpg",
name: "Melting Petr"
},
{
image: "37.jpg",
name: "Unit Circle Petr"
},
{
image: "38.jpg",
name: "Crazy Petr"
},
{
image: "39.jpg",
name: "Violent Duck Petr"
}
]
// Shuffles quiz_bank so that the first four indexes are randomized and can be used as the buttons
// shuffle function from https://stackoverflow.com/a/2450976
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex > 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
function newQuestion() {
// Resets page
document.getElementById('quiz').innerHTML = "";
// Shuffles quiz_bank so that the first four indexes are randomized and can be used as the buttons
// Is a shuffled array of the quiz_bank, changed everytime
var shuffledArray = shuffle(quiz_bank);
// Array with 4 random values between 0-40
let random_variables = [];
var newRandom = shuffledArray[0];
var newRandom2 = shuffledArray[1];
var newRandom3 = shuffledArray[2];
var newRandom4 = shuffledArray[3];
random_variables.push(newRandom);
random_variables.push(newRandom2);
random_variables.push(newRandom3);
random_variables.push(newRandom4);
// Initially blurs the image
document.getElementById("image").style.filter = "blur(10Px)";
// Sets the current, correct answer to one of the four buttons
var current = random_variables[Math.floor(Math.random() * 3)];
document.getElementById("image").innerHTML = `
<image src="imgs/${current.image}"></image>
`;
// Checks if the button clicked is the correct answer
document.getElementById('quiz').innerHTML += `
<div>
<div>
<button onclick="selected(${newRandom == current})">${newRandom.name}</button>
<button onclick="selected(${newRandom2 == current})">${newRandom2.name}</button>
<button onclick="selected(${newRandom3 == current})">${newRandom3.name}</button>
<button onclick="selected(${newRandom4 == current})">${newRandom4.name}</button>
</div>
</div>
`;
}
// Loads a new screen
function loaded(){
newQuestion();
shuffle(quiz_bank);
let timer = setInterval(function() {
timeLeft -= 1;
document.getElementById("timer").innerText = `Time: ${timeLeft} `;
if(timeLeft <= 0) {
clearInterval(timer);
endScene();
}
}, 1000);
}
// Checks if the correct answer is chosen and updates the score by 1
function selected(correct){
console.log("user seleced correct value",correct)
if(correct) {
playerScore+=1;
}
document.getElementById("image").style.filter = "blur(0Px)";
setTimeout(
function() {
updateScore();
newQuestion();
}, 500);
}
// Loads the end screen
function endScene() {
localStorage.setItem("score", playerScore);
window.location = 'wtp_end_scene.html';
}
// Updates the html of the score to match
function updateScore() {
document.getElementById("counter").innerText = `Score: ${playerScore}`;
}
// Displays the number of Petrs guessed at the end screen
function endLoad() {
document.getElementById("finalScore").innerText = `You found ${localStorage.getItem("score")} Petrs!`;
}