var _x=this.x+1;if(this.canMove(_x,this.y)) this.x=_x;this.putNewBox();drawGrid();}this.moveDown=function(){this.clearOldBox();var _y=this.y+1;if(this.canMove(this.x,_y)){this.y=_y;this.putNewBox();drawGrid();}else{this.putNewBox();drawGrid();checkLineFull();return;}}this.rotate=function(){var tmp=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]; for(j=0;j<=this.h;j++)for(i=0;i<=this.w;i++)tmp[this.w-i][j]=this.arr[j][i];var newBox=new Box(this.x,this.y,tmp,this.color);this.clearOldBox();if(! newBox.canMove(this.x,this.y)) this.putNewBox();else{box=newBox;box.putNewBox();drawGrid();}}this.canMove=function(x,y){for(var j=0;j<=this.h;j++)for(var i=0;i<=this.w;i++){if(grid[y+j][x+i]!=0 && this.arr[j][i]!=0){ return false; }}return true;}}function drawGrid(){for(var j=0;j<=15;j++)for(var i=0;i<=10;i++){if( grid[j][i]!=gridBuf[j][i]){paintCell(j,i,grid[j][i]);}gridBuf[j][i]=grid[j][i];}}function paintCell(i,j,color){var htmlGrid=document.getElementById("TetrisGrid").firstChild;htmlGrid.childNodes[i].childNodes[j].style.backgroundColor=colors[color];}function init(){var html='';for(var i=0;i<=15;i++){html+='';for(var j=0;j<=10;j++){html+='';}html+=' \r\n';}html+='
';document.write(html);}function checkLineFull(){var full,i,j,i2;var y3=box.y+box.h;var y4=box.y;for(i=y3;i>=y4;){ full=1; for(j=1;j<10;j++)if(grid[i][j]==0){full=0; break;} if(full==0){ --i; continue;} for(i2=i; i2>0;i2--)for(j=1;j<10;j++)grid[i2][j]=grid[i2-1][j]; drawGrid(); y4++; gotLine++;}checkGameOver();}function checkGameOver(){var bOver=false;for(var j=1;j<10;j++)if(grid[1][j]>0){ bOver=true; break;}if(!bOver){box=new Box(1,1,boxdata[Math.floor(Math.random()*7)],Math.floor(Math.random()*5)+1);box.putNewBox();}else{bGameOver=true;msg.innerHTML="游戏结束! 您的得分为"+gotLine*100;window.clearInterval();}}function document_onkeydown(){if(bGameOver) return;switch(event.keyCode){case 37: box.moveLeft();break;case 39:box.moveRight();break;case 38:box.rotate();break;case 40:box.moveDown();break;}}function moveDownBox(){var interval=1000-10*(gotLine>80?80 :gotLine);msg.innerHTML="等级:"+Math.floor(gotLine/10)+" 得分:"+gotLine*100;box.moveDown();window.setTimeout('moveDownBox()',interval);}function startGame(){init();window.setTimeout('moveDownBox()',1000);bGameOver=false;box=new Box(1,1,boxdata[Math.floor(Math.random()*7)],Math.floor(Math.random()*5)+1);box.putNewBox();drawGrid();}
<!-- if (document.all)document_onkeydown()//-->
sunnisdu 山东大学
开始送给娟的俄罗斯方块
startGame();