-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.js
134 lines (120 loc) · 2.59 KB
/
pong.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
pong=function(){
//600x600 screen
var width=600;
var height=400;
var screen=document.getElementById('pongCanvas').getContext('2d');
var ball={x:300,y:300,r:10};
var lastBall={x:300,y:300,r:10};
//speed vector for ball
var ballVec={x:3,y:2};
var maxSpeed=20;
var left={y:200,w:10,h:100,speed:0};
var leftVec=0;
var right={y:200,w:10,h:100,speed:0};
var rightVec=0;
var scores={left:0,right:0};
//pixel margin of error
var margin=5;
var scored=function(left){
ball={x:300,y:300,r:10};
ballVec={x:(left?5:-5),y:2};
if(left){
scores.left++;
uScore();
}else{
scores.right++;
uScore();
}
}
var sDisp=document.getElementById('score');
var uScore=function(){
sDisp.innerHTML=scores.left+':'+scores.right;
}
var onPaddleCollision=function(paddle){
ballVec.x=-1.5*ballVec.x;
ballVec.y-=paddle.speed;
}
var onFloorCollision=function(){
ballVec.y=-ballVec.y;
}
var rect=function(x,y,w,h,ctx,color){
ctx.fillStyle=color;
ctx.fillRect(x,y,w,h);
}
var circle=function(x,y,r,ctx,color){
ctx.fillStyle=color;
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI,true);
ctx.fill();
}
var draw=function(){
rect(0,0,width,height,screen,'black');
rect(width/2-2.5,0,5,height,screen,'white');
//draw left panel
rect(0,left.y,left.w,left.h,screen,'white');
//draw right panel
rect(width-right.w,right.y,right.w,right.h,screen,'white');
//draw ball
circle(ball.x,ball.y,ball.r,screen,'white');
}
var min=function(a,b){
if(a<b) return a;
return b;
}
var max=function(a,b){
if (a>b) return a;
return b;
}
var tick=function(){
//update speeds
ball.x+=ballVec.x;
ball.y+=ballVec.y;
if(keys[69])
left.speed=min(left.speed+10,maxSpeed);
if(keys[68])
left.speed=max(left.speed-10,-maxSpeed);
if(keys[79])
right.speed=min(right.speed+10,maxSpeed);
if(keys[76])
right.speed=max(right.speed-10,-maxSpeed);
left.y-=left.speed;
if(left.y<0){
}
right.y-=right.speed;
left.speed*=0.7;
right.speed*=0.7;
ball.x*=0.999999999999999;
ball.y*=0.9999999999999999;
if(ball.y<0 || ball.y>height){
onFloorCollision();
}
if(ball.x<=left.w){//if hits left
if(ball.y>=left.y-margin && ball.y<=(left.y+left.h+margin)){
onPaddleCollision(left);
}else{//passes line left
scored(false);
}
}
if(ball.x>=width-right.w){//if hits right
if(ball.y>=right.y-margin && ball.y<=(right.y+right.h+margin)){
onPaddleCollision(right);
}else{//passes line
scored(true);
}
}
}
var keys=[];
document.onkeydown=function(evt){
console.log(evt.which);
keys[evt.which]=true;
}
document.onkeyup=function(evt){
keys[evt.which]=false;
}
var onFrame=function(){
draw();
tick();
setTimeout(onFrame,50);
}
setTimeout(onFrame,20);
};