-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
404 lines (357 loc) · 12.4 KB
/
index.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
//HTML Elements of GameBox
var gameBox = document.getElementsByClassName('gameBox')[0];
var rods = document.getElementsByClassName('rod');
var ball = document.getElementsByClassName('ball')[0];
var pause = document.getElementsByClassName('pause')[0];
var message = document.getElementsByClassName('message')[0];
var tapSound = document.querySelector('.tapSound');
var outSound = document.querySelector('.outSound');
//HTML Elements of DetailsBox
var controlBtn = document.querySelectorAll('.detailsBox .controlBtn button');
var playerNameH1 = document.querySelector('.detailsBox .playerName h1');
var fullScoreboard = document.querySelector('.detailsBox .scoreboard .fullScoreboard');
var fullScoreboardBody = document.querySelector('.detailsBox .scoreboard .fullScoreboard tbody');
var eachGameScoreboard = document.querySelector('.detailsBox .scoreboard .eachGameScoreboard');
var scoreboardRound = document.querySelector('.detailsBox .scoreboard .eachGameScoreboard h2');
var eachGameScoreboardP = document.querySelector('.detailsBox .scoreboard .eachGameScoreboard p');
//Variables
var gameBoxWidth = gameBox.clientWidth;
var gameBoxHeight = gameBox.clientHeight;
var rodWidth = rods[0].clientWidth;
var rodHeight = rods[0].clientHeight;
var ballWidth = ball.clientWidth;
var ballHeight = ball.clientHeight;
var ballTop = 20;
var topOperation = "+";
var ballLeft = 100;
var leftOperation = "+";
var rodMovingSpeed = 20;
var ballMovingSpeed = 0.5;
var scoreSpeed = 5;
var gameState = "";
var gameID = null;
var roundID = null;
var messageIntervalID = null;
var out = false;
var scores = [];
var roundScore = 0;
var playerName = "";
//Setup new Game or Round
function setupNewGame() {
//Clear Previous Round ID & Message ID
clearTimeout(roundID);
roundID = null;
clearInterval(messageIntervalID);
messageIntervalID = null;
//Focus out from buttons & disable or change value of "New Game" button
controlBtn[0].blur();
controlBtn[1].blur();
controlBtn[0].disabled = true;
controlBtn[0].style.cursor = "not-allowed";
controlBtn[0].innerHTML = (scores.length == 4) ? "New Game" : "Next Round";
if (scores.length == 5) {
scores = [];
resetFullScoreboard();
}
if (scores.length == 0) {
var playerName = prompt("Please Enter Your Name - ");
if (playerName != "" && playerName != null) {
playerNameH1.innerHTML = playerName;
} else {
playerNameH1.innerHTML = 'Player 1';
}
}
//game Initial Value
rodMovingSpeed = 20;
ballMovingSpeed = 0.5;
gameState = "new";
gameID = null;
out = false;
roundScore = 0;
scoreSpeed = 5;
//rod Initial Value
var rodStartingPosition = Math.floor(Math.random() * (gameBoxWidth - rodWidth));
rods[0].style.left = rodStartingPosition + "px";
rods[1].style.left = rodStartingPosition + "px";
rods[0].style.transform = "none";
rods[1].style.transform = "none";
//ball Initial Value
var ballStartingPositionTop = gameBoxHeight - rodHeight - ballHeight;
var ballStartingPositionLeft = rodStartingPosition + (rodWidth / 2) - (ballWidth / 2);
ball.style.top = ballStartingPositionTop + "px";
ball.style.left = ballStartingPositionLeft + "px";
ball.style.transform = "none";
ballTop = ballStartingPositionTop;
topOperation = "-";
ballLeft = ballStartingPositionLeft;
leftOperation = "+";
ball.style.background = "var(--ballColor)";
//Initial DetailsBox & Message
eachGameScoreboardP.innerHTML = roundScore;
scoreboardRound.innerHTML = "Round " + (scores.length + 1);
message.innerHTML = "Press SPACE to Start Round " + (scores.length + 1) + ".";
displayFullScoreboard();
}
//Start Interval for Round
function startGame() {
gameID = setInterval(() => {
//Ball Next Position
ball.style.top = ballTop + "px";
ball.style.left = ballLeft + "px";
ballMovingSpeed = ballMovingSpeed + 0.0000625;
ballTop = eval(ballTop + topOperation + ballMovingSpeed);
ballLeft = eval(ballLeft + leftOperation + ballMovingSpeed);
if (ballTop >= (gameBoxHeight - rodHeight - ballHeight)) {
//If ball crosses or equal to bottom rod & Atleast some part of ball on rod then reverse Vertical direction else OUT.
var ballPos = (ballLeft + (3 * ballWidth / 4));
if (ballPos >= rods[0].offsetLeft && (ballLeft + (ballWidth / 4)) <= rods[0].offsetLeft + rodWidth) {
topOperation = "-";
scoreSpeed = getScoreSpeed(ballMovingSpeed);
roundScore += scoreSpeed;
eachGameScoreboardP.innerHTML = roundScore;
tapSound.play();
} else {
gameOver();
}
} else if (ballTop <= rodHeight) {
//If ball crosses or equal to top rod & Atleast some part of ball on rod then reverse Vertical direction else OUT.
var ballPos = (ballLeft + (3 * ballWidth / 4));
if (ballPos >= rods[0].offsetLeft && (ballLeft + (ballWidth / 4)) <= rods[0].offsetLeft + rodWidth) {
topOperation = "+";
scoreSpeed = getScoreSpeed(ballMovingSpeed);
roundScore += scoreSpeed;
eachGameScoreboardP.innerHTML = roundScore;
tapSound.play();
} else {
gameOver();
}
}
//Reverse Horizontal Directions
if (ballLeft >= (gameBoxWidth - ballWidth)) {
leftOperation = "-";
} else if (ballLeft <= 0) {
leftOperation = "+";
}
}, 5);
}
//Round Finished
function gameOver() {
//clear Interval ID of Finished Round
outSound.play();
clearInterval(gameID);
gameID = null;
out = true;
gameState = "over";
ballTop = eval(ballTop + topOperation + 5);
ball.style.top = ballTop + "px";
ball.style.left = ballLeft + "px";
ball.style.background = "#a90200";
//update Scores
scores[scores.length] = roundScore;
updateFullScoreboard();
enableButtons();
//If Last Round is finished then return else Setup Next Round.
if (scores.length == 5) {
displayFullScoreboard();
calculateTotalScore();
return;
}
let i = 9;
message.innerHTML = "Score : " + roundScore + ".</br>Next Round Starting in " + i + ".";
i = i - 1;
messageIntervalID = setInterval(() => {
if (i == 0) {
clearInterval(messageIntervalID);
messageIntervalID = null;
}
message.innerHTML = "Score : " + roundScore + ".</br>Next Round Starting in " + i + ".";
i = i - 1;
}, 1000)
roundID = setTimeout(() => {
setupNewGame();
}, 10000)
}
//Reset Game
function resetGame() {
disableButtons();
//clear All Intervals & Timeouts
clearInterval(gameID);
clearTimeout(roundID);
clearInterval(messageIntervalID);
gameID = null;
roundID = null;
messageIntervalID = null;
//Update Varibles to Initial Values
controlBtn[0].blur();
controlBtn[1].blur();
controlBtn[0].innerHTML = "New Game";
scores = [];
resetFullScoreboard();
gameState = "";
out = false;
roundScore = 0;
scoreSpeed = 5;
//Update HTML Elements to Initial Values
message.innerHTML = "Click on New Game to Start.";
pause.style.display = "none";
rods[0].style.left = "50%";
rods[1].style.left = "50%";
rods[0].style.transform = "translate(-50%)";
rods[1].style.transform = "translate(-50%)";
ball.style.top = "50%";
ball.style.left = "50%";
ball.style.transform = "translate(-50%, -50%)";
ball.style.background = "var(--ballColor)";
eachGameScoreboardP.innerHTML = "";
scoreboardRound.innerHTML = "";
playerNameH1.innerHTML = "";
enableButtons();
}
//Disable both buttons
function disableButtons() {
controlBtn[0].disabled = true;
controlBtn[1].disabled = true;
controlBtn[0].style.cursor = "not-allowed";
controlBtn[1].style.cursor = "not-allowed";
}
//Enable both buttons
function enableButtons() {
controlBtn[0].disabled = false;
controlBtn[1].disabled = false;
controlBtn[0].style.cursor = "pointer";
controlBtn[1].style.cursor = "pointer";
}
//Show Full Scoreboard & Hide Round Score
function displayFullScoreboard() {
fullScoreboard.style.display = "flex";
eachGameScoreboard.style.display = "none";
}
//Hide Full Scoreboard & Show Round Score
function hideFullScoreboard() {
fullScoreboard.style.display = "none";
eachGameScoreboard.style.display = "flex";
}
//Update Full Scoreboard
function updateFullScoreboard() {
//create textNodes
var text1 = document.createTextNode("Round " + scores.length);
var text2 = document.createTextNode(scores[scores.length - 1]);
//create tds & append text Nodes
var td1 = document.createElement("td");
var td2 = document.createElement("td");
td1.appendChild(text1);
td2.appendChild(text2);
//create tr & appends tds
var tr = document.createElement("tr");
tr.appendChild(td1);
tr.appendChild(td2);
//append Row to full Scoreboard
fullScoreboardBody.appendChild(tr);
}
//Reset Full Scoreboard
function resetFullScoreboard() {
//Remove All Rows from Full Scoreboard
var fullScoreboardRows = document.querySelectorAll('.detailsBox .scoreboard .fullScoreboard tbody tr');
for (var i = 0; i < fullScoreboardRows.length; i++) {
fullScoreboardRows[i].remove();
}
}
//Calculate Total Score After last rounds
function calculateTotalScore() {
var sum = 0;
for (var i = 0; i < scores.length; i++) {
sum += scores[i];
}
message.innerHTML = "Total Score : " + sum + ".</br>Play again to beat this score.";
//create textNodes
var text1 = document.createTextNode("Total");
var text2 = document.createTextNode(sum);
//create tds & append text Nodes
var td1 = document.createElement("td");
var td2 = document.createElement("td");
td1.appendChild(text1);
td2.appendChild(text2);
//create tr & appends tds
var tr = document.createElement("tr");
tr.appendChild(td1);
tr.appendChild(td2);
//append Row to full Scoreboard
fullScoreboardBody.appendChild(tr);
}
//Get Score Update Speed based on ball moving speed
function getScoreSpeed(ballMovingSpeed) {
if (ballMovingSpeed < 1.0) {
return 5;
} else if (ballMovingSpeed >= 1.0 && ballMovingSpeed < 1.5) {
rodMovingSpeed = 22;
return 7;
} else {
rodMovingSpeed = 25;
return 9;
}
}
//Move Left on Key A
function moveLeft() {
var leftDistance = rods[0].offsetLeft;
if (leftDistance == 0) {
return;
}
var value;
if ((leftDistance - rodMovingSpeed) <= 0) {
value = 0;
} else {
value = leftDistance - rodMovingSpeed;
}
rods[0].style.left = value + "px";
rods[1].style.left = value + "px";
}
//Move Right on Key D
function moveRight() {
var leftDistance = rods[0].offsetLeft;
if (leftDistance + rodWidth == gameBoxWidth) {
return;
}
var value;
if ((leftDistance + rodWidth + rodMovingSpeed) >= gameBoxWidth) {
value = gameBoxWidth - rodWidth;
} else {
value = leftDistance + rodMovingSpeed;
}
rods[0].style.left = value + "px";
rods[1].style.left = value + "px";
}
//Space, A & D keyPress Event Handlers
document.addEventListener('keydown', function(e) {
//Handle Space Press only when New Game is Started
if (e.code == "Space" && !out && gameState != "") {
if (gameID != null) {
hideFullScoreboard();
clearInterval(gameID);
gameID = null;
gameState = "pause";
message.innerHTML = "Press SPACE to continue."
pause.style.display = "block";
controlBtn[1].disabled = false;
controlBtn[1].style.cursor = "pointer";
} else {
hideFullScoreboard();
gameState = "running";
message.innerHTML = "";
pause.style.display = "none";
controlBtn[1].disabled = true;
controlBtn[1].style.cursor = "not-allowed";
startGame();
}
}
//Handle A & D only when game is in Running State
if (gameState == "running") {
if (e.code == "KeyA" || e.keyCode == "37") {
moveLeft();
return;
}
if (e.code == "KeyD" || e.keyCode == "39") {
moveRight();
return;
}
}
})