首页 > 语言 > 关键词  > Javascript最新资讯  > 正文

只要60行代码:搞定Javascript版俄罗斯方块

2015-05-07 14:03 · 稿源: 51CTO

游戏结束图:

javascript实现源码:

  1. <!doctypehtml>
  2. <html><head><title>俄罗斯方块</title>
  3. <metaname="Description"content="俄罗斯方块Javascript实现">
  4. <metaname="Keywords"content="俄罗斯方块,Javascript,实现,短小精悍,游戏,ithomer,ithomer.net">
  5. </head><body>
  6. <divid="box"style="margin:20pxauto;text-align:center;width:252px;font:25px/25px宋体;background:#000;color:#9f9;border:#99920pxridge;text-shadow:2px3px1px#0f0;"></div>
  7. <divid="footer"style="margin-top:200pxauto;text-align:center;font-size:16px;">©2009-2014AllRightsby<ahref="https://blog.ithomer.net">ithomer.net</a></div>
  8. <script>
  9. vardomain="www.zuidaima.com";
  10. varauthor="zuidaima";
  11. varmap=eval("["+Array(23).join("0x801,")+"0xfff]");
  12. vartatris=[[0x6600],[0x2222,0xf00],[0xc600,0x2640],[0x6c00,0x4620],[0x4460,0x2e0,0x6220,0x740],[0x2260,0xe20,0x6440,0x4700],[0x2620,0x720,0x2320,0x2700]];
  13. varkeycom={"38":"rotate(1)","40":"down()","37":"move(2,1)","39":"move(0.5,-1)"};
  14. vardia,pos,bak,run;
  15. functionstart(){
  16. dia=tatris[~~(Math.random()*7)];
  17. bak=pos={fk:[],y:0,x:4,s:~~(Math.random()*4)};
  18. rotate(0);
  19. }
  20. functionover(){
  21. document.onkeydown=null;
  22. clearInterval(run);
  23. alert("GAMEOVER");
  24. }
  25. functionupdate(t){
  26. bak={fk:pos.fk.slice(0),y:pos.y,x:pos.x,s:pos.s};
  27. if(t)return;
  28. for(vari=0,a2="";i<22;i++)
  29. a2+=map[i].toString(2).slice(1,-1)+"<br/>";
  30. for(vari=0,n;i<4;i++)
  31. if(/([^0]+)/.test(bak.fk[i].toString(2).replace(/1/g,"\u25a1")))
  32. a2=a2.substr(0,n=(bak.y+i+1)*15-RegExp.$_.length-4)+RegExp.$1+a2.slice(n+RegExp.$1.length);
  33. document.getElementById("box").innerHTML=a2.replace(/1/g,"\u25a0").replace(/0/g,"\u3000");
  34. }
  35. functionis(){
  36. for(vari=0;i<4;i++)
  37. if((pos.fk[i]&map[pos.y+i])!=0)returnpos=bak;
  38. }
  39. functionrotate(r){
  40. varf=dia[pos.s=(pos.s+r)%dia.length];
  41. for(vari=0;i<4;i++)
  42. pos.fk[i]=(f>>(12-i*4)&15)<<pos.x;
  43. update(is());
  44. }
  45. functiondown(){
  46. ++pos.y;
  47. if(is()){
  48. for(vari=0;i<4&&pos.y+i<22;i++)
  49. if((map[pos.y+i]|=pos.fk[i])==0xfff)
  50. map.splice(pos.y+i,1),map.unshift(0x801);
  51. if(map[1]!=0x801)returnover();
  52. start();
  53. }
  54. update();
  55. }
  56. functionmove(t,k){
  57. pos.x+=k;
  58. for(vari=0;i<4;i++)
  59. pos.fk[i]*=t;
  60. update(is());
  61. }
  62. document.onkeydown=function(e){
  63. eval(keycom[(e?e:event).keyCode]);
  64. };
  65. start();
  66. run=setInterval("down()",400);
  67. </script></body></html>
  • 相关推荐
  • 大家在看

这篇文章对你有价值吗?

  • 热门标签

热文

  • 3 天
  • 7天