-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpong-game.js
425 lines (385 loc) · 12.4 KB
/
pong-game.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
let ctx = null;
let canvas = null;
let game = null;
let hud = null;
let menu = null;
let ball = null;
let player = null;
let player2 = null;
//let particles = []
// Main Functions ********************************************
function main() {
//Canvas
canvas = document.querySelector("#ballCanvas");
ctx = canvas.getContext("2d");
ctx.font = "25px Helvetica";
// Make the objects
game = new Game();
hud = new Hud();
menu = new Menu();
ball = new Sphere(canvas.width / 2, canvas.height / 2); //x,y
player = new Paddle(20, canvas.height / 2, 15, 80); //x,y,w,h
player2 = new Paddle(canvas.width - 35, canvas.height / 2, 15, 80); //x,y,w,h
//Events
menuEvents(menu);
ballState(ball)
// Update and Draw
_update();
_draw();
}
function _update() {
requestAnimationFrame(() => _update());
//ctx.clearRect(0, 0, canvas.width, canvas.height);
game.update();
// Game States
if (game.state == "menu") {
menu.update();
} else if (game.state == "game") {
ball.update(canvas.width, canvas.height);
player.update(canvas.width, canvas.height);
player2.update(canvas.width, canvas.height);
hud.update()
//if (game.changeHud) hud.update()
}
}
function _draw() {
requestAnimationFrame(() => _draw());
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Game States
if (game.state == "menu") {
menu.draw();
} else if (game.state == "game") {
ball.draw();
player.draw();
player2.draw();
hud.draw();
}
}
// Objects ********************************************
// Game manager singleton
class Game {
constructor() {
if (!!Game.instance) return Game.instance;
Game.instance = this;
this.state = "menu";
this.inMatch = false;
this.p1Score = 0;
this.p2Score = 0;
this.inGamePlayers = 0; //How much payers in the game (0 means 2 bots)
this.autoStartTime = 3; //If only bots in the game
this.autoStartTimeActual = new Date(); //If only bots in the game
this.mousePos = {
x: 0,
y: 0,
w: 0,
h: 0
} // w & h to make it compatible with overlap function(helpers.js)
this.audioManager = document.createElement("audio");
return this;
}
update(){
if(this.state=="game" && !this.inMatch && this.inGamePlayers <= 0){
// Auto start match counter (only if all players are bots)
if ((new Date() - this.autoStartTimeActual) / 1000 > this.autoStartTime) {
this.startMatch()
}
}
}
setMatch() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.stiky = true;
player.y = (canvas.height / 2) - (player.h / 2)
player.dy = 0
player2.y = (canvas.height / 2) - (player.h / 2)
player2.dy = 0
this.inMatch = false;
this.autoStartTimeActual = new Date()
}
startGame(gameType) {
switch (gameType) {
case 0:
movePlayer(player);
this.inGamePlayers = 1;
break;
case 1:
movePlayer(player);
movePlayer(player2);
this.inGamePlayers = 2;
break;
case 2:
this.autoStartTimeActual = new Date();
break;
default: //Error default case 1 player
movePlayer(player);
this.inGamePlayers = 1;
break;
}
this.state = "game";
}
startMatch() {
ball.stiky = false;
this.inMatch = true;
ball.dx = randomRange(0, 1) == 0 ? ball.speed : -ball.speed;
ball.dy = randomRange(0, 1) == 0 ? ball.speed : -ball.speed;
//Audio sound
game.audioManager.src = "audio/startMatch.wav";
game.audioManager.volume = 1;
game.audioManager.play();
}
gameOver() {
this.state = "gameOver";
}
}
class Sphere {
constructor(x, y) {
this.color = 'salmon';
this.x = x;
this.y = y;
this.dx = -3;
this.dy = 3;
this.radius = 10;
this.stiky = true; //ball can't move
this.speed = 3.5;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
update(xLimit, yLimit) {
//Canvas bounce
/*if ( this.x < this.radius || this.x + this.radius > xLimit) this.dx = -this.dx;*/
if (this.y + this.radius > yLimit || this.y < this.radius) {
this.dy = -this.dy;
game.audioManager.src = "audio/hit.wav";
game.audioManager.volume = 0.2;
game.audioManager.play();
}
// Paddle colission
if (onCollideBall(this, player) || onCollideBall(this, player2)) {
game.audioManager.src = "audio/hitpaddle.wav";
game.audioManager.volume = 0.2;
game.audioManager.play();
if (onCollideBall(this, player)) {
//Colide with X paddle position
if (!deflectY(this, player)) {
this.dx = -this.dx;
} //Colide with X paddle position
else {
this.dy = -this.dy
}
} else {
//Colide with X paddle1 position
if (!deflectY(this, player2)) {
this.dx = -this.dx;
} //Colide with X paddle position
else {
this.dy = -this.dy
}
}
}
//Game points
if (this.x + this.radius < 0) {
game.p2Score++;
game.audioManager.src = "audio/point.wav";
game.audioManager.volume = 0.3;
game.audioManager.play();
game.setMatch();
}
if (this.x - this.radius > xLimit) {
game.p1Score++;
game.audioManager.src = "audio/point.wav";
game.audioManager.volume = 0.3;
game.audioManager.play();
game.setMatch();
}
// Stick the ball
if (this.stiky) {
this.dx = 0;
this.dy = 0;
}
// Update ball position
this.x += this.dx;
this.y += this.dy;
}
}
class Paddle {
constructor(x, y, w, h) {
this.x = x;
this.y = y - (h / 2);
this.w = w;
this.h = h;
this.dx = 0;
this.dy = 0;
this.speed = 3;
this.color = "white";
this.bot = true
this.botMultiplierSpeed = 0.91 // 1 equal player speed / 1.5 is 150% player speed
}
update(xLimit, yLimit) {
//Update Paddle position
this.x += this.dx;
this.y += this.dy;
this.y = clamp(this.y, 0, yLimit - this.h)
if (this.bot) this.botMovement();
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.fillRect(this.x, this.y, this.w, this.h);
ctx.closePath();
}
// NPC Logic
botMovement() {
// If the ball is in his half then can move
if (Math.abs((this.x + (this.w / 2)) - ball.x) < canvas.height - 35) {
//Detect the y psosition of the ball
if (ball.y < this.y + (this.h / 2)) {
this.dy = -this.speed * (this.botMultiplierSpeed);
} else if (ball.y > this.y + (this.h / 2)) {
this.dy = this.speed * (this.botMultiplierSpeed);
} else {
this.dy = 0
}
} else {
this.dy = 0
}
}
}
class Hud {
constructor() {
this.p1 = game.p1Score;
this.p2 = game.p2Score;
this.instructions = true;
this.instructionsColor = "#898989";
this.blinkCounter = new Date(); //Time in seconds
}
update() {
this.p1 = game.p1Score;
this.p2 = game.p2Score;
this.instructions = game.inMatch ? false : true;
//player can see instructions
if (this.instructions) {
// Blink counter
if ((new Date() - this.blinkCounter) / 1000 > 0.5) {
if (this.instructionsColor == "#898989") {
this.instructionsColor = "white"
} else {
this.instructionsColor = "#898989"
}
this.blinkCounter = new Date()
}
}
}
draw() {
ctx.font = "25px Helvetica";
ctx.fillText(this.p1, (canvas.width / 2) - 80, 30);
ctx.fillText(this.p2, (canvas.width / 2) + 80, 30);
//player can see instructions
if (this.instructions) {
if(game.inGamePlayers <= 0){
let lt = 3-(Math.round((new Date() - game.autoStartTimeActual) / 1000))
ctx.fillStyle = this.instructionsColor;
ctx.font = "16px Helvetica";
ctx.fillText("Match start in " + lt, canvas.width / 2, canvas.height-90);
}
// One player in game
if(game.inGamePlayers > 0){
ctx.fillStyle = this.instructionsColor;
ctx.font = "16px Helvetica";
ctx.fillText("Space key to start", canvas.width / 2, canvas.height-90);
ctx.fillText("W - Up", 50, canvas.height-90);
ctx.fillText("S - Down", 50, canvas.height-65);
}
// Two player in game
if(game.inGamePlayers > 1){
ctx.fillStyle = this.instructionsColor;
ctx.font = "16px Helvetica";
ctx.fillText("↑ - Up", canvas.width-50, canvas.height-90);
ctx.fillText("↓ - Down", canvas.width-50, canvas.height-65);
}
}
}
}
class Menu {
constructor() {
this.mainText = "PONG"
this.activeButton = null;
this.buttonsPos = {
x: (canvas.width / 2) - 85,
y: (canvas.height/2) -50
}
this.margin = {
x: 0,
y: 60
}
this.buttons = [{
id: 0,
text: "Player vs NPC",
x: this.buttonsPos.x,
y: this.buttonsPos.y,
w: ctx.measureText("Player vs NPC").width,
h: 30,
color: "#e0e0e0"
},
{
id: 1,
text: "Player vs Player",
x: this.buttonsPos.x + (this.margin.x * 1),
y: this.buttonsPos.y + (this.margin.y * 1),
w: ctx.measureText("Player vs Player").width,
h: 30,
color: "#e0e0e0"
},
{
id: 2,
text: "NPC vs NPC",
x: this.buttonsPos.x + (this.margin.x * 2),
y: this.buttonsPos.y + (this.margin.y * 2),
w: ctx.measureText("Player vs Player").width,
h: 30,
color: "#e0e0e0"
}
]
}
update() {
let a = overlap(game.mousePos, this.buttons[0])
let b = overlap(game.mousePos, this.buttons[1])
let c = overlap(game.mousePos, this.buttons[2])
if (a || b || c) {
if (a) this.activeButton = 0;
else if (b) this.activeButton = 1;
else if (c) this.activeButton = 2;
document.documentElement.style.cursor = "pointer";
} else {
document.documentElement.style.cursor = "default";
this.activeButton = null;
}
this.buttons.forEach(btn => {
if(btn.id==this.activeButton) btn.color= "white"
else btn.color= "#e0e0e0"
});
}
draw() {
ctx.textAlign = "center";
ctx.fillStyle = "#fff";
ctx.font = "70px Helvetica";
ctx.fillText(this.mainText, canvas.width / 2, 100);
ctx.textAlign = "left";
ctx.textBaseline = 'top';
ctx.font = "100 25px Helvetica";
this.buttons.forEach(btn => {
ctx.fillStyle = btn.color;
ctx.fillText(btn.text, btn.x, btn.y);
//ctx.fillRect(btn.x, btn.y, btn.w, btn.h);
});
ctx.textBaseline = 'alphabetic';
ctx.textAlign = "center";
ctx.font = "10px Helvetica";
ctx.fillStyle = "#e0e0e0";
ctx.fillText("antony999k", canvas.width / 2, canvas.height - 18);
}
}