-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPongGame - Bots.html
369 lines (322 loc) · 7.61 KB
/
PongGame - Bots.html
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
<!DOCTYPE html>
<head>
<style>
canvas {
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
}
</style>
</head>
<body>
<canvas id="canvas1" tabindex="0" style="border:1px solid #000000;">
</canvas>
<script>
//Default canvas code.
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");
var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame;
//Sets the canas to fullscren.
canvas.width = 800;
canvas.height = 400;
//Audio declaration
var blip = [];
for (i = 1; i <= 7; i++)
{
blip[i] = new Audio();
blip[i].src = 'blip' + [i] + '.mp3';
}
//Seperator attributes
var sepWidth = 5;
var sepHeight = 10;
var sepColor = "white";
var sepX = (canvas.width/2) - (sepWidth/2);
var sepY = 0;
//Shared players attributes
var barWidth = 10;
var barHeight = 100;
var barColor = "white";
var barSpeed = 0;
//First player position.
var player1BarX = 0;
var player1BarY = (canvas.height / 2) - (barHeight / 2);
//Second player position.
var player2BarX = canvas.width - barWidth;
var player2BarY = (canvas.height / 2) - (barHeight / 2);
//Level attribute
var levelFont = "25px Arial";
var levelX = 0;
var levelY = canvas.height;
var level = 1;
//Shared score attribute
var scoreFont = "50px Arial";
var scoreY = 50;
//Player 1 score
var player1Score = 0;
var player1ScoreX = sepX - 75;
//Player 2 score
var player2Score = 0;
var player2ScoreX = sepX + 50;
//Score conditions
var scoreWin = 3;
var scoreLose = 3;
//Ball attributes
var ballX = canvas.width/2;
var ballY = canvas.height/2;
var ballRad = 7.5;
var ballColor = "white";
var ballSpeed = 6;
//Ball movements
var goDown = false;
var goUp = false;
var goRight = true;
var goLeft = false;
//The delay time is in milliseconds.
var delayBetweenFrames = 0;
function aiMovement()
{
if (ballY > player2BarY + barHeight)
{
player2BarY++;
}
else if (ballY < player2BarY)
{
player2BarY--;
}
if (ballY > player1BarY + barHeight)
{
player1BarY++;
}
else if (ballY < player1BarY)
{
player1BarY--;
}
}
function getDistance(barX, barY)
{
//Collision detection
if (barX < ballX + ballRad
&& barX + barWidth > ballX
&& barY < ballY + ballRad
&& barY + barHeight > ballY)
{
return true;
}
}
function barLimitCheck(barY, keyInput)
{
//If the bar reaches the canvas limit, then stop it from going outside.
if (barY == (canvas.height - barHeight) && (keyInput == 40 || keyInput == 83))
{
barSpeed = 0;
}
else if (barY == 0 && (keyInput == 38 || keyInput == 87))
{
barSpeed = 0;
}
else
{
barSpeed = 1;
}
return barSpeed;
}
function drawRectangle(x, y, width, height, color)
{
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
function drawCircle(x, y, radius, color)
{
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
}
function drawText(font, text, x, y)
{
ctx.font = font;
ctx.fillText(text, x, y);
}
function drawSeperator()
{
while (sepY < canvas.height)
{
drawRectangle(sepX, sepY, sepWidth, sepHeight, sepColor);
sepY += 20;
}
//If the sepeator reaches canvas limit, then its Y will be reset to 0 so it can continue drawing the seperator.
if (sepY >= canvas.height)
{
sepY = 0;
}
}
function resetBall()
{
ballX = canvas.width/2;
ballY = canvas.height/2;
}
function ballMovement()
{
if (goDown)
{
ballY += ballSpeed;
}
if (goUp)
{
ballY -= ballSpeed;
}
if (goRight)
{
ballX += ballSpeed;
}
if (goLeft)
{
ballX -= ballSpeed;
}
if (ballY >= canvas.height)
{
blip[3].play();
goUp = true;
goDown = false;
}
if (ballY <= 0)
{
blip[4].play();
goUp = false;
goDown = true;
}
}
function hitReaction(playerNum)
{
if (playerNum == 1)
{
goLeft = false;
goRight = true;
blip[1].play();
}
else
{
goLeft = true;
goRight = false;
blip[2].play();
}
if (Math.random() <= 0.33)
{
goUp = true;
goDown = false;
}
else if (Math.random() >= 0.66)
{
goUp = false;
goDown = true;
}
else
{
goUp = false;
goDown = false;
}
}
function goalDetection()
{
if (ballX >= canvas.width)
{
blip[5].play();
player1Score++;
resetBall();
goRight = false;
goLeft = true;
}
if (ballX <= 0)
{
blip[5].play();
player2Score++;
resetBall();
goRight = true;
goLeft = false;
}
}
function winLevel()
{
blip[6].play();
level++;
ballSpeed += 0.25;
scoreWin++;
scoreLose--;
barHeight -= 20;
player1Score = 0;
player2Score = 0;
}
function loseLevel(gameComplete)
{
if (gameComplete == 1)
{
blip[6].play();
alert("You have completed the game!");
}
else
{
blip[7].play();
alert("Game over!");
}
//Resets the game stats.
level = 1;
ballSpeed = 4;
scoreWin = 3;
scoreLose = 3;
barHeight = 100;
player1Score = 0;
player2Score = 0;
}
//Declare the animate() function.
function animate()
{
//Use the setTimeout to do the animation between frame using the delayBetweenFrames.
setTimeout(function()
{
//Clears the canvas.
ctx.clearRect(0, 0, canvas.width, canvas.height);
//Animation code goes into this anonymous function handler.
requestAnimationFrame(animate);
//Draws the black background.
drawRectangle(0, 0, canvas.width, canvas.height, "black");
//Draws the seperator.
drawSeperator();
//Moves the ball.
ballMovement();
//Draws the ball.
drawCircle(ballX, ballY, ballRad, ballColor);
//Moves the bar if no player is present
aiMovement();
//Detects goals
goalDetection();
//Draws the first player.
drawRectangle(player1BarX, player1BarY, barWidth, barHeight, barColor);
//Draws the second player.
drawRectangle(player2BarX, player2BarY, barWidth, barHeight, barColor);
//Draws 1st player score.
drawText(scoreFont, player1Score, player1ScoreX, scoreY);
//Draws 2nd player score.
drawText(scoreFont, player2Score, player2ScoreX, scoreY);
//Draws the level number.
drawText(levelFont, "Level: " + level, levelX, levelY);
//If the ball hits the bar
if (getDistance(player1BarX, player1BarY))
{
hitReaction(1);
}
if (getDistance(player2BarX, player2BarY))
{
hitReaction(2);
}
}, delayBetweenFrames);
}
//Calls the animate() function.
animate();
</script>
</body>
</html>