-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
358 lines (313 loc) · 9.07 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
// game canvas
const c = document.getElementById('canvas');
const ctx = c.getContext('2d');
// remove pause button's event on spacebarPressed
window.addEventListener('keydown', (event) => event.keyCode === 32 ? event.preventDefault() : null);
// general params
const netWidth = 4;
const paddleWidth = 10;
const paddleHeight = 100;
const font = 'franchise';
let onButtonPressed = false;
let isGameover = false;
let isNewGame = false;
let winningScore = 7;
// keyboard params
let arrow = {
up: false,
down: false
}
let spacebarPressed = false;
// net
const net = {
x: (c.width - netWidth) / 2,
y: 0,
width: 4,
color: 'white'
}
// player object
const player = {
x: 10,
y: (c.height - paddleHeight) / 2,
speed: 8,
score: 0
}
// ai object
const ai = {
x: c.width - 20,
y: (c.height - paddleHeight) / 2,
score: 0
}
// ball
const ball = {
// the default location of the ball is in player's position
x: player.x + 20,
y: c.height / 2,
radius: 10,
speed: 7,
velocityX: 3,
velocityY: 3,
}
// gif
const gifLeft = {
x: 0,
y: 0
}
const gifRight = {
x: 0,
y: 0
}
// FUNCTIONS
// pause the game
pause = () => onButtonPressed = !onButtonPressed;
// background
drawBackground = () => {
// draw the background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, c.width, c.height)
}
// net
drawNet = () => {
for (let i = 0; i <= c.height; i += 15) {
ctx.fillStyle = net.color;
ctx.fillRect(net.x, i, net.width, 10)
}
}
// score
drawScore = (score, x, y) => {
ctx.font = `60px ${font}`;
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.fillText(score, x, y)
}
// paddles
drawPaddle = (x, y) => {
ctx.fillStyle = 'white';
ctx.fillRect(x, y, 10, 100)
}
// load images
const ballImg = new Image();
ballImg.src = './imgs/ricardo.jpg';
// ball
drawBall = (x, y, radius) => {
ctx.save();
ctx.fillStye = 'red';
ctx.beginPath();
ctx.arc(x, y, radius + 1, 0, Math.PI * 2)
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2)
ctx.closePath();
ctx.clip();
// draw ricardo
ctx.drawImage(ballImg, x - 30, y - 25, 60, 60);
ctx.restore();
}
// pause sign
drawPause = () => {
ctx.font = `10em ${font}`;
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.fillText('PAUSED', c.width / 2, c.height / 2);
}
// gameover
drawGameover = () => {
if (isGameover) {
// draw the background
ctx.fillStyle = 'rgb(88, 85, 85)';
ctx.fillRect(30, c.height / 4, c.width - 60, c.height - c.height / 3);
// draw the title
ctx.font = `6em ${font}`;
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
// to see who scores winningScore
winner = player.score === winningScore ? 'YOU WIN' : 'COMPUTER WINS';
ctx.fillText(winner, c.width / 2, c.height / 2);
ctx.font = `3em ${font}`;
ctx.fillText('CLICK THE BUTTON BELOW', c.width / 2, c.height / 2 + 100);
ctx.fillText('TO START A NEW GAME', c.width / 2, c.height / 2 + 150);
// make the old pause button into the restart button
button = document.getElementById('button');
button.setAttribute('onclick', 'restart()');
button.textContent = 'NEW GAME';
}
}
// check gameover
checkGameover = () => {
if (player.score === winningScore || ai.score === winningScore) {
isGameover = true;
}
}
// detect ball collisions
collision = (paddle, ball) => {
paddle.top = paddle.y;
paddle.bottom = paddle.y + paddleHeight;
paddle.right = paddle.x + paddleWidth;
paddle.left = paddle.x;
ball.top = ball.y - ball.radius;
ball.bottom = ball.y + ball.radius;
ball.right = ball.x + ball.radius;
ball.left = ball.x - ball.radius;
return ball.left < paddle.right && ball.top < paddle.bottom && ball.x > paddle.left && ball.bottom > paddle.top;
}
// move paddles
movePaddles = () => {
//move player
if (arrow.up && player.y >= 0) {
player.y -= player.speed;
} else if (arrow.down && player.y + paddleHeight <= c.height) {
player.y += player.speed;
}
// move ai
if (ball.x >= c.width / 4 && ai.y >= 0 && ai.y + paddleHeight <= c.height) {
ai.y += (ball.y - (ai.y + paddleHeight / 2)) * 0.13;
// when ai paadle hits the top or bottom
} else if (ai.y <= 0) {
ai.y += 1;
} else if (ai.y + paddleHeight >= c.height) {
ai.y += -1;
}
}
// pause or restart the game when player hits enter button
window.addEventListener('keydown', (event) => {
event.keyCode === 13 && !isGameover ? pause()
: event.keyCode === 13 && isGameover
? restart() : null;
});
const enter = document.getElementById('button');
enter.addEventListener('keydown', (event) => event.keyCode === 13 ? pause() : null);
render = () => {
// draw background
drawBackground();
// when the game is paused, show pause imgae
if (onButtonPressed) { drawPause() }
// net
drawNet();
// player's score
drawScore(player.score, c.width / 4, c.height / 5);
// ai's score
drawScore(ai.score, c.width * 3 / 4, c.height / 5);
// player's paddle
drawPaddle(player.x, player.y);
// ai's paddle
drawPaddle(ai.x, ai.y);
// ball
drawBall(ball.x, ball.y, ball.radius, ball.color);
// when the game is over, show gameover
if (isGameover) { return drawGameover() }
}
// update function, to update positions of everything
update = () => {
// handle all keybaord events
onKeyDownChange = (event) => {
switch (event.keyCode) {
case 38:
arrow.up = true;
break;
case 40:
arrow.down = true;
break;
case 32:
if (!onButtonPressed) {
spacebarPressed = true;
break;
}
}
}
onKeyUpChange = (event) => {
switch (event.keyCode) {
case 38:
arrow.up = false;
break;
case 40:
arrow.down = false;
break;
}
}
if (!spacebarPressed) {
// hit the ball when it's player's position
window.addEventListener('keydown', onKeyDownChange);
window.addEventListener('keyup', onKeyUpChange);
// the player holds the ball while able to move
movePaddles();
ball.x = player.x + 20;
ball.y = player.y + paddleHeight / 2;
// the ball will start off at straight line
ball.velocityX = 7;
ball.velocityY = 0;
} else {
// move paddles
movePaddles();
// check when the ball hits the walls
if (ball.y + ball.radius >= c.height || ball.y - ball.radius <= 0) {
ball.velocityY *= -1;
}
//when player wins
if (ball.x > c.width) {
player.score++;
reset();
}
// when ai wins
if (ball.x < 0) {
ai.score++;
reset();
}
// check is the game over
checkGameover();
// move the ball
ball.x += ball.velocityX;
ball.y += ball.velocityY;
// collision detection of paddles
let paddle = ball.x < c.width / 2 ? player : ai;
let angle = 0;
// if ball hits a paddle
if (collision(paddle, ball)) {
// default angle is 0
// when it hits the top of the paddle
if (ball.y < paddle.y + paddleHeight / 2) {
// -1 * Math.PI / 4 = -45deg
angle = -1 * Math.PI / 4;
} else if (ball.y > paddle.y + paddleHeight / 2) {
angle = Math.PI / 4;
} else { }
ball.velocityX = (paddle === player ? 1 : -1) * ball.speed * Math.cos(angle);
ball.velocityY = ball.speed * Math.sin(angle);
ball.speed += 1;
}
}
}
// reset the game when anyone scores
reset = () => {
ball.speed = 7;
// let the player hold the ball
spacebarPressed = false;
}
// restart a new game when the game is over
restart = () => {
// change the restart button back to pause button
isGameover = false;
button = document.getElementById('button');
button.setAttribute('onclick', 'pause()');
button.textContent = "PAUSE/RESUME";
// restore paddles abd scores
player.y = (c.height - paddleHeight) / 2;
player.score = 0;
ai.y = (c.height - paddleHeight) / 2;
ai.score = 0;
gameLoop();
}
gameLoop = () => {
!onButtonPressed
? (
render(),
update()
)
: (
render()
)
!isGameover ?
window.requestAnimationFrame(gameLoop)
: render();
}
gameLoop();