您的位置:首页 > Web前端 > JavaScript

纯js实现俄罗斯方块[效率待优化]

2009-12-28 09:28 561 查看
var mapObject=null;
var baseShapObject=null;
var timeInterval=null;
$(document).ready(function()
{
 mapObject=new MapClass();
 mapObject.createMap(); 
 
 createTable(mapObject);
 
 baseShapObject=new BaseShapClass();
 baseShapObject.createShap();
 
 timeInterval=window.setInterval("doingClass()",150);
 
 $(document).bind("keydown",function(event)
 {
  if(event.keyCode==38)      //up
  {
   baseShapObject.changeShap();
  } 
  else if(event.keyCode==37)     //left
  {
   baseShapObject.moveLeft(mapObject);
  }
  else if(event.keyCode==39)     //right
  {
   baseShapObject.moveRight(mapObject);
  }
  else if(event.keyCode==32)     //space
  {
   baseShapObject.moveAtOnceDown(mapObject);
  }
 });
 
});

 

function doingClass()
{
 if(baseShapObject.isFail(mapObject))
 {
  window.clearInterval(timeInterval);
  timeInterval=null;
  alert("失败了!");
 }
 if(baseShapObject.isMoveToNextLine(mapObject))
 {
  baseShapObject.moveDown();
 }
 else
 {
  mapObject.addShapToMap(baseShapObject);
  mapObject.cleanLine();
  baseShapObject.createShap();
 }

 mapObject.displayShapInMap(baseShapObject);
}

 

function BaseShapClass()
{
 this.top=0;
 this.left=3;
 this.width=4;
 this.height=4;
 this.positionArray=new Array();
 this.baseOneShap=null;
 
 this.getTop=function ()
 {
  return this.top;
 }
 
 this.getLeft=function ()
 {
  return this.left;
 }
 
 this.getWidth=function ()
 {
  return this.width;
 }
 
 this.getHeight=function ()
 {
  return this.height;
 }
 
 this.createShap=function ()
 {
  var shapType=Math.floor(Math.random()*5)+1;
  
  switch(shapType)
  {
   case 1:
    this.baseOneShap=new ShapOne();
   case 2:
    this.baseOneShap=new ShapTwo();
   case 3:
    this.baseOneShap=new ShapThree();
   case 4:
    this.baseOneShap=new ShapFour();
   case 5:
    this.baseOneShap=new ShapFive();                
  }
  this.positionArray=this.baseOneShap.getShap();
  this.top=0;
  this.left=3;
 }
 
 this.changeShap=function ()
 {
  this.positionArray=this.baseOneShap.getNextPositionShap();
 }
 
 this.moveLeft=function (map)
 {
  if(this.left<=0)
  {
   var k=0;
   for(var i=0;i<this.getHeight();i++)
   {
    if(this.getPosition(i,0-this.left)==1)
    {
     break;
    }
    else
    {
     k++;
    }
   }
   if(k==this.getHeight())
   {
    this.left --;
   }
  }
  else
  {
   this.left --;
  }
  
 }

 this.moveRight=function (map)
 {
  if((this.left+this.width)>=map.getWidth())
  {
   var k=0;
   for(var i=0;i<this.getHeight();i++)
   {
    if(this.getPosition(i,map.getWidth()-this.left-1)==1)
    {
     break;
    }
    else
    {
     k++;
    }
   }
   if(k==this.getHeight())
   {
    this.left ++;
   }
  }
  else
  {
   this.left ++;
  }
 }
 
 this.moveDown=function ()
 {
  this.top ++;
 }
 
 this.moveAtOnceDown=function (map)
 {
  for(var i=0;i<map.getHeight();i++)
  {
   if(this.isMoveToNextLine(map))
   {
    this.top ++;
   }
   else
   {
    break;
   }
  }
 }
 
 this.isMoveToNextLine=function (map)
 {
  if(this.getTop()+1>map.getHeight())
  {
   return false;
  }
  for(var i=0;i<this.getWidth();i++)
  {
   if(this.getPosition(this.getHeight()-1,i)==1 && map.getAPosition(this.getTop(),this.getLeft()+i)==1)
   {
    return false;
   }
  }
  return true;
 }
 
 this.isFail=function (map)
 {
  for(var i=0;i<map.getWidth();i++)
  {
   if(map.getAPosition(0,i)==1)
   {
    return true;
   }
  }
  return false;
 } 
 
 this.showShap=function ()
 {
  var returnStr="";
  for(var i=0;i<this.height;i++)
  {
   for(var j=0;j<this.width;j++)
   {
    returnStr +=this.positionArray[i][j]+",";
   }
   returnStr +="/n";
  }
  return returnStr;
 }
 
 this.getPosition=function (x,y)
 {
  return this.positionArray[x][y];
 }
}

function MapClass()
{
 this.mapArray=new Array();
 this.height=30;
 this.width=15;
 this.score=0;
 this.scorePreLine=100;
 
 this.getAPosition=function (x,y)
 {
  return this.mapArray[x][y];
 }
 
 this.createMap=function()
 {
  for(var i=0;i<this.height;i++)
  {
   var tempArray=new Array();
   for(var j=0;j<this.width;j++)
   {
    tempArray.push(0);
   }
   this.mapArray.push(tempArray);
  }
 }
 
 this.showMap=function ()
 {
  var returnStr="";
  for(var i=0;i<this.height;i++)
  {
   for(var j=0;j<this.width;j++)
   {
    returnStr +=this.mapArray[i][j]+",";
   }
   returnStr +="/n";
  }
  return returnStr;
 } 
 
 this.cleanLine=function ()
 {
  for(var i=this.height-1;i>=0;i--)
  {
   var fullSum=0;
   for(var j=0;j<this.width;j++)
   {
    if(this.mapArray[i][j]==0)
    {
     break;
    }
    else
    {
     fullSum ++;
    }
   }
   
   if(fullSum==this.width)
   {
    for(var m=i;m>=0;m--)
    {
     if(m==0)
     {
      for(var n=0;n<this.width;n++)
      {
       this.mapArray[m]
=0;
      }
     }
     else
     {
      for(var n=0;n<this.width;n++)
      {
       this.mapArray[i][j]=this.mapArray[i-1][j];
      }
     }

    }
    this.score +=this.scorePreLine;
   }
  }
 }
 
 this.addShapToMap=function (shap)
 {
  var shapLeft=shap.getLeft();
  var shapTop=shap.getTop(); 
  for(var i=0;i<shap.getHeight();i++)
  {
   for(var j=0;j<shap.getWidth();j++)
   {
    if(shap.getPosition(i,j)==1)
    {
     this.mapArray[shapTop-shap.getHeight()+i][shapLeft+j]=1;
    }
   }
  }
 }

 this.displayShapInMap=function (shap)
 {
  var tempArray=new Array();
  for(var i=0;i<this.mapArray.length;i++)
  {
   var innerArray=new Array();
   for(var j=0;j<this.mapArray[i].length;j++)
   {
    innerArray.push(this.mapArray[i][j]);
   }
   tempArray.push(innerArray);
  }
  
  
  var shapLeft=shap.getLeft();
  var shapTop=shap.getTop(); 
  for(var i=0;i<shap.getHeight();i++)
  {
   for(var j=0;j<shap.getWidth();j++)
   {
    if(shap.getPosition(i,j)==1)
    {
     var x=shapTop-shap.getHeight()+i;
     var y=shapLeft+j;
     if(x>=0 && y>=0)
     {
      tempArray[x][y]=1;
     }
    }
   }
  }
  
  for(var i=0;i<tempArray.length;i++)
  {
   for(var j=0;j<tempArray[i].length;j++)
   {
    if(tempArray[i][j]==1)
    {
     $("#map"+(i*100+j)).attr("src","down.gif");
    }
    else
    {
     $("#map"+(i*100+j)).attr("src","up.gif");
    }
   }
  }
  $("#score").html(this.score);
  
  tempArray=null;
 }

 this.getHeight=function ()
 {
  return this.height;
 } 
 
 this.getWidth=function ()
 {
  return this.width;
 }
 
}

function createTable(map)
{
 var width=map.getWidth();
 var height=map.getHeight();
 var writeStr='<table style="border:solid 1px black;">';
 writeStr +='<th><td colspan="'+map.getWidth()+'">得分:<span id="score" style="color:red">--</span></td></th>';
 for(var i=0;i<height;i++)
 {
  writeStr +='<tr>';
  for(var j=0;j<width;j++)
  {
   writeStr +='<td>';
   
   writeStr +='<img src="up.gif" id="map'+(i*100+j)+'">';
   
   writeStr +='</td>';
  }
  writeStr +='</tr>';
 }
 writeStr +='</table>';
 
 
 $("body").append($(writeStr));
}

function ShapOne()
{
 this.pisitionArrayArray=new Array(
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(1,1,1,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(0,1,1,0),
   new Array(0,1,0,0)  
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(1,1,1,0),
   new Array(0,1,0,0)  
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(1,1,0,0),
   new Array(0,1,0,0) 
  )
 
 );
 this.step=0;
 
 this.getShap=function ()
 {
  this.step=Math.floor(Math.random()*5);
  return this.pisitionArrayArray[this.step];
 }
 this.getNextPositionShap=function ()
 {
  this.step ++;
  if(this.step >=4)
  {
   this.step=0;
  }
  return this.pisitionArrayArray[this.step];
 }
 this.getStep=function ()
 {
  return this.step;
 }
 
}

function ShapTwo()
{
 this.pisitionArrayArray=new Array(
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(1,1,1,1)
  ),
  new Array(
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0)  
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(1,1,1,1) 
  ),
  new Array(
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0) 
  )
 
 );
 this.step=0;
 
 this.getShap=function ()
 {
  this.step=Math.floor(Math.random()*5);
  return this.pisitionArrayArray[this.step];
 }
 this.getNextPositionShap=function ()
 {
  this.step ++;
  if(this.step >=4)
  {
   this.step=0;
  }
  return this.pisitionArrayArray[this.step];
 }
 this.getStep=function ()
 {
  return this.step;
 }
 
}

function ShapThree()
{
 this.pisitionArrayArray=new Array(
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,1,1,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,1,1,0) 
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,1,1,0) 
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,1,1,0)
  )
 
 );
 this.step=0;
 
 this.getShap=function ()
 {
  this.step=Math.floor(Math.random()*5);
  return this.pisitionArrayArray[this.step];
 }
 this.getNextPositionShap=function ()
 {
  this.step ++;
  if(this.step >=4)
  {
   this.step=0;
  }
  return this.pisitionArrayArray[this.step];
 }
 this.getStep=function ()
 {
  return this.step;
 }
 
}

function ShapFour()
{
 this.pisitionArrayArray=new Array(
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(0,1,1,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,1,1),
   new Array(0,1,0,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,0,1,0),
   new Array(0,0,1,0) 
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,0,1,0),
   new Array(1,1,1,0)
  )
 
 );
 this.step=0;
 
 this.getShap=function ()
 {
  this.step=Math.floor(Math.random()*5);
  return this.pisitionArrayArray[this.step];
 }
 this.getNextPositionShap=function ()
 {
  this.step ++;
  if(this.step >=4)
  {
   this.step=0;
  }
  return this.pisitionArrayArray[this.step];
 }
 this.getStep=function ()
 {
  return this.step;
 }
 
}

function ShapFive()
{
 this.pisitionArrayArray=new Array(
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0),
   new Array(1,1,0,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(0,1,0,0),
   new Array(0,1,1,1)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,1,1,0),
   new Array(0,1,0,0),
   new Array(0,1,0,0)
  ),
  new Array(
   new Array(0,0,0,0),
   new Array(0,0,0,0),
   new Array(1,1,1,0),
   new Array(0,0,1,0)
  )
 
 );
 this.step=0;
 
 this.getShap=function ()
 {
  this.step=Math.floor(Math.random()*5);
  return this.pisitionArrayArray[this.step];
 }
 this.getNextPositionShap=function ()
 {
  this.step ++;
  if(this.step >=4)
  {
   this.step=0;
  }
  return this.pisitionArrayArray[this.step];
 }
 this.getStep=function ()
 {
  return this.step;
 }
 
}

 

 

 

 

 

 

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息