-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
492 lines (332 loc) · 13.7 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'use strict';
//game components
const playButton = document.querySelector('.play-btn');
const audioDestroy = document.querySelector('.destroy');
const audioLose = document.querySelector('.lose');
const audioWin = document.querySelector('.win');
const audioBackground = document.querySelector('.background');
const displayInfo = document.querySelector('.score');
const responseTime = document.querySelector('.response-time');
const scoreTable = document.querySelector('.score-bar');
const outputMessage = document.querySelector('.output');
const body = document.querySelector('body');
const titleHeader = document.querySelector('.header-title');
const welcomeMessage = document.querySelector('.welcome');
const hamburger = document.querySelector('.hamburger');
const navBar = document.querySelector('.nav-bar');
const hamburgerlines = document.querySelectorAll('.line');
const rankBar = document.querySelector('.ranks');
const lifeProgress = document.querySelector('.progress');
const listElem = document.querySelector('.scoreBoard');
document.querySelector('footer').textContent = `KennyCodes || ${new Date().getFullYear()}`;
let gamePlay = false;
let gameFinished = true;
let mRoll;
//get random color
function getColor(){
const colors = ['red', 'green', 'blue', 'orange', 'yellow', 'grey', 'steelblue', 'cyan', 'black', 'coral', 'purple', 'lime', 'steelblue', 'gold'];
const colorIndex = Math.floor(Math.random()*colors.length);
return colors[colorIndex];
}
//get random positions (x, y, + or -)
function getPos(){
let xfactor = (window.innerWidth/2)-70;
let yfactor = (window.innerHeight/2)-100;
const x = Math.floor(Math.random()*xfactor);
const y = Math.floor(Math.random()*yfactor);
const yaddMinus = ['+', '-'].sort((a, b)=> 0.5 - Math.random());
const xaddMinus = ['+', '-'].sort((a, b)=> 0.4 - Math.random());
const pos = {x, y, xsign: xaddMinus[0], ysign: yaddMinus[0]};
return pos;
}
//game initial variables
let score = 0;
let attempts = 0;
const lifeColors = ['green', 'orange', 'darkorange', 'red', 'transparent'];
let playerRTime = [];
let x;
//play game function
function play(){
gameFinished = false;
// clearInterval(mRoll)
if(gamePlay){
scoreTable.classList.add('show');
hamburger.classList.add('hide');
audioBackground.play();
audioBackground.volume = 0.05;
let attackSucceed = false;
playButton.textContent = 'Playing...';
// create the object to display
const circleObj = document.createElement('div');
circleObj.classList.toggle('circle');
circleObj.style.background = `${getColor()}`;
const squareObj = document.createElement('div');
squareObj.classList.toggle('square');
squareObj.style.background = `${getColor()}`;
const rectangleVObj = document.createElement('div');
rectangleVObj.classList.toggle('rectangleVertical');
rectangleVObj.style.background =`${getColor()}`;
const rectangleHObj = document.createElement('div');
rectangleHObj.classList.toggle('rectangleHorizontal');
rectangleHObj.style.background =`${getColor()}`;
const triangleUp = document.createElement('div');
triangleUp.classList.toggle('triangleUp');
triangleUp.style.borderBottomColor = `${getColor()}`;
const triangleDown = document.createElement('div');
triangleDown.classList.toggle('triangleDown');
triangleDown.style.borderTopColor = `${getColor()}`;
const objects = [circleObj, squareObj, triangleDown, rectangleVObj, triangleUp, rectangleHObj];
const obj = objects[(Math.floor(Math.random()*objects.length))];
obj.style.transform = `translate(${getPos().xsign}${getPos().x}px, ${getPos().ysign}${getPos().y}px)`;
//Add object to the screen
audioBackground.play();
audioBackground.volume = 0.1;
let time = new Date().getTime();
body.append(obj);
const colors = ['lavender', 'lightcyan', 'lightpurple', 'lightpink', 'lightcoral', 'lightsalmon'];
body.style.background = `${colors.sort((a, b)=> 0.5 - Math.random())[0]}`;
let elapsed;
const runTime = function(){
elapsed = ((new Date().getTime()) - time)/10000;
};
let y = setInterval(runTime, 100);
const objectMissed = function(){
if(!attackSucceed){
attempts += 1;
audioLose.play();
obj.remove();
//update Life
lifeProgress.style.width = `${((5 - attempts)/5)*100}%`;
lifeProgress.style.background = lifeColors[attempts - 1];
if(attempts == 5){
audioLose.play();
scoreTable.classList.toggle('show');
audioBackground.pause();
audioBackground.currentTime = 0;
audioLose.volume = 0.2;
if(score >= 3){
outputMessage.textContent = `{Score: ${score} } Nice Try...`;
}else{
outputMessage.textContent = `{Score: ${score} } Ooops...`;
}
outputMessage.classList.add('show');
setTimeout(()=>{
outputMessage.classList.remove('show');
}, 3000);
setTimeout(()=>{
playButton.classList.toggle('hide');
}, 3500);
hamburger.classList.toggle('hide');
playButton.textContent = 'RESTART';
body.style.background = 'rgb(219, 210, 193)';
gamePlay = false;
clearInterval(x);
gameFinished = true;
addScore();
titleHeader.classList.toggle('hide');
}
}
};
setTimeout(objectMissed, 1100);
//when the object is clicked
obj.onclick = ()=>{
obj.remove();
//time limit to earn a point
if(elapsed < 0.21){
audioDestroy.play();
audioDestroy.volume = 0.2;
attackSucceed = true;
score +=1;
playerRTime.push(elapsed);
}
displayInfo.textContent = ` Score : ${score}`;
responseTime.textContent = `Time :${(elapsed*10).toFixed(2)} `;
if(score == 100){
//stop the game loop
clearInterval(x);
gameFinished = true;
scoreTable.classList.toggle('show');
audioBackground.pause();
audioBackground.currentTime = 0;
audioWin.play();
audioWin.volume = 0.15;
const messages = ['Good Job!', 'Bravo!', 'Awesome...', 'Hero', 'Boss Mode!', 'Master Badge', 'Wonderful!', 'Amazing'];
const messageIndex = Math.floor(Math.random()*(messages.length));
playButton.textContent = 'RESTART';
outputMessage.textContent = `${messages[messageIndex]}`;
setTimeout(()=>{
outputMessage.classList.add('show');
},4000);
addScore();
titleHeader.classList.toggle('hide');
}
};
}
}
// game start control
playButton.onclick = ()=>{
if(gameFinished){
playButton.classList.toggle('hide');
hamburger.classList.remove('hide');
gamePlay = true;
// rollingDiv.classList.add('show')
score = 0;
attempts = 0;
gameFinished = false;
lifeProgress.style.background = 'green';
lifeProgress.style.width = '100%';
displayInfo.textContent = 'Score : 0';
outputMessage.classList.remove('show');
scoreTable.classList.remove('show');
titleHeader.classList.add('hide');
welcomeMessage.classList.add('hide');
playButton.textContent = 'Starting...';
const gettingready = function(){
const rTime = (Math.random()*2 + 1);
x = setInterval(play, rTime*1000);
};
gettingready();
}
};
// handle the user name input
const userInput = document.createElement('input');
const saveBtn = document.createElement('button');
const inputContainer = document.createElement('div');
const scorePlate = document.createElement('p');
inputContainer.classList.add('inputContainer');
userInput.classList.add('userInput'); // css sttyling
saveBtn.classList.add('saveBtn');
scorePlate.classList.add('scorePlate');
saveBtn.textContent = 'Save';
userInput.setAttribute('placeholder', 'Please Enter Your Name...');
userInput.autofocus = true;
// function to render the form to the screen
function addInterface(){
userInput.value = '';
scorePlate.textContent = `Score : ${score}`;
inputContainer.append(userInput);
inputContainer.append(saveBtn);
inputContainer.append(scorePlate);
body.append(inputContainer);
}
//check for QuickData in LocalStorage & Create one if none is found
if(localStorage.getItem('QuickData') == null){
localStorage.setItem('QuickData', JSON.stringify([]));
}
//add score to localstorage
function addScore(){
//get data
let storedScores = JSON.parse(localStorage.getItem('QuickData'));
if(score >= 3 ){
const aveSpeed = (playerRTime.reduce((acc, v)=> acc + v ,0)/playerRTime.length);
const date = new Date().toDateString();
if(storedScores.length < 5){
//alert to enter name
setTimeout(addInterface, 4000);
saveBtn.onclick = ()=>{
let userName = userInput.value;
if(userName.trim().length > 0){
inputContainer.remove();
const user = {name: userName, score: score, aveTime: aveSpeed, date: date};
storedScores = JSON.stringify([...storedScores, user]);
localStorage.setItem('QuickData', storedScores);
if(storedScores.length > 1){
let secondFetch = JSON.parse(localStorage.getItem('QuickData'));
secondFetch.sort((a, b)=>{
if(a.score === b.score){
return a.aveTime - b.aveTime;
}else{
return b.score - a.score;
}
});
localStorage.setItem('QuickData', JSON.stringify(secondFetch));
}
}
};
}else{
let found = 'no';
for(let i = 0; i < storedScores.length; i++){
//find the first user with lower score
if((score > storedScores[i].score) || ((score == storedScores[i].score) && (aveSpeed < storedScores[i].aveTime))){
found = i;
break; //find only one and jump out
}
}
if(found !== 'no'){
//alert to enter name;
addInterface();
saveBtn.onclick = ()=>{
let userName = userInput.value;
if(userName.trim().length > 0){
inputContainer.remove();
let user = {name: userName, score: score, aveTime: aveSpeed, date: date};
//no removal done here
storedScores.splice(found, 0, user);
//remove the last person currently the lowest
storedScores.pop();
localStorage.setItem('QuickData', JSON.stringify(storedScores));
}
};
}
}
}
}
//Nav-Bar Control
hamburger.onclick = ()=>{
navBar.classList.toggle('active');
playButton.classList.toggle('hide');
if(rankBar.textContent == 'Close'){
rankBar.textContent = 'Ranks';
listElem.classList.toggle('hide');
}
hamburgerlines.forEach(e=>{
e.classList.toggle('active');
});
titleHeader.classList.toggle('hide');
welcomeMessage.classList.toggle('hide');
};
//fetch the high scores and display them
function getRanking(){
listElem.innerHTML = '';
const rankRecordArray = JSON.parse(localStorage.getItem('QuickData'));
rankRecordArray.map((obj, i)=>{
const listItem = document.createElement('li');
const nameElem = document.createElement('p');
const scoreElem = document.createElement('p');
const dateElem = document.createElement('p');
const divElem = document.createElement('div');
const spanElem = document.createElement('span');
nameElem.textContent = `Name: ${obj.name.toUpperCase()}`;
scoreElem.textContent = `Score: ${obj.score}`;
dateElem.textContent = `Date: ${obj.date}`;
spanElem.textContent = `${i+1}.`;
listItem.append(nameElem);
listItem.append(scoreElem);
listItem.append(dateElem);
divElem.append(spanElem);
divElem.append(listItem);
listElem.append(divElem);
divElem.classList.add('divElem');
});
if(rankBar.textContent == 'Close'){
listElem.classList.remove('hide');
}else {
listElem.classList.add('hide');
navBar.classList.toggle('active');
playButton.classList.remove('hide');
titleHeader.classList.remove('hide');
}
body.append(listElem);
}
let hideBtn = document.querySelector('.hideScore');
//handle the click event
let rankBarInfo = rankBar.textContent;
rankBar.onclick = ()=>{
rankBar.classList.toggle('hide');
if(rankBar.classList.contains('hide')){
rankBar.textContent = 'Close';
}else{
rankBar.textContent = rankBarInfo;
}
getRanking();
};