60行js代码实现俄罗斯方块
这是我之前网上看到的,很牛逼的一位大神写的,一直膜拜中
<!doctypehtml><html><head></head><body> <divid="box"style="width:252px;font:25px/25px宋体;background:#000;color:#9f9;border:#99920pxridge;text-shadow:2px3px1px#0f0;"></div> <script> varmap=eval("["+Array(23).join("0x801,")+"0xfff]"); vartatris=[[0x6600],[0x2222,0xf00],[0xc600,0x2640],[0x6c00,0x4620],[0x4460,0x2e0,0x6220,0x740],[0x2260,0xe20,0x6440,0x4700],[0x2620,0x720,0x2320,0x2700]]; varkeycom={"38":"rotate(1)","40":"down()","37":"move(2,1)","39":"move(0.5,-1)"}; vardia,pos,bak,run; functionstart(){ dia=tatris[~~(Math.random()*7)]; bak=pos={fk:[],y:0,x:4,s:~~(Math.random()*4)}; rotate(0); } functionover(){ document.onkeydown=null; clearInterval(run); alert("GAMEOVER"); } functionupdate(t){ bak={fk:pos.fk.slice(0),y:pos.y,x:pos.x,s:pos.s}; if(t)return; for(vari=0,a2="";i<22;i++) a2+=map[i].toString(2).slice(1,-1)+"<br/>"; for(vari=0,n;i<4;i++) if(/([^0]+)/.test(bak.fk[i].toString(2).replace(/1/g,"\u25a1"))) a2=a2.substr(0,n=(bak.y+i+1)*15-RegExp.$_.length-4)+RegExp.$1+a2.slice(n+RegExp.$1.length); document.getElementById("box").innerHTML=a2.replace(/1/g,"\u25a0").replace(/0/g,"\u3000"); } functionis(){ for(vari=0;i<4;i++) if((pos.fk[i]&map[pos.y+i])!=0)returnpos=bak; } functionrotate(r){ varf=dia[pos.s=(pos.s+r)%dia.length]; for(vari=0;i<4;i++) pos.fk[i]=(f>>(12-i*4)&15)<<pos.x; update(is()); } functiondown(){ ++pos.y; if(is()){ for(vari=0;i<4&&pos.y+i<22;i++) if((map[pos.y+i]|=pos.fk[i])==0xfff) map.splice(pos.y+i,1),map.unshift(0x801); if(map[1]!=0x801)returnover(); start(); } update(); } functionmove(t,k){ pos.x+=k; for(vari=0;i<4;i++) pos.fk[i]*=t; update(is()); } document.onkeydown=function(e){ eval(keycom[(e?e:event).keyCode]); }; start(); run=setInterval("down()",400); </script></body></html>
以上所述就是本文的全部内容,希望大家能够喜欢。