-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.html
387 lines (308 loc) · 6.5 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
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</head>
<body>
<table>
<tr>
<td>
<table>
<tr>
<td>
<audio preload>
<source src="pong.wav" type="audio/wav"/>
</audio>
<audio preload>
<source src="point.wav" type="audio/wav"/>
</audio>
<font size="36" color="#FF0000"><div id="scoreplayer1">0</div>
</td>
<td>
</font><font size="36" color="#0000FF"><div id="scoreplayer2">0</div></font>
</td>
</div>
</td>
</tr>
<tr>
<td>
<canvas id="myCanvas" width="600" height="400" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
</td>
</tr>
<tr>
<td>
<ul>
<li>
Taille balle: <div id="radiusIn"/> <!--<input type="range" id="radiusIn" name="radiusIn" value="20" min="5" max="40" />-->
</li>
<li>
Vitesse balle: <div id="speedIn"/> <!--<input type="range" id="speedIn" name="speedIn" value="4" min="1" max="10" />-->
</li>
<li>
Taille joueur 1: <div id="player1SizeIn"/> <!--<input type="range" id="player1SizeIn" name="player1SizeIn" value="200" min="50" max="200" />-->
</li>
<li>
Taille joueur 2: <div id="player2SizeIn"/> <!--<input type="range" id="player2SizeIn" name="player2SizeIn" value="200" min="5" max="200" />-->
</li>
</td>
</tr>
</table>
<script>
$(document).ready(function() {
$( "#radiusIn" ).slider({min:5,max:40, animate:true, value:15});
$( "#speedIn" ).slider({min:1,max:10, animate:true, value:6});
$( "#player1SizeIn" ).slider({min:50,max:200, animate:true, value:100});
$( "#player2SizeIn" ).slider({min:50,max:200, animate:true, value:100});
timer();
});
</script>
<script type="text/javascript">
function Player() {
this.color="#00A";
this.x= 0;
this.y= 0;
this.width= 10;
this.height= 200;
this.draw=Draw;
function Draw() {
cxt.fillStyle = this.color;
cxt.fillRect(this.x, this.y, this.width, this.height);
}
}
var planet = {
color: "#00FF00",
x: 220,
y: 270,
radius: 20,
width: 40,
height: 40,
score:0,
draw: function() {
cxt.beginPath();
cxt.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
cxt.fillStyle = this.color;
cxt.fill();
//cxt.lineWidth = 2;
//cxt.strokeStyle = "black";
//cxt.stroke();
cxt.closePath();
}
};
/*
img = new Image(); // Create new img element
img.src = 'franck.jpeg'; // Set source path
var planet = {
color: "#0AA",
x: 220,
y: 270,
radius: 20,
width: img.width,
height: img.height,
score:0,
draw: function() {
cxt.beginPath();
cxt.drawImage(img, planet.x, planet.y)
cxt.closePath();
}
};
*/
i=0;
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
/*c.addEventListener("mousemove", function(e){
x=e.clientX;
y=e.clientY;
});*/
window.addEventListener("keydown", function(e){
if(e.keyCode == 38) {/* up2 was pressed */
up2=true;
}
if(e.keyCode == 40) {/* down2 was pressed */
down2=true;
}
if(e.keyCode == 65) {/* a was pressed */
a1=true;
}
if(e.keyCode == 87) {/* w was pressed */
w1=true;
}
if(e.keyCode == 83) {/* s was pressed */
s1=true;
}
if(e.keyCode == 37) {/* left1 was pressed */
left2=true;
}
});
window.addEventListener("keyup", function(e){
if(e.keyCode == 65) {/* a was pressed */
a1=false;
}
if(e.keyCode == 38) {/* up2 arrow was pressed */
up2=false;
}
if(e.keyCode == 40) {/* down2 arrow was pressed */
down2=false;
}
if(e.keyCode == 87) {/* w was pressed */
w1=false;
}
if(e.keyCode == 83) {/* s was pressed */
s1=false;
}
if(e.keyCode == 37) {/* left was pressed */
left2=false;
}
});
var a1=false;
var w1=false;
var up2=false;
var down2=false;
var left2=false;
var s1=false;
var col1=true;
var col2=false;
var colh=true;
var colb=false;
var player1 = new Player();
var player2 = new Player();
var score1=0;
var score2=0;
var speed=4;
var audioPong = document.getElementsByTagName("audio")[0];
var audioPoint = document.getElementsByTagName("audio")[1];
player1.x= 20;
player1.y= 150;
player1.color= "#FF0000";
player2.x= 600-32-20;
player2.y= 150;
player2.color="#0000FF";//blue
function timer(){
cxt.clearRect ( 0 , 0 , 600 , 400 );
//cxt.fillStyle="#FF0000";//rouge
//cxt.fillRect(0+i%100,0+i%100,150,75);
//i++;
//cxt.fillRect(x,y,150,75);
if(a1==true)
player1.y=player1.y-5;
if(w1==true)
player1.y=player1.y+5;
if(up2==true)
player2.y=player2.y-5;
if(down2==true)
player2.y=player2.y+5;
player1.height=$("#player1SizeIn").slider("value");
player1.draw();
player2.height=$("#player2SizeIn").slider("value");
player2.draw();
planet.radius = $("#radiusIn").slider("value");
planet.width=planet.radius*2;
planet.height=planet.radius*2;
planet.draw();
//rebond joueur1
if(collides(player1,planet))
{
audioPong.play();
col1 = true;
col2 = false;
//if(planet.y<player1.y+player1.height/2)
//angle inverse
if(a1)
{
colb= true;
colh= false;
}
else if(w1)
{
colb= false;
colh= true;
}
//coup à plat
else if(s1)
{
colb= false;
colh=false;
}
}
//rebond joueur2
if(collides(player2,planet))
{
audioPong.play();
col2 = true;
col1 = false;
//angle inverse
if(up2)
{
colb= true;
colh= false;
}
else if(down2)
{
colb= false;
colh= true;
}
//coup à plat
else if(left2)
{
colb= false;
colh=false;
}
}
//rebond bas
if(planet.y+planet.radius>=c.height)
{
colb=true;
colh=false;
}
//rebond haut
if(planet.y-planet.radius<=0)
{
colh=true;
colb=false;
}
//score contre player1 gauche
if(planet.x<=0)
{
audioPoint.play();
score2++;
document.getElementById("scoreplayer2").innerHTML = score2;
planet.x=c.width/2;
}
//score contre player1 gauche
if(planet.x>=c.width)
{
audioPoint.play();
score1++;
document.getElementById("scoreplayer1").innerHTML = score1;
planet.x=c.width/2;
}
speed = $("#speedIn").slider("value");
if(col1)
{
planet.x=planet.x+speed;
}
else if(col2)
{
planet.x=planet.x-speed;
}
if(colb)
{
planet.y=planet.y-speed;
}else if(colh)
{
planet.y=planet.y+speed;
}
setTimeout("timer()",16);
}
function collides(a, b) {
return a.x < b.x-b.radius + b.width &&
a.x + a.width > b.x-b.radius &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
</script>
</body>
</html>