这几天被这东西搞脑子都成一团浆糊了~
- JScript code
<html> <head> <title></title> </head> <body> <script type="text/javascript"> function box(height,width,cellSize){ this.height=height; this.width=width; this.cellSize=cellSize; this.createTable(); this.createStyle(); this.map=[ [ [[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]], [[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]] ], [ [[1,1],[1,1]] ], [ [[0,0,0],[0,1,0],[1,1,1]], [[1,0,0],[1,1,0],[1,0,0]], [[0,0,0],[1,1,1],[0,1,0]], [[1,0,0],[1,1,0],[1,0,0]] ], [ [[1,0,0],[1,0,0],[1,1,0]], [[0,0,0],[1,1,1],[1,0,0]], [[1,1,0],[1,0,0],[1,0,0]], [[0,0,0],[0,1,0],[1,1,1]] ], [ [[0,0,1],[0,0,1],[0,1,1]], [[0,0,0],[1,0,0],[1,1,1]], [[0,1,1],[0,1,0],[0,1,0]], [[0,0,0],[1,1,1],[1,0,0]] ], [ [[0,0,0],[1,1,0],[0,1,1]], [[0,1,0],[1,1,0],[1,0,0]] ], [ [[0,0,0],[0,1,1],[1,1,0]], [[0,1,0],[0,1,1],[0,0,1]] ] ]; this.init(); } box.prototype={ $:function(ID){return document.getElementById(ID);}, createTable:function(){ var table=document.createElement("table"); table.border=0; var tbody=document.createElement("tbody"); tbody.id="body"; table.appendChild(tbody); var i,j; for(i=0;i<this.height;i++){ tbody.insertRow(i); for(j=0;j<this.width;j++){ tbody.rows[i].insertCell(j); tbody.rows[i].cells[j].className="space"; tbody.rows[i].cells[j].width=this.cellSize; tbody.rows[i].cells[j].height=this.cellSize; } } document.body.appendChild(table); }, createStyle:function(){ var style=document.createElement("style"); style.type="text/css"; var css=".space{background:#999999;}"+ ".now{background:#990000;}"+ ".ok{background:#ff0000}"; try{ style.styleSheet.cssText=css; } catch(ei){ style.appendChild(document.createTextNode(css)); } document.getElementsByTagName("head")[0].appendChild(style); }, getBox:function(){ var mapFirst=Math.floor(Math.random()*this.map.length); var mapSecond=Math.floor(Math.random()*this.map[mapFirst].length); return this.map[mapFirst][mapSecond]; }, boxStart:function(minBox,row,cell){ var i,j; for(i=0;i<minBox.length;i++){ for(j=0;j<minBox[i].length;j++){ if(minBox[i][j]&&this.$("body").rows[row+i]){ this.$("body").rows[row+i].cells[cell+j].className="now"; } } } }, clearSpace:function(minBox){ for(var x=minBox.length-1;x>=0;x--){ if(Number(minBox[x].join(""))){ break; } } return x; }, clearNow:function(td){ for(var i=0;i<td.length;i++){ td[i].className=td[i].className==="now"?"space":td[i].className; } }, clearOk:function(td){ for(var i=0;i<td.length;i++){ td[i].className=td[i].className==="now"?"ok":td[i].className; } }, testBorder:function(minBox,x,y,lr){ var k,l; for(k=0;k<minBox.length;k++){ for(l=0;l<minBox[k].length;l++){ if(this.$("body").rows[x+k]){ if(this.$("body").rows[x+k].cells[y+l].className==="now"&&this.$("body").rows[x+k+1].cells[y+l].className==="ok"&&lr==="bottom"){ if(x<=0){ return "end"; } return false; } } if(this.$("body").rows[x+k]&&this.$("body").rows[x+k].cells[y+l-1]&&lr==="left"){ if(this.$("body").rows[x+k].cells[y+l].className==="now"){ this.$("body").rows[x+k].cells[y+l].className="space"; this.$("body").rows[x+k].cells[y+l-1].className="now"; } if(this.$("body").rows[x+k].cells[y+l].className==="now"&&this.$("body").rows[x+k].cells[y+l-1].className==="ok"){ return false; } } if(this.$("body").rows[x+k]&&this.$("body").rows[x+k].cells[y+l+1]&&lr==="right"){ if(this.$("body").rows[x+k].cells[y+l].className==="now"){ this.$("body").rows[x+k].cells[y+l].className="space"; this.$("body").rows[x+k].cells[y+l+1].className="now"; } if(this.$("body").rows[x+k].cells[y+l].className==="now"&&this.$("body").rows[x+k].cells[y+l+1].className==="ok"){ return false; } } } } return "continue"; }, boxControl:function(self,minBox,x,y,td,time,speed){ var fn=arguments.callee; if(x<=self.height-self.clearSpace(minBox)-1&&self.testBorder(minBox,x,y,"bottom")) { if(self.testBorder(minBox,x,y,"bottom")==="end"){ alert("Game Over"); clearTimeout(time); time=null; return; } else { self.clearNow(td); self.boxStart(minBox,x,y); x++; document.onkeydown=function(){ if(window.event.keyCode===37){ if(y>0&&self.testBorder(minBox,x,y,"left")){ y--; } } else if(window.event.keyCode===39){ if(y<self.width-minBox.length&&self.testBorder(minBox,x,y,"right")){ y++; } } else if(window.event.keyCode==40){ clearTimeout(time); time=null; speed=50; fn(self,minBox,x,y,td,time,speed); } }; document.onkeyup=function(){ if(window.event.keyCode===40){ speed="500"; } }; time=setTimeout(function(){fn(self,minBox,x,y,td,time,speed);},speed); } } else { self.clearOk(td); self.init(); } }, init:function(){ var minBox=this.getBox(); var x=-this.clearSpace(minBox); var y=Math.floor((this.width-minBox[0].length)/2); var td=this.$("body").getElementsByTagName("td"); var self=this; var time=null,speed=500; this.boxControl(self,minBox,x,y,td,time,speed); } } new box(15,10,24); </script> </body> </html>