var a=document,b=a.body.children.c,c=b.getContext("2d"),d=Math,e=[],f,g,h=+new Date,i=A=X=Z=0,j,k,m,n,o,p,r;b.height=(b.width=600)/2;function s(l,q,u,v,w){c.fillStyle=l;c.fillRect(q,u,v,w)}function t(l){return function(q){e[q.keyCode]=l}}a.onkeydown=t(1);a.onkeyup=t(0);setInterval(function(){s("#acf",0,0,600,j=150);s("#a63",0,j,600,j);g=+new Date;A=5-i/50;e[38]?i+=A:i=i>2?i-2:0;e[40]&&(i-=9);f=(g-h)/36E5;Z+=i*f;j=3*f*i/200;X+=e[37]?-j:e[39]?j:0;h=g;for(j=1;j<70;j++)(9*d.sin((90-j)/57.3)/d.sin(j/57.3)+Z*1E3)%18>9&&s("#b73",0,150*(1+j/70),600,15/7);k=d.abs(X*1E5%700)-100;X>0&&(k=500-k);s("#bdf",k,100,100,600/17);s("#bdf",k+600/19,600/7,40,600/17);var l=e[37]?-1:e[39]?1:0;for(j=0;j<20;j++){a=2;k=300+(l/2-1)*30-j*l*a;n=60;d.abs(j-9.5)>8&&(n=600/11,k+=30/11);m=200+j*a;p=150/7;s("#a63",k,m+p*1.4,n,p/4);r=p*(j/19+2)/3*(1.5+d.sin(j/19*d.PI*2-d.PI/2)/2.5);s("red",k,m-r+p,n,r);o=n*0.9;j>12&&j<17&&s("#600",k+(n-o)/2,m-r+p,o,r);r=n/4;j%10>1&&j%10<6&&(s("#000",k,m+p*0.8,r,r),s("#000",k+n-r,m+p*0.8,r,r))}},50);
//Author: Dmitry Scriptin ( twitter: @scriptin )
//Simple car simulator for JS1K contest
//vars and shortcuts
var z=document,
c=z.body.children.c,
$=c.getContext('2d'),
m=Math,
k=[],//keys
s=600,//size - everything is relative to that!
dt,t1,t=+new Date(),//delta time, new time, old time
cs=A=X=Z=0,//speed,acceleration,x-coord('rotation'),z-coord(distance)
i,x,y,w,w1,h,h1,n=70,r=57.3;//functions' vars - just to save bytes
c.height=(c.width=s)/2;
function F(s,x,y,w,h){//main drawing procedure
$.fillStyle=s;
$.fillRect(x,y,w,h);
}
function K(s){//key events generator
return function(e){
k[e.keyCode]=s;
};
}
z.onkeydown=K(1);
z.onkeyup=K(0);
function C(d){//car
for(i=0;i<20;i++){//20 layers of isometric car
z=s/300;//shortcut
x=s/2+(d/2-1)*s/20-i*d*z;//x coord
w=s/10;//width
m.abs(i-9.5)>8&&(w=s/11,x+=s/220);//narrow nose&tail
y=s/3+i*z;//y coord
h=s/28;//height
F('#a63',x,y+h*1.4,w,h/4);//shadow
//sin() with offset and scaling to make rooftop
//linear growth of height from nose to tail
h1=h*(i/19+2)/3*(1.5+m.sin(i/19*m.PI*2-m.PI/2)/2.5);
F('red',x,y-h1+h,w,h1);//chassis
w1=w*0.9;
i>12&&i<17&&F('#600',x+(w-w1)/2,y-h1+h,w1,h1);//glass
h1=w/4;
i%10>1&&i%10<6&&//layers 2-5,12-15 for wheels
(F('#000',x,y+h*0.8,h1,h1),F('#000',x+w-h1,y+h*0.8,h1,h1));//wheels
}
}
setInterval(function(){
//background
F('#acf',0,0,s,i=s/4);
F('#a63',0,i,s,i);
//movement calculations
t1=+new Date();
A=5-cs/50;
k[38]?cs+=A:cs=cs>2?cs-2:0;
k[40]?cs-=9:0;
dt=(t1-t)/36e5;
Z+=cs*dt;
i=3*dt*cs/200;
X+=k[37]?-i:k[39]?i:0;
t=t1;
//stripes on the ground
for(i=1;i<n;i++)
(9*m.sin((90-i)/r)/m.sin(i/r)+Z*1e3)%18>9&&
F('#b73',0,s/4*(1+i/n),s,s/4/n);
//a 'cloud' =)
x=m.abs((X*1e5*s/600)%(s+s/6))-s/6;
X>0&&(x=s-x-s/6);
F('#bdf',x,s/6,s/6,s/17);
F('#bdf',x+s/19,s/7,s/15,s/17);
C(k[37]?-1:k[39]?1:0);//drawing car
//direction is argument: -1 left, 0 forward, 1 right
},50); //20fps