所在的位置: js >> js介绍 >> 30行js写的贪吃蛇游戏

30行js写的贪吃蛇游戏

20行精简版

varsn=[42,41],dz=43,fx=1,n,ctx=document.getElementById("can").getContext("2d");functiondraw(t,c){ctx.fillStyle=c;ctx.fillRect(t%20*20+1,~~(t/20)*20+1,18,18);}document.onkeydown=function(e){fx=sn[1]-sn[0]==(n=[-1,-20,1,20][(e

event).keyCode-37]

fx)?fx:n};!function(){sn.unshift(n=sn[0]+fx);if(sn.indexOf(n,1)0

n0

n

fx==1n%20==0

fx==-1n%20==19){returnalert("GAMEOVER");refesh();}draw(n,"Lime");if(n==dz){while(sn.indexOf(dz=~~(Math.random()*))=0);draw(dz,"Yellow");}else{draw(sn.pop(),"Black");}setTimeout(arguments.callee,);}();functionrefesh(){location.reload();}

诠释完好版本

!DOCTYPEhtmlhtmlheadmeta


转载请注明:http://www.aierlanlan.com/rzfs/1078.html