-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.htm
379 lines (303 loc) · 9.52 KB
/
pong.htm
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Pong!</title>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<style type="text/css">.clearfix { zoom: 1; display: block; }</style>
<![endif]-->
<style type="text/css">
* { margin:0; padding:0; }
html, body {
height:98%;
font-family: sans-serif;
}
h1 {
text-align: center;
color: #fff;
font:2em bold sans-serif;
margin-top:5px;
}
.spacer {
width: 5%;
display: inline-block;
}
body {
background: #000;
border:15px solid #fff;
}
#ball, .paddle {
background: #fff;
}
#ball {
height:50px;
width:50px;
position: absolute;
top:50%;
left:50%;
margin: -25px 0 0 -25px;
}
.paddle {
position: absolute;
height:220px;
width:30px;
top:50%;
margin-top:-110px;
}
#rightPaddle { right:25px; }
#leftPaddle { left:25px; }
#game-over {
position: absolute;
top:0;
left:0;
width:100%;
height:100%;
background: #000;
color: #fff;
text-align: center;
padding-top:20%;
}
#game-over h2 {
font-size: 5em;
}
#game-over a {
margin-bottom: 1%;
display: inline-block;
font-size: 1.25em;
}
button {
background: #fff;
border:0;
padding: .5% 3%;
font-size: 1.5em;
color: #000;
font-weight: bold;
}
.divider {
height:100%;
border:5px dashed #fff;
left:50%;
top:15px;
margin-left:-5px;
display: block;
position: absolute;
}
</style>
</head>
<body>
<section>
<h1></h1>
<span class="divider"></span>
<span id="ball"></span>
<span class="paddle" id="leftPaddle"></span>
<span class="paddle" id="rightPaddle"></span>
</section>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="leap.js"></script>
<script type="text/javascript">
// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
log.history = log.history || []; // store logs to an array for reference
log.history.push(arguments);
if(this.console){
console.log( Array.prototype.slice.call(arguments) );
}
};
var previousFrame = null,
leapEnabled = true,
gameEnabled = true,
ball = null,
leftPaddle = null,
rightPaddle = null,
leftPaddleStartY = 0,
rightPaddleStartY = 0,
ballDirection = -1,
speed = 5,
ballSkew = 0.3,
lastRightYOffset = 0.1,
lastLeftYOffset = 0.1,
beep = new Audio("beep.wav"),
leftPlayerScore = 0,
rightPlayerScore = 0;
$(function(){
ball = $('#ball');
leftPaddle = $('#leftPaddle');
rightPaddle = $('#rightPaddle');
// Allow the player to pause
$(document).on('keyup', 'body', function(e){
if(e.keyCode == 80){
leapEnabled = !leapEnabled;
gameEnabled = !gameEnabled;
if(!gameEnabled){
$('h1').html('GAME <span class="spacer"></span> PAUSED');
}else {
$('h1').html(rightPlayerScore + '<span class="spacer"></span>' + rightPlayerScore);
moveBall();
}
} // if
});
// Tell the user they suck at life
function gameOver(){
leapEnabled = false;
gameEnabled = false;
console.log("Game over :(");
// Display a message and allow them to reset
$('body').append('<div id="game-over"><h2>Game Over</h2><br><a>← Waggle your finger to restart, or click below →</a><br><button id="restart">Restart</button></div>');
} // gameOver
// Increment a player's score by 1
function score(player){
if(player == 'left'){
leftPlayerScore++;
}else if(player == 'right') {
rightPlayerScore++;
}
$('h1').html(leftPlayerScore + '<span class="spacer"></span>' + rightPlayerScore);
} // score
// Animate the ball ever 10ms
function moveBall(){
if(gameEnabled){
var offsetLeft = parseInt(ball.css('left').replace('px', '')),
offsetTop = parseInt(ball.css('top').replace('px', ''));
// Ensure we have values for our offsets
offsetLeft = (offsetLeft == null) ? ball.offset().left : offsetLeft;
offsetTop = (offsetTop == null) ? ball.offset().top : offsetTop;
// Is the skew causing it to go off the screen on the y-axis?
if(offsetTop < 35 || offsetTop > ($('body').height() - 35)){
ballSkew *= -1;
// Correct the error
offsetTop = (offsetTop + (30 * (ballSkew / Math.abs(ballSkew))));
//lastLeftYOffset *= -1;
//lastRightYOffset *= -1;
beep.play();
} // if
// Track the ball
var ballTop = ball.offset().top,
ballBottom = ballTop + ball.height();
// Is the ball about to go off the screen on the x-axis?
if(offsetLeft < 75){
// Hits the left side
// Test if the leftPaddle was in the right place
var leftPaddleTop = leftPaddle.offset().top,
leftPaddleBottom = leftPaddleTop + leftPaddle.height();
// Have we hit the leftPaddle?
if((leftPaddleTop - ball.height()) <= ballBottom && (leftPaddleBottom + ball.height()) >= ballBottom){
ballDirection = (ballDirection == -1) ? 1 : -1;
// Update the skew of the ball
ballSkew = (lastLeftYOffset / 15);
beep.play();
// Speed the ball up
if(speed > 1){
speed--;
}
} else {
score('right');
restartGame();
// Stop the current execution
return 0;
} // if
}else if(offsetLeft > ($('body').width() - 50)){
// Hits the right side
// Test if the rightPaddle was in the right place
var rightPaddleTop = rightPaddle.offset().top,
rightPaddleBottom = rightPaddleTop + rightPaddle.height();
// Have we hit the rightPaddle?
if((rightPaddleTop - ball.height()) <= ballBottom && (rightPaddleBottom + ball.height()) >= ballBottom){
ballDirection = (ballDirection == -1) ? 1 : -1;
// Update the skew of the ball
ballSkew = (lastRightYOffset / 15);
beep.play();
// Speed the ball up
if(speed > 1){
speed--;
}
} else {
score('left');
restartGame();
// Stop the current execution
return 0;
} // if
}
ball.animate({
left: offsetLeft + ((speed + (10 - speed)) * ballDirection),
top : offsetTop + ballSkew
}, speed, function(){
setTimeout(function(){ moveBall() }, 10);
});
}else {
console.log("The game isn't enabled :(");
} // if
} // moveBall
var leftPaddleOrigin, rightPaddleOrigin;
// Restart after a score increment
function restartGame(){
// Reset the skew
ballSkew = 0.1;
// Reset the ball
ball.css({ top: '50%', left: '50%', marginLeft: -25, marginTop:-25 });
// Reset the speed
speed = 5;
// Start the ball moving again
setTimeout(function() { moveBall(); }, 1000);
} // restartGame
// Game initilisation method
function startGame(){
// Keep track of the paddles's starting positions
leftPaddleOrigin = leftPaddle.offset().top;
rightPaddleOrigin = rightPaddle.offset().top;
$('#game-over').remove();
// Reset the score
score();
// Move the ball into position
ball.css({ top: '50%', left: '50%', marginLeft: -25, marginTop:-25 });
speed = 5;
leapEnabled = true;
gameEnabled = true;
// Start the game
moveBall();
}
// Allow the player to reset the game
$('body').on('click', 'restart', function(){
startGame();
});
// Allow Leap to move the paddles
Leap.loop(function(frame){
//Frame motion factors
if(leapEnabled){
// Make sure we have 2 fingers
if (frame.pointables.length == 2){
// Get the 2 fingers for left and right
// Use the height of the browser to get a relative offset
var leftYOffset = ((frame.pointables[0].tipPosition[1] * ($('body').height() / 160)) - $('body').height()) * -1,
rightYOffset = ((frame.pointables[1].tipPosition[1] * ($('body').height() / 160)) - $('body').height()) * -1;
// Make our offset available to the game function
lastRightYOffset = rightYOffset;
lastLeftYOffset = leftYOffset;
// Has the left finger moved?
if(Math.abs(leftYOffset) > 1){
var newYOffset = leftPaddleOrigin + leftYOffset;
// Is the paddle about to go off-screen?
if(newYOffset >= 20 && (newYOffset + leftPaddle.height()) <= $('body').height()){
// Move the paddle to the new position
leftPaddle.offset({ top: leftPaddleOrigin + leftYOffset });
} // if
} // if
// Has the right finger moved?
if(Math.abs(rightYOffset) > 1){
var newYOffset = rightPaddleOrigin + rightYOffset;
// Is the paddle about to go off-screen?
if(newYOffset >= 20 && (newYOffset + rightPaddle.height()) <= $('body').height()){
// Move the paddle to the new position
rightPaddle.offset({ top: rightPaddleOrigin + rightYOffset });
} // if
} // if
} // if previousFrame
} // if leapEnabled
});
// Start the actual game
startGame();
});
</script>
</body>
</html>