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

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][n]=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;
 }
 
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值