-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSnake
1 lines (1 loc) · 1.57 KB
/
Snake
1
javascript:for(Q=64,/*maze size*/m=b=Q*Q,a=[P=l=u=d=p=S=w=0],/*a=grid,P=playing,l=food-iters left (until tail starts moving),u=food age,d=dir,p=prev-dir,S=score,w=score to be added on,m=the score to be added if food was eaten now*/u=89,f=(h=j=t=(b+Q)/2)-1,/*f=food,h=head,j=prev-head,t=tail. set food to left so gets picked up immediately, setting initial snake size*/(B=(D=document).body).appendChild(x=D.createElement('p')),(X=x.style).position='fixed',X.left=X.top=0,X.background='#FFF',x.innerHTML='%3Cp%3E%3C/p%3E%3Ccanvas%3E',v=(s=x.childNodes)[0],(s=s[1]).width=s.height=Q*5,c=s.getContext('2d'),onkeydown=onblur=F=function(e,z){z?%20/*calc%20food*/a[f]?(w+=m,/*aging%20food%20score%20calc*/f=Math.random(l+=8)*(R=Q-2)*R|(u=0),F(f+=Q+1+(f/R|0)*2,z)/*random%20(re)calc%20food%20loc*/):F(f)/*draw%20food*/:e%3C0?(%20/*iter*/l?--l:(y=t,t=a[t]-2,F(y)),/*set%20tail*/S+=(w*=.8)/4,/*pretty%20score%20up*/m=999/(u+++10),/*age%20food%20a%20bit*/a[h+=[-1,-Q,1,Q][d=p]]?/*collision%20detect*/B.removeChild(x,alert('Game%20Over')):(F(h),/*draw%20head*/F(e,j=h),/*calc%20food*/v.innerHTML=P?(setTimeout(F,50,e,0),S|0):'Press%20P')/*next%20iter*/):-e?(/*draw%20(+recalc%20sqr)*/y=(a[e]=e%3CQ|e%3E=Q*Q-Q|/*tb%20walls*/!(e%Q)|e%Q==Q-1|/*lr%20walls*/(e==h)*2)/*snake%20head*/+(e==f),e==h&&(a[j]=2+h),/*snake%20chain*/c.fillStyle='hsl('+!a[e]*99+','+m*2+'%,'+y*50+'%)',/*color%20calc*/c.fillRect(e%Q*5,(e/Q|0)*5,5,5)):/*keyinput*/isNaN(y=e.keyCode-37)|y==43?(P=y&&!P)&&F(-1)/*pause*/:p=!P|y&-4|!(y^2^d)?/*arrow%20keys&¬%20directly%20opposite*/p:y;return!1};--b;F(b));/*init%20grid*/void%20F(-1)/*dummy%20update*/