-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
318 lines (287 loc) · 8.03 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
//creating the components
const gameBoard = document.querySelector("#gameBoard");
const ctx = gameBoard.getContext("2d");
const scoreText = document.querySelector("#scoreText");
const resetBtn = document.querySelector("#resetBtn");
const gameWidth = gameBoard.width;
const gameHeight = gameBoard.height;
const boardBackground = "rgb(81, 88, 178)";
const paddle1Color = "rgb(19, 19, 19)";
const paddle2Color = "rgb(231, 4, 10)";
const paddleBorder = "black";
const ballColor = "orange";
const ballBorderColor = "black";
const ballRadius = 10;
const paddleSpeed = 70;
let intervalID;
let ballSpeed;
let ballX = gameWidth / 2;
let ballY = gameHeight / 2;
let ballXDirection = 0;
let ballYDirection = 0;
let player1Score = 0;
let player2Score = 0;
let running = false;
let paddle1 = {
width: 25,
height: 100,
x: 0,
y: 0,
};
let paddle2 = {
width: 25,
height: 100,
x: gameWidth - 25,
y: gameHeight - 100,
};
//adding the event listeners
window.addEventListener("keydown", changeDirection);
window.addEventListener("keydown", checkForStartGame);
resetBtn.addEventListener("click", resetGame);
//to prevent the scroll function of window by arrow key
window.addEventListener(
"keydown",
function (e) {
if (
[
"Space",
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
].indexOf(e.key) > -1
) {
e.preventDefault();
}
},
false
);
//game
showStartInstructions();
//a function to show instructions on the screen
function showStartInstructions() {
ctx.font = "30px Permanent Marker";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.fillText(
"Press Enter to Start The Game",
gameWidth / 2,
gameHeight / 2
);
}
//function to check if to start the game
function checkForStartGame(event) {
if (event.keyCode == 13) {
resetGame();
}
}
//a function to start the game
function gameStart() {
running = true;
resetBtn.style.visibility = "hidden";
createBall();
nextTick();
}
//this function calls the arrow function to call all mentioned functions
//in it every 1ms
function nextTick() {
if (running) {
intervalID = setTimeout(() => {
clearBoard();
drawPaddles();
moveBall();
drawBall();
checkForWinner();
checkCollision();
nextTick();
}, 1);
}
}
//this function clears the board every next tick
function clearBoard() {
//board background
ctx.fillStyle = boardBackground;
ctx.fillRect(0, 0, gameWidth, gameHeight);
ctx.strokeStyle = "white";
//centre vertical dividing line
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(gameWidth / 2, 0);
ctx.lineTo(gameWidth / 2, gameHeight);
ctx.stroke();
//center horizontal dividing line
ctx.lineWidth = 4;
ctx.moveTo(gameWidth / 2, gameHeight / 2);
ctx.lineTo(gameWidth, gameHeight / 2);
ctx.lineTo(0, gameHeight / 2);
ctx.stroke();
}
//a function to draw the player paddles
function drawPaddles() {
ctx.strokeStyle = paddleBorder;
ctx.lineWidth = 2;
ctx.fillStyle = paddle1Color;
ctx.fillRect(paddle1.x, paddle1.y, paddle1.width, paddle1.height);
ctx.strokeRect(paddle1.x, paddle1.y, paddle1.width, paddle1.height);
ctx.fillStyle = paddle2Color;
ctx.fillRect(paddle2.x, paddle2.y, paddle2.width, paddle2.height);
ctx.strokeRect(paddle2.x, paddle2.y, paddle2.width, paddle2.height);
}
//a function to create the ball
function createBall() {
ballSpeed = 1;
//we will take random value between 0 and 1 to decide if the ball will go
//towards player 1 or player 2 and either upwards or downwards
if (Math.round(Math.random()) == 1) {
ballXDirection = 1;
} else {
ballXDirection = -1;
}
if (Math.round(Math.random()) == 1) {
ballYDirection = 1;
} else {
ballYDirection = -1;
}
//place the ball in the center of the table
ballX = gameWidth / 2;
ballY = gameHeight / 2;
drawBall(ballX, ballY);
}
//a function to move the ball
function moveBall() {
ballX += ballSpeed * ballXDirection;
ballY += ballSpeed * ballYDirection;
}
//a function to draw the ball on the table
function drawBall() {
ctx.fillStyle = ballColor;
ctx.strokeStyle = ballBorderColor;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
}
//a function to check if the ball colides
function checkCollision() {
//to check upper and lower boundries of the table
if (ballY <= 0 + ballRadius) {
ballYDirection *= -1;
} else if (ballY >= gameHeight - ballRadius) {
ballYDirection *= -1;
}
//if ball goes out of table
if (ballX <= 0) {
player2Score++;
updateScore();
createBall();
return;
}
if (ballX >= gameWidth) {
player1Score += 1;
updateScore();
createBall();
return;
}
//if ball collides with one of the paddles
if (ballX <= paddle1.x + paddle1.width + ballRadius) {
if (ballY >= paddle1.y && ballY <= paddle1.y + paddle1.height) {
ballX = paddle1.x + paddle1.width + ballRadius; //if ball gets stuck
ballXDirection *= -1;
ballSpeed += 0.2;
}
}
if (ballX >= paddle2.x - ballRadius) {
if (ballY >= paddle2.y && ballY <= paddle2.y + paddle2.height) {
ballX = paddle2.x - ballRadius; // if ball gets stuck
ballXDirection *= -1;
ballSpeed += 0.2;
}
}
}
//a function to move the paddles on table
function changeDirection(event) {
const KeyPressed = event.keyCode;
const paddle1Up = 87;
const paddle1Down = 83;
const paddle2Up = 38;
const paddle2Down = 40;
switch (KeyPressed) {
case paddle1Up:
if (paddle1.y > 0) {
paddle1.y -= paddleSpeed;
}
break;
case paddle1Down:
if (paddle1.y < gameHeight - paddle1.height) {
paddle1.y += paddleSpeed;
}
break;
case paddle2Up:
if (paddle2.y > 0) {
paddle2.y -= paddleSpeed;
}
break;
case paddle2Down:
if (paddle2.y < gameHeight - paddle2.height) {
paddle2.y += paddleSpeed;
}
break;
}
}
//a function to update the score on screen
function updateScore() {
scoreText.textContent = `${player1Score} : ${player2Score}`;
}
//a function to check for the winner
function checkForWinner() {
//in table tennis game point is at 10 but a player can only
//win the game if he/she is atleast two points ahead of other player
if (player1Score > 10 && player1Score >= player2Score + 2) {
showWinner("Player 1");
} else if (player2Score > 10 && player2Score >= player1Score + 2) {
showWinner("Player 2");
}
}
//a function to show the winner on screen and stop the game
function showWinner(winner) {
ctx.font = "70px Georgia";
ctx.textAlign = "center";
ctx.lineWidth = 6;
if (winner == "Player 1") {
ctx.fillStyle = paddle1Color;
ctx.strokeStyle = "red";
} else {
ctx.fillStyle = paddle2Color;
ctx.strokeStyle = "black";
}
ctx.strokeText(`${winner} Won!`, gameWidth / 2 + 12, gameHeight / 2 - 30);
ctx.fillText(`${winner} Won!`, gameWidth / 2 + 12, gameHeight / 2 - 30);
running = false;
resetBtn.style.visibility = "visible";
}
//a function to reset the game and start over
function resetGame() {
player1Score = 0;
player2Score = 0;
paddle1 = {
width: 25,
height: 100,
x: 0,
y: 0,
};
paddle2 = {
width: 25,
height: 100,
x: gameWidth - 25,
y: gameHeight - 100,
};
ballSpeed = 1;
ballX = 0;
ballY = 0;
ballXDirection = 0;
ballYDirection = 0;
updateScore();
clearInterval(intervalID);
gameStart();
}