-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.html
416 lines (387 loc) · 8.81 KB
/
pong.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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pong</title>
<script type="text/javascript" src="js/jQuery.js"></script>
<script>
// JavaScript Document
var cwidth = 650;
var cheight = 330;
var ballrad = 10;
var boxx = 5;
var boxy = 5;
var boxwidth = 600;
var boxheight = 300;
var boxboundx = boxwidth+boxx-ballrad;
var boxboundy = boxheight+boxy-ballrad;
var inboxboundx = boxx+ballrad;
var inboxboundy = boxy+ballrad;
var ballx = boxwidth/2+boxx;
var bally = boxheight/2+boxy;
var ctx;
var ballvx = 2;
var ballvy = 2;
var maxv = 6;
var up = false;
var down = false;
var up2 = false;
var down2 = false;
var intervalid;
var moveinterval;
var speedinterval;
var player1=0;
var player2=0;
var countdown = 0;
var totalHits = 0;
//////////////////////////////////////
//Moving Ball Functions
//////////////////////////////////////
function init() {
ctx = document.getElementById('canvas').getContext('2d');
ctx.lineWidth = ballrad;
moveball();
getready();
setTimeout(function() {
startCountdown();},1000);// buffers automatically when created
setTimeout(function(){
speedinterval = setInterval(speedup,1000);
moveinterval = setInterval(doublemovement,16.5);
intervalid = setInterval(moveball,16.5);clearInterval(startInterval);start();},5000);
}
function speedup()
{
if(ballvy<0)
ballvy-=.25;
else
ballvy +=1;
if(ballvx<0)
ballvx-=.25;
else
ballvx +=1;
}
function calcspeed(right)
{
if(right==false) //////check for hit on the left paddle
{
if(bally<=y+ll/2+5&&bally>=y+ll/2-5)
{
ballvy = 0;
ballvx +=1.1;
}
else if(ballvy<0&&up)
{
ballvy+=1;
ballvx-=.1;
}
else if(ballvy<0&&down)
{
ballvy-=.1;
ballvx+=1;
}
else if(ballvy>0&&down)
{
ballvy-=.1;
ballvx+=1;
}
else if(ballvy>0&&up)
{
ballvy+=1;
ballvx-=.1;
}
}
else if(right==true) ///check for hit on right paddle
{
if(bally<=yy+ll/2+5&&bally>=yy+ll/2-5)
{
if(ballvx<maxv){
ballvy +=1;
ballvx +=1.5;
}
}
if(ballvy<0)
{
if(ballvx<maxv){
ballvy+=1;
ballvx+=1.5;
}
}
}
}
function doublemovement()
{
if(up)
{
if(y-boxy>0)
y-=dy;
}
if(up2)
{
if(yy-boxy>0)
yy-=dyy;
}
if(down)
{
if(y+l/2<boxheight)
y+=dy;
}
if(down2)
{
if(yy+l/2<boxheight)
yy+=dyy;
}
draw();
draw2();
}
//////////////////////////////////////////////////
////calculates and draws the balls movements
//////////////////////////////////////////////////
function moveball() {
ctx.lineWidth = ballrad;
ctx.clearRect(boxx,boxy,boxwidth,boxheight);;
ctx.beginPath();
ctx.strokeRect(boxx,boxy,boxwidth,boxheight);
ctx.fillStyle = "rgb(0,0,255)";
ctx.fill(); ///draw playing field
ctx.lineWidth = 4;
ctx.moveTo(boxx+boxwidth/2,boxy);
ctx.lineTo(boxx+boxwidth/2,boxy+boxheight); ///draw middle line
ctx.strokeRect(boxx,boxy-50+boxheight/2,15,100); ///draw left goal box
ctx.strokeRect(boxx+boxwidth-15,boxy-50+boxheight/2,15,100); ///draw right goal box
ctx.stroke();
ctx.closePath();
ctx.fillStyle = "rgb(200,0,50)";
ctx.arc(ballx,bally,ballrad,0,Math.PI*2,true); ///draw ball
ctx.fill();
moveandcheck();
draw();
draw2();
}
/////////////////////////////////////
//////checks balls motion with walls and collisions with the paddles
/////////////////////////////////////
function moveandcheck() {
var nballx = ballx+ballvx;
var nbally = bally +ballvy;
doublemovement();
if((nballx>=xx-ballrad&&nballx<=xx+5)&&(bally<yy+l+ballrad&&bally>yy-ballrad)) //////check for hit on the right paddle
{
console.log('hit');
console.log(totalHits);
$('#hits').html(++totalHits);
if(totalHits==10){window.location='frogger.html';}
nballx = xx-ballrad;
ballx = nballx;
bally = nbally;
calcspeed(true);
ballvx = -ballvx;
}
else if((nballx<=x+r+ballrad&&nballx>=x+r-5)&&(bally<y+l+ballrad&&bally>y-ballrad)) ///check for hit on left paddle
{
nballx = x+r+ballrad;
ballx = nballx;
bally = nbally;
calcspeed(false);
ballvx = -ballvx;
}
else if(nballx>boxboundx) { //point for player 1 = right side
$('#winner').html('<h1>Player 1 Wins!!</h1>');
$('#player1').html(++player1);
resetnewgame();
}
else if(nballx<inboxboundx) { //point for player 2 = left
$('#winner').html('<h1>Player 2 Wins!!</h1>');
$('#player2').html(++player2);
resetnewgame();
}
else if(nbally>boxboundy) {
nbally = boxboundy;
ballvy = -ballvy;
ballx = nballx;
bally = nbally;
}
else if(nbally<inboxboundy) {
nbally = inboxboundy;
ballvy = -ballvy;
ballx = nballx;
bally = nbally;
}
else
{
ballx = nballx;
bally = nbally;
}
}
function getready()
{
$('#winner').html('<h2 style ="text-align:center;">Get Ready</h2>');
}
function start()
{
$('#winner').html('<h1 style ="text-align:center;">Go!</h1>');
}
function startCountdown()
{
startInterval = setInterval(Countdown,1000);
}
function Countdown()
{
switch(++countdown)
{
case 1:
$('#winner').html('<h3 style ="text-align:center;">3</h3>');
break;
case 2:
$('#winner').html('<h2 style ="text-align:center;">2</h2>');
break;
case 3:
$('#winner').html('<h1 style ="text-align:center;">1</h1>');
break;
}
}
function resetnewgame()
{
totalHits = 0;
$('hits').html(totalHits);
clearInterval(moveinterval);
clearInterval(intervalid);
clearInterval(speedinterval);
ballx = boxwidth/2+boxx;
bally = boxheight/2+boxy;
ballvx = 2;
ballvy = 2;
countdown = 0;
moveball();
getready();
up = false;
down = false;
up2 = false;
down2 = false;
setTimeout(function() {
startCountdown();},1000);
setTimeout(function(){
speedinterval = setInterval(speedup,1000);
moveinterval = setInterval(doublemovement,16.5);
intervalid = setInterval(moveball,16.5);clearInterval(startInterval); start();},5000);
}
//////////////////////////////////////////////
///Watches the Key presses and sets motion
//////////////////////////////////////////////
$(document).keydown(function (e) {
var keyCode = e.keyCode || e.which,
arrow = { up: 87, down: 83, up2:38,down2:40 };
switch (keyCode) {
case arrow.up:
up = true;
break;
case arrow.down:
down = true;
break;
case arrow.up2: /* Up arrow was pressed */
up2 = true;
break;
case arrow.down2: /* Down arrow was pressed */
down2 = true;
}
});
//////////////////////////////////////////////
///Watches the Key presses and sets motion
//////////////////////////////////////////////
$(document).keyup(function (e) {
var keyCode = e.keyCode || e.which,
arrow = { up: 87, down: 83, up2:38, down2:40 };
switch (keyCode) {
case arrow.up:
up = false;
break;
case arrow.down:
down = false;
break;
case arrow.up2: /* Up arrow was pressed */
up2 = false;
break;
case arrow.down2: /* Down arrow was pressed */
down2 = false;
}
});
////////////////////////////////
////Moving Right-Stick functions
////////////////////////////////
var dx = 5;
var dy = 5;
var x = 70;
var y = 100;
var r = 5;
var l = 50;
var WIDTH = 400;
var HEIGHT = 300;
/////////////////////////
///draws Right paddle
////////////////////////
function circle(x,y,r) {
ctx.beginPath();
ctx.rect(x,y,r,l);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
/////////////////////////
///draws Right paddle
////////////////////////
function draw() {
ctx.lineWidth = 1;
ctx.fillStyle = "white";
ctx.strokeStyle = "black";
ctx.fillStyle = "black";y=bally-40;
circle(x, y, r);
}
/////////////////////////////////
////Moving Left-Stick functions
/////////////////////////////////
var dxx = 5;
var dyy = 5;
var xx = boxx+boxwidth-x;
var yy = 100;
var rr = 5;
var ll = 50;
var WIDTH = 400;
var HEIGHT = 300;
/////////////////////////
///draws Left paddle
////////////////////////
function circle2(xx,yy,rr) {
ctx.beginPath();
ctx.rect(xx,yy,rr,ll);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
/////////////////////////
///draws Left paddle
////////////////////////
function draw2() {
ctx.lineWidth = 1;
ctx.fillStyle = "white";
ctx.strokeStyle = "black";
ctx.fillStyle = "black";
circle2(xx, yy, rr);
}
</script>
</head>
<body id = "window" onLoad = "init()">
<div id = "winner" style = "display:inline; position:absolute;left:775px;font-size:25px;text-align:center;">
</div>
<canvas id="canvas" width = "650" height = "310" style = "position:absolute;top:150px;left:500px;">
Your browser doesn't support the HTML5 element canvas.
</canvas>
<div id = "winner" style = "display:inline; position:absolute;top:350px;left:10px">
</div>
<div id = "score" style = "display:inline; position:absolute;top:450px;left:750px">
<h4 style = "left:70px; text-align:center;text-decoration:underline;font-size:25px;">Score</h4>
Player 1: <label id = "player1">0</label>
Player 2: <label id = "player2">0</label><br />
Bounces off your paddle: <label id="hits">0</label>/10
</div>
<br/>
</body>
</html>