-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript-adults.js
More file actions
354 lines (266 loc) · 8.96 KB
/
script-adults.js
File metadata and controls
354 lines (266 loc) · 8.96 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
let cards = document.querySelectorAll('.innercard');
let lockBoard = false;
let hasFlippedCard = false;
let firstCard, secondCard;
let flipButton = document.getElementById("see-cards");
//Sound effects
let victorySound = new Audio("sounds/victory.mp3");
let finalVictorySound = new Audio("sounds/celebrate.mp3");
let noMatchSound = new Audio("sounds/incorrect.mp3");
let gameOverSound = new Audio("sounds/game_over.mp3");
let netflixSound = new Audio("sounds/Netflix_Audio.mp3");
let celebritiesSound = new Audio("sounds/celebrities.mp3");
let moviesSound = new Audio("sounds/movies.mp3");
let brandsSound = new Audio("sounds/brands.mp3");
let allSound = new Audio("sounds/Yes.mp3");
let scoreFinal = 40;
let timeFinal = 70;
let score;
let timeLeft;
let scoreField = document.getElementById('score');
let scoreFieldPopup = document.getElementById('score-pop');
let timeFieldPopup = document.getElementById('time-pop');
let congratulationsPop = document.getElementById("popup1");
let gameOverPop = document.getElementById("popup2");
let closeicon = document.querySelector(".close");
let matchedCard = document.getElementsByClassName("match");
if(window.location.href.includes('ad1-Netflix')){ //first page
sessionStorage.setItem('score', 30);
sessionStorage.setItem('timeLeft', 60);
}
function getScore() {
score = sessionStorage.getItem('score');
score = Number(score);
setScore();
}
function setScore(){ //show score and save score to sessionStorage
scoreField.innerText = score;
scoreFieldPopup.innerText = score;
sessionStorage.setItem('score', score);
}
getScore();
let nextLevelButton = document.querySelector("#next-level");
if(nextLevelButton){
nextLevelButton.addEventListener('click', savedScore);
nextLevelButton.addEventListener('click', savedTimeLeft);
}
function savedScore (){
score = sessionStorage.getItem('score');
score = Number(score) + scoreFinal;
setScore();
}
function reloadScore (){
sessionStorage.setItem('score', 30);
}
//Initial Timer
let firstTimer = 7;
let initialTimer = setInterval(function () {
document.getElementById("countdown").innerHTML = firstTimer;
firstTimer -= 1;
if (firstTimer <= -1) {
clearInterval(initialTimer);
document.getElementById("countdown").innerHTML = "GO!";
getTimeLeft(); // after initial timer ends, starts Game timer
}
}, 1000);
function getTimeLeft() {
timeLeft = sessionStorage.getItem('timeLeft');
timeLeft = Number(timeLeft); //+ 10; //When i get to a new page i add ten to the score
gameTimer();
// setTimeLeft();
}
function setTimeLeft(){ //save time to sessionStorage
countdown.innerText = timeLeft;
sessionStorage.setItem('timeLeft', timeLeft);
}
function savedTimeLeft (){
timeLeft = sessionStorage.getItem('timeLeft');
timeLeft = Number(timeLeft) + timeFinal;
setTimeLeft();
}
function savedTimeLeftBoard (){
timeLeft = sessionStorage.getItem('timeLeft');
timeLeft = Number(timeLeft);
setTimeLeft();
}
function reloadTime (){
sessionStorage.setItem('timeLeft', 60);
}
//Game Timer
function gameTimer() {
let beginTimer = setInterval(function () {
let countdown = document.getElementById("countdown");
countdown.innerHTML = timeLeft;
timeLeft -= 1;
if(matchedCard.length == cards.length){ //stop the timer when you win
timeFieldPopup.innerText = timeLeft +" seconds.";
clearInterval(beginTimer);
setTimeLeft();
}
if (timeLeft <= -1 || score < 0) {
clearInterval(beginTimer);
}
if (timeLeft < 0) {
gameOverSound.play();
countdown.style.backgroundColor = "red";
countdown.style.color = "black";
let goT = document.getElementById("game-over-time");
goT.style.visibility = "visible";
gameOverPop.classList.add("show");
reloadScore(); // Reload score when you run out of time and clicked on Try Again button
reloadTime(); // Reload time when you run out of time and clicked on Try Again button
}
}, 1000);
}
// get time for scoreboard localSession
let scoreboardButton = document.querySelector("#score-board");
if(scoreboardButton){
scoreboardButton.addEventListener('click', savedTimeLeftBoard);
}
// //Initial flip so the player can view all the cards and memorize
function initialFlip() {
setTimeout(loadFlip, 800);
}
function loadFlip() {
cards.forEach(card => {
card.classList.add('flip');
card.classList.add('avoid-clicks');
});
}
// //Player has 8 seconds to view the cards,
// //below we're making all the cards face back again
function flipBack() {
setTimeout(loadAnotherFlip, 8000);
}
function loadAnotherFlip() {
cards.forEach(card => {
card.classList.remove('flip');
card.classList.remove('avoid-clicks');});}
////////////////////////////////////////////////////////////
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
this.classList.add('flip');
if (!hasFlippedCard) {
hasFlippedCard = true;
firstCard = this;
return;
}
hasFlippedCard = false;
secondCard = this;
checkForMatch();
}
function checkForMatch() {
let isMatch = firstCard.parentElement.dataset.framework === secondCard.parentElement.dataset.framework;
isMatch ? disableCards() : unflipCards();
}
function disableCards() {
if(window.location.href.includes('ad1-Netflix')){
netflixSound.play();
}
if(window.location.href.includes('ad2-Celebrities')){
celebritiesSound.play();
}
if(window.location.href.includes('ad3-Movies')){
moviesSound.play();
}
if(window.location.href.includes('ad4-Brands')){
brandsSound.play();
}
if(window.location.href.includes('ad5-All')){
allSound.play();
}
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
firstCard.parentElement.style.filter = "blur(5px)"; // adds filter effect to 1st
secondCard.parentElement.style.filter = "blur(5px)"; // and 2nd card matched
firstCard.classList.add('match');
secondCard.classList.add('match');
resetBoard();
}
function unflipCards() {
noMatchSound.play();
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
resetBoard();
}, 1500);
score -= 5;
if(score<=25){ // condition to show flip button
document.getElementById("see-cards").style.visibility = "visible";
}
if(score<2){ // condition to hide flip button
document.getElementById("see-cards").style.visibility = "hidden";
}
setScore();
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
(function shuffle() {
cards.forEach(card => {
let randomPos = Math.floor(Math.random() * cards.length);
card.parentElement.style.order = randomPos;
});
})();
function congratulations() {
if (matchedCard.length == cards.length) {
//show congratulations congratulationsPop
setTimeout(function(){
if(window.location.href.includes("ad5-All")){
finalVictorySound.play();
finalVictorySound.loop = true;
}
else{
victorySound.play();
}
congratulationsPop.classList.add("show");
}, 800);}
}
//shows the cards if flip button is clicked:
flipButton.addEventListener('click', function() {
flipButton.classList.add('avoid-clicks'); //disable flip button while cards are flipping
score = score-2; // pays for flip
gameOver(); // calls gameOver function to check if score goes below zero
setScore(); // calls set score since there are changes being made to the score
cards.forEach(card => {
if(!card.classList.contains("match")){ // condition to flip only unmatched cards
card.classList.add('flip');
card.classList.add('avoid-clicks')}});
setTimeout(function(){ //settimeout to flip cards back after 4 seconds
cards.forEach(card => {
if(!card.classList.contains("match")){
console.log(card.classList);
card.classList.remove('flip');
card.classList.remove('avoid-clicks')}})},4000);
if(score<2){ // condition to hide flip button
document.getElementById("see-cards").style.visibility = "hidden";
}
setTimeout(function(){ //enable flip button again after flip is over
flipButton.classList.remove('avoid-clicks');
},4000);
});
initialFlip();
flipBack();
function gameOver() {
if (score < 0) {
let goS = document.getElementById("game-over-score");
gameOverSound.play();
goS.style.visibility = "visible";
gameOverPop.classList.add("show");
sessionStorage.clear();
};
}
let tryAgainBtn = document.querySelector("#try-again");
tryAgainBtn.addEventListener('click', tryAgain);
function tryAgain(){
reloadScore();
reloadTime();
}
cards.forEach(card => {
card.addEventListener('click', flipCard);
card.addEventListener("click", congratulations);
card.addEventListener("click", gameOver);
});