4-sided two player pong game in 1024Bytes ~ 1 year ago
js1k is a new contest in which participants have to do something in Javascript in no more than 1024 Bytes.
I could not resist and took the bait, so I've done a 2-sided two player pong game, that you play clicking on the image:
Don't forget to check out the rest of participants, great stuff! :)
The most difficult thing was to put all the code, that initially was 2029 Bytes, to 1024 Bytes. It ended preety obfuscated:
var d=document;
var C=d.getElementById('c');
d.onkeydown=kd;
d.onkeyup=ku;
d.onmousemove=mm;
var K=[], M=Math, w=500, h=w, c= C.getContext('2d'),
t, V=0, a, H=h, O, P, A, B, J=['#8cf','#c8f'], li, v=20, z=100, G=v, E=0, r=120, n=50;
C.width=w;C.height=600;
Z(0);
window.setInterval(l, 26);
function Z(q)
{
E+=q;
O=P=250;
a= M.random()*2*M.PI;
A=M.cos(a)*5;
B=M.sin(a)*5;
l();
t=30;
}
function l()
{
if(t<=0){
c.globalAlpha=0.4;
f(0,0,w,600,'#fff');
f(v,v,w-40,h-40,'#ddd');
c.globalAlpha=1;
f(250,h+v,E,5,J[E>0?1:0]);
if (K[65]) V-=3;
if (K[90]) V+=3;
V= V<-v?-v:V>v?v:V;
G+= V;
if(G<v){ G=v; V=0;}
if(G>h-r){ G=h-r; V=0;}
V*=0.8;
f(0,G,v,z,J[0]);
f(G,0,z,v,J[0]);
H= H<v?v:H>h-r?h-r:H;
f(w-v,H,v,z,J[1]);
f(H,h-v,z,v,J[1]);
O+= A;
P+= B;
if (O<v || P<v) Z(v);
if (O>w-v || P>h-v) Z(-v);
if ((O<30 && M.abs(P-(G+n))<n) || (O>w-30 && M.abs(P-(H+n))<n)) A*=-1-M.random()*0.2;
if ((P<30 && M.abs(O-(G+n))<n) || (P>h-30 && M.abs(O-(H+n))<n)) B*=-1-M.random()*0.2;
f(O-10,P-10,v,v,'#999')
}t--;
}
function f(y,u,i,o,x){c.fillStyle=x;c.fillRect(y,u,i,o);}
function kd(e){K[e.keyCode]=1;}
function ku(e){K[e.keyCode]=0;}
function mm(e){H=e.pageY-40;}
var C=d.getElementById('c');
d.onkeydown=kd;
d.onkeyup=ku;
d.onmousemove=mm;
var K=[], M=Math, w=500, h=w, c= C.getContext('2d'),
t, V=0, a, H=h, O, P, A, B, J=['#8cf','#c8f'], li, v=20, z=100, G=v, E=0, r=120, n=50;
C.width=w;C.height=600;
Z(0);
window.setInterval(l, 26);
function Z(q)
{
E+=q;
O=P=250;
a= M.random()*2*M.PI;
A=M.cos(a)*5;
B=M.sin(a)*5;
l();
t=30;
}
function l()
{
if(t<=0){
c.globalAlpha=0.4;
f(0,0,w,600,'#fff');
f(v,v,w-40,h-40,'#ddd');
c.globalAlpha=1;
f(250,h+v,E,5,J[E>0?1:0]);
if (K[65]) V-=3;
if (K[90]) V+=3;
V= V<-v?-v:V>v?v:V;
G+= V;
if(G<v){ G=v; V=0;}
if(G>h-r){ G=h-r; V=0;}
V*=0.8;
f(0,G,v,z,J[0]);
f(G,0,z,v,J[0]);
H= H<v?v:H>h-r?h-r:H;
f(w-v,H,v,z,J[1]);
f(H,h-v,z,v,J[1]);
O+= A;
P+= B;
if (O<v || P<v) Z(v);
if (O>w-v || P>h-v) Z(-v);
if ((O<30 && M.abs(P-(G+n))<n) || (O>w-30 && M.abs(P-(H+n))<n)) A*=-1-M.random()*0.2;
if ((P<30 && M.abs(O-(G+n))<n) || (P>h-30 && M.abs(O-(H+n))<n)) B*=-1-M.random()*0.2;
f(O-10,P-10,v,v,'#999')
}t--;
}
function f(y,u,i,o,x){c.fillStyle=x;c.fillRect(y,u,i,o);}
function kd(e){K[e.keyCode]=1;}
function ku(e){K[e.keyCode]=0;}
function mm(e){H=e.pageY-40;}

