A tribute to the Atari 2600/Activision classic: Boxing. Arrow keys to move, Z and X to swing.
for(_='-1,Zfunction(Y.map(YX[Z1]XWonkeyV](UeeUT);RRc.Q:1P0,OO1NMath.L<Labs(Kc[c.J;JH)HGI.FFxEE+D]*Ca.B){A80@||b.=[];-m[:30M.a,]=or(p in }Qfill(b.p?M:o).-=2*Lsign(Qstroke(OO-l,ln={c:OaP,b:[3O3O2N0O60].push({p:g:D40*=4Rk[GbaUG200&&( FyFn.x]&&!d FDm[4C,d:[Z-6,-18,-3,18]}R3Cd={a:Ob:[,],n:{},h:"#fc)c[p[0]+[p[6]]p;Vdown=Vup=YaAk[Bwhich-37Btype[5]};kl=3.2;oddd",xOy0};M000",xP60Oy:600};setInterval(YAJld30Hfy"#684"HfcUON92N0@GsS"#c84"HbaUQrect(,10N52O6@RSiWbAWd,eAI=b?o:M;m=Fb[e]HfyJsSFhHfxUF1500*!b+,@TE,,4O4GT,1NRSt:,rP0+fP0+mUE,0CdGqtUE1C,E2CGqtU,T,3O3Rit:,r+f+Rif(b==e)if(8>c++)fd)m[p]+=d[pCa;else 0<a?(a=Zc=0):Fn=0})}Rk[0] E-1] -2] D3] +510k[531SXaAiXbAb.g>Brr<Bgt>Bff<Bt||((0>a++,(0<x+=@*b.p)})}RK-E) -D)R@Ky-) yy-+@)R.1>Lrandom()&&!n.d 0>-yE=250>E?250:E>-?-:E;=242>?242:638<?638:;=<D?DP670<?1670:},16R';G=/[^ -?IMS[-}]/.exec(_);)with(_.split(G))_=join(shift());eval(_)
Zm9yKF89Jy0xLFpmdW5jdGlvbihZLm1hcChZWFtaMV1YV29ua2V5Vl0oVWVlVVQpO1JSYy5ROjFQMCxPTzFOTWF0aC5MPExhYnMoS2NbYy5KO0pIKUhHSS5GRnhFRStEXSpDYS5CKXtBODBAfHxiLh89W107Hi1tWx06MzAcTS4bYSwaXT0Zb3IocCBpbiAYfVFmaWxsKBdiLnA/TTpvKS4WLT0yKkxzaWduKBVRc3Ryb2tlKBRPTy1s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leGVjKF8pOyl3aXRoKF8uc3BsaXQoRykpXz1qb2luKHNoaWZ0KCkpO2V2YWwoXyk=
if (window.c === undefined) c = document.getElementById('cvs').getContext('2d'); // shimmed
/*
* Boxing 1K by Jason "--jp" Plackey
* jp [at] chicagowebexperts [dot] com
*
* For JS1k 2019 - X
* Final packed entry size is 1022b.
*
* A tribute to an Atari 2600/Activision classic: Boxing.
* Arrow keys move, Z and X swing.
*
* For a write up on the creation, check out:
* http://www.chicagowebexperts.com/js1k/the-making-of-boxing-1k.html
*
*/
onkeydown = onkeyup = e => { K[e.which-37] = (e.type[5]); }
K = [];
P = 3.2;
W = {s:0,m:[[30,30,20,100,60],[30,30,20,100,60]],n:{},c:'#ddd',x:300,y:300}
B = {s:0,m:[[30,30,20,100,60],[30,30,20,100,60]],n:{},c:'#000',x:1600,y:600}
setInterval(()=>{
c.lineWidth = 30;
c.fillStyle = '#684';
c.fillRect(0,0,1920,1080);
c.strokeStyle = '#c84';
c.beginPath();
c.rect(200,100,1920-400,1080-400);
c.stroke();
F=[];
G=[];
[-1,1].map((x,j)=>{
[-1,1].map((y,i)=>{
$=j?W:B;
m=$.m[i];
c.fillStyle = c.strokeStyle = $.c;
c.fillText($.s,!j*1500+200,80);
c.beginPath();
c.ellipse($.x,$.y,40,40,0,-P,P); // head
c.ellipse($.x+40*x,$.y,10,10,0,-P,P); // nose
F.push({p:x,l:$.x+40*x,t:$.y,r:10+$.x+40*x,b:10+$.y});
c.fill();
c.beginPath();
c.moveTo($.x,$.y-m[0]*y);
c.quadraticCurveTo($.x-m[1]*x,$.y-m[3]*y, $.x-m[2]*x,$.y-m[3]*y); // backward arc
c.quadraticCurveTo($.x+m[4]*x,$.y-m[3]*y, $.x+m[4]*x,$.y-m[3]*y); // forward reach
c.stroke();
c.beginPath();
c.ellipse($.x+(m[4]*x),$.y-m[3]*y,30,30,0,-P,P);
G.push({p:x,l:$.x+(m[4]*x),t:$.y-m[3]*y,r:30+$.x+(m[4]*x),b:30+$.y-m[3]*y});
c.fill();
if ($.n.m == i)
if ( $.n.f++ < 8) for (p in $.n.d) m[p] += $.n.d[p]*$.n.s;
else
if ($.n.s>0)
$.n.s = -1,
$.n.f = 0;
else
$.n = 0;
// n.m = arm
// n.f = frame
// n.s = sequence/direction
// n.d = deltas
});
});
if (K[0]) $.x-=4;
if (K[1]) $.y-=4;
if (K[2]) $.x+=4;
if (K[3]) $.y+=4;
if (K[51] && !$.n.d) $.n = {f:0,s:1,m:0,d:[-1,-6,-18,-3,18]};
if (K[53] && !$.n.d) $.n = {f:0,s:1,m:1,d:[-1,-6,-18,-3,18]};
/* diagnostic */
// hit boxes
/*c.beginPath();
c.save()
c.strokeStyle = '#f00';
c.lineWidth = 1;
F.forEach(v=>{
c.rect(v.l,v.t,v.r-v.l,v.b-v.t);
});
G.forEach(v=>{
c.rect(v.l,v.t,v.r-v.l,v.b-v.t);
});
c.stroke();
c.restore();
*/
F.map(x=>{
G.map(y=>{
if (!(y.l > x.r || y.r < x.l || y.t > x.b || y.b < x.t))
((y.p<0)?B:W).s++,
((y.p>0)?B:W).x+=80*y.p;
});
});
if (Math.abs(B.x-$.x)>200) B.x-=2*Math.sign(B.x-$.x+200);
if (Math.abs(B.y-$.y)>80) B.y-=2*Math.sign(B.y-$.y+80);
if (Math.random()<.1&&!B.n.d) B.n = {f:0,s:1,m:($.y-B.y<0),d:[-1,-6,-18,-3,18]};
$.x=$.x<250?250:$.x>B.x-200?B.x-200:$.x;
$.y=$.y<242?242:$.y>638?638:$.y;
B.x=B.x<$.x+200?$.x+200:B.x>1670?1670:B.x;
}, 16);