• 60行JavaScript俄罗斯方块


     1 <!doctype html><html><head></head><body>  
     2 <div id="box" style="252px;font:25px/25px 宋体;background:#000;color:#9f9;border:#999 20px ridge;text-shadow:2px 3px 1px #0f0;"></div>  
     3 <script>  
     4 var map=eval("["+Array(23).join("0x801,")+"0xfff]");  
     5 var tatris=[[0x6600],[0x2222,0xf00],[0xc600,0x2640],[0x6c00,0x4620],[0x4460,0x2e0,0x6220,0x740],[0x2260,0xe20,0x6440,0x4700],[0x2620,0x720,0x2320,0x2700]];  
     6 var keycom={"38":"rotate(1)","40":"down()","37":"move(2,1)","39":"move(0.5,-1)"};  
     7 var dia, pos, bak, run;  
     8 function start(){  
     9     dia=tatris[~~(Math.random()*7)];  
    10     bak=pos={fk:[],y:0,x:4,s:~~(Math.random()*4)};  
    11     rotate(0);  
    12 }  
    13 function over(){  
    14     document.onkeydown=null;  
    15     clearInterval(run);  
    16     alert("GAME OVER");  
    17 }  
    18 function update(t){  
    19     bak={fk:pos.fk.slice(0),y:pos.y,x:pos.x,s:pos.s};  
    20     if(t) return;  
    21     for(var i=0,a2=""; i<22; i++)  
    22         a2+=map[i].toString(2).slice(1,-1)+"<br/>";  
    23     for(var i=0,n; i<4; i++)  
    24         if(/([^0]+)/.test(bak.fk[i].toString(2).replace(/1/g,"u25a1")))  
    25             a2=a2.substr(0,n=(bak.y+i+1)*15-RegExp.$_.length-4)+RegExp.$1+a2.slice(n+RegExp.$1.length);  
    26     document.getElementById("box").innerHTML=a2.replace(/1/g,"u25a0").replace(/0/g,"u3000");  
    27 }  
    28 function is(){  
    29     for(var i=0; i<4; i++)  
    30         if((pos.fk[i]&map[pos.y+i])!=0) return pos=bak;  
    31 }  
    32 function rotate(r){  
    33     var f=dia[pos.s=(pos.s+r)%dia.length];  
    34     for(var i=0; i<4; i++)  
    35         pos.fk[i]=(f>>(12-i*4)&15)<<pos.x;  
    36     update(is());  
    37 }  
    38 function down(){  
    39     ++pos.y;  
    40     if(is()){  
    41         for(var i=0; i<4 && pos.y+i<22; i++)  
    42             if((map[pos.y+i]|=pos.fk[i])==0xfff)  
    43                 map.splice(pos.y+i,1), map.unshift(0x801);  
    44         if(map[1]!=0x801) return over();  
    45         start();  
    46     }  
    47     update();  
    48 }  
    49 function move(t,k){  
    50     pos.x+=k;  
    51     for(var i=0; i<4; i++)  
    52         pos.fk[i]*=t;  
    53     update(is());  
    54 }  
    55 document.onkeydown=function(e){  
    56     eval(keycom[(e?e:event).keyCode]);  
    57 };  
    58 start();  
    59 run=setInterval("down()",400);  
    60 </script></body></html> 
  • 相关阅读:
    Python pip 下载速度慢? Windows 设置 国内源,用阿里云国内镜像加速
    Go timer 是如何被调度的?
    Go sync.Pool 浅析
    一次错误使用 go-cache 导致出现的线上问题
    golang面向对象分析
    一文完全掌握 Go math/rand
    这一次,彻底搞懂 Go Cond
    面试题:让你捉摸不透的 Go reslice
    当 Go struct 遇上 Mutex
    这可能是最容易理解的 Go Mutex 源码剖析
  • 原文地址:https://www.cnblogs.com/andlp/p/5159878.html
Copyright © 2020-2023  润新知