一个javascript写的俄罗斯方块

自己写的,随便看看吧,最近在努力研究J2ME,正在自己开发一个类似泡泡堂的手机网络游戏,到时候把源代码发来交流一下.下面这个文档直接粘贴过去就能用.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script>
 alert("欢迎您的使用!  A,S,D分别控制方向,空格控制块块的转换,如有bug请呼QQ 58740339                                                                                        ----北大青鸟Y273盘盘");
</script>
</head>

<body onKeyDown="controlkey()" onLoad="">
<div align="center">
<h1>俄罗斯芳块</h1>
<table width="200" height="300" border="1" cellpadding="0" cellspacing="0" id="gametable">

</table>
<h3 id="nscore">得分: 0<h3>

</div>

</body>
</html>

<script>
var obj = document.getElementById("gametable");

var blockx = 4;
var blocky = 1;
var blockt =new Array();
var blocktype = 10;
var tempblock;
var tscore=0;
var downstatus=0;
var allowmove=0;
var allowtran=0;
var blockname;
var blocktd = new Array();
for (i=0; i<150; i++)
{
 blocktd[0]=0;
}

/****************
function block10()
{
 this.block = new Array();
 this.isLeft = true;
 this.isRight = false;
 this.transLeft = function left;
}
function left(x, y)
{
 
}
********************/
var block10 = new Array(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0);
var block11 = new Array(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0);
var block20 = new Array(0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0);
var block21 = new Array(0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0);
var block30 = new Array(0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0);
var block31 = new Array(0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0);
var block40 = new Array(0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0);
var block50 = new Array(0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0);
var block51 = new Array(0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0);
var block52 = new Array(0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0);
var block53 = new Array(0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0);
var block60 = new Array(0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0);
var block61 = new Array(0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0);
var block62 = new Array(0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0);
var block63 = new Array(0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0);
var block70 = new Array(0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0);
var block71 = new Array(0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0);
var block72 = new Array(0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0);
var block73 = new Array(0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0);
var blockstatus = new Array(10,11,20,21,30,31,50,51,52,53,60,61,62,63,70,71,72,73);

function blockdown()               //向下移动
{
 blocktd[blockt[0]]=0;
 blocktd[blockt[1]]=0;
 blocktd[blockt[2]]=0;
 blocktd[blockt[3]]=0;
 if(blocktd[blockt[0]+10]==1 || blocktd[blockt[1]+10]==1 || blocktd[blockt[2]+10]==1 || blocktd[blockt[3]+10]==1 ||blockt[0]>139  ||blockt[1]>139 ||blockt[2]>139 ||blockt[3]>139)
 {
  blocktd[blockt[0]]=1;
  blocktd[blockt[1]]=1;
  blocktd[blockt[2]]=1;
  blocktd[blockt[3]]=1; 
  addblock();
 }
 else  
 {
  bianhuan1();
  blocky++;
  for (i=0;i<4;i++)
  {
   blockt[i]=blockt[i]+10;
  }
  bianhuan3(); 
 }
  if(downstatus==0)
 {
  setTimeout(blockdown,1000);
 }else   downstatus=0;
 
}

function blockleft()  //向左移动
{
 blocktd[blockt[0]]=0;
 blocktd[blockt[1]]=0;
 blocktd[blockt[2]]=0;
 blocktd[blockt[3]]=0;
 if(blocktd[blockt[0]-1]==1 || blocktd[blockt[1]-1]==1 || blocktd[blockt[2]-1]==1 || blocktd[blockt[3]-1]==1 || blockt[0]%10==0 ||blockt[1]%10==0 ||blockt[2]%10==0 ||blockt[3]%10==0 )
 {
  blocktd[blockt[0]]=1;
  blocktd[blockt[1]]=1;
  blocktd[blockt[2]]=1;
  blocktd[blockt[3]]=1; 
  
 }
 else  
 {
  bianhuan1();
  blockx--;
  for (i=0;i<4;i++)
  {
   blockt[i]=blockt[i]-1;
  }
  bianhuan3(); 
 }
 
 
}

function blockright()  //向右移动
{
 blocktd[blockt[0]]=0;
 blocktd[blockt[1]]=0;
 blocktd[blockt[2]]=0;
 blocktd[blockt[3]]=0;
 if(blocktd[blockt[0]+1]==1 || blocktd[blockt[1]+1]==1 || blocktd[blockt[2]+1]==1 || blocktd[blockt[3]+1]==1  || blockt[0]%10==9 ||blockt[1]%10==9 ||blockt[2]%10==9 ||blockt[3]%10==9)
 {
  blocktd[blockt[0]]=1;
  blocktd[blockt[1]]=1;
  blocktd[blockt[2]]=1;
  blocktd[blockt[3]]=1; 
  
 }
 else  
 {
  bianhuan1();
  blockx++;
  for (i=0;i<4;i++)
  {
   blockt[i]=blockt[i]+1;
  }
  bianhuan3(); 
 }
 
 
}

function bianhuan1()  //当前块消失
{
 for(i=0;i<4;i++)
 {
  blocktd[blockt[i]]=0;
  obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="white";
 }
 
}
function bianhuan3()  //当前块出现
{
 for(i=0;i<4;i++)
 {
  blocktd[blockt[i]]=1;
  obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="orange";
 }
}

function bianhuan2()  //当前块初始化
{
 for (ii=0,j=0;ii<16;ii++)
 {
  if(tempblock[ii]==1)
  {
   
   var tempx=ii%4;
   var tempy=(ii-tempx)/4;
   obj.rows(blocky+tempy-1).cells(blockx+tempx-1).bgColor="orange";
   blockt[j]=(blocky+tempy-1)*10+blockx+tempx-1;
   blocktd[blockt[j]]=1;
   j++;
  }
 }
}

function transferblock()  //方块转换
{
 var bsort=1;
 blockname=blocktype;
 allowtran=1;
 for (i=0;i<18;i++)
 {
  if((blocktype+1)== blockstatus[i])
  {
  blocktype++;
  bsort++;
  break;
  }
 }
 if(bsort==1)
 {
 blocktype=blocktype-blocktype%10;
 }
 tempblock=eval("block"+blocktype);
 blocktd[blockt[0]]=0;
 blocktd[blockt[1]]=0;
 blocktd[blockt[2]]=0;
 blocktd[blockt[3]]=0; 
 for (ii=0;ii<16;ii++)
 {
  if(tempblock[ii]==1)
  {
   var tempx=ii%4;
   var tempy=(ii-tempx)/4;
   //if((blockx+tempx-1)>9)
   //blockleft();

   
   if(blocktd[(blocky+tempy-1)*10+blockx+tempx-1]==1  )
   {
    blocktd[blockt[0]]=1;
    blocktd[blockt[1]]=1;
    blocktd[blockt[2]]=1;
    blocktd[blockt[3]]=1;
    blocktype=blockname;
    tempblock=eval("block"+blocktype);
    allowtran=0;
    break;
   }
   if((blockx+tempx-1)<0)
   {
    if(blocktd[blockt[0]+1]==1 || blocktd[blockt[1]+1]==1 || blocktd[blockt[2]+1]==1 || blocktd[blockt[3]+1]==1  )
     {
      allowtran=0;
      break;   
     }
     else  {

     for(i=0;i<4;i++)
     {
     obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="white";
     }
     blockx++;
     for (i=0;i<4;i++)
     {
      blockt[i]=blockt[i]+1;
     }
     for(i=0;i<4;i++)
     {
     obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="orange";
     }
     ii=0; 
    }
   }
   if((blockx+tempx-1)>9)
   {
    if(blocktd[blockt[0]-1]==1 || blocktd[blockt[1]-1]==1 || blocktd[blockt[2]-1]==1 || blocktd[blockt[3]-1]==1  )
    {
    allowtran=0;
    break;
  
    }
    else  
    {
     for(i=0;i<4;i++)
     {
      obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="white";
     }
     blockx--;
     for (i=0;i<4;i++)
     {
      blockt[i]=blockt[i]-1;
     }
     for(i=0;i<4;i++)
     {
      obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="orange";
     }
     ii=0; 
    }
   }
   if((blocky+tempy-1)>14)
   {
    allowtran=0;
    break;
   }
  }
 }
 if(allowtran==1)
 {
  for(i=0;i<4;i++)
  {
  obj.rows((blockt[i]-blockt[i]%10)/10).cells(blockt[i]%10).bgColor="white";
  }
  for (ii=0,j=0;ii<16;ii++)
  {
   if(tempblock[ii]==1)
   {
   var tempx=ii%4;
   var tempy=(ii-tempx)/4;
   blockt[j]=(blocky+tempy-1)*10+blockx+tempx-1;
   blocktd[blockt[j]]=1;
   obj.rows(blocky+tempy-1).cells(blockx+tempx-1).bgColor="orange";
   j++;
   }
  }
 } 
}


function controlkey()  //键盘控制
{
 if (event.keyCode==83)
 {
  downstatus=1;
  blockdown();
 }
 if (event.keyCode==32)
 {
  transferblock();
 }
 if (event.keyCode==65)
 {
  blockleft();
 }
 if (event.keyCode==68)
 {
  blockright();
 }
}

function addblock()  //消除行,新增一个随机块
{
 for(j=140;j>10;j-=10)
 {
  if(blocktd[j]==1 &&blocktd[j+1]==1 && blocktd[j+2]==1 && blocktd[j+3]==1 && blocktd[j+4]==1 && blocktd[j+5]==1 && blocktd[j+6]==1 && blocktd[j+7]==1 && blocktd[j+8]==1 && blocktd[j+9]==1  ) 
  {
  tscore+=10;
  for (jj=0;jj<10;jj++)
   {
    blocktd[j+jj]==0;
   }
   for (w=j+9;w>10;w--)
   {
    blocktd[w]=blocktd[w-10];
   }
   j+=10;
  }
 }
 
 for (j=149;j>10;j--)
 {
  if(blocktd[j]==1)
  {
   obj.rows((j-j%10)/10).cells(j%10).bgColor="orange";
  }
  if(blocktd[j]==0)
  {
   obj.rows((j-j%10)/10).cells(j%10).bgColor="white";
  }
 }
 var ttscore=document.getElementById("nscore");
 ttscore.innerText="得分:"+tscore;
 if(blocktd[24]==1||blocktd[25]==1)
 {
 
 alert("恭喜你,你现在的得分是"+tscore+"分.   Game Over! 点确定重新开始");
 window.location.reload();
 
 }
 blocktype=(Math.floor(Math.random()*7)+1)*10;
 tempblock=eval("block"+blocktype);
 blockx=4;
 blocky=1;
 bianhuan2();
}


for (i=0;i<15;i++)                  //初始化程序
{
 var objrow= obj.insertRow();
 for (j=0;j<10;j++)
    {
        var objcell=objrow.insertCell();
  objcell.innerHTML="&nbsp";
    }
}
blocktype=(Math.floor(Math.random()*7)+1)*10;
tempblock=eval("block"+blocktype);
bianhuan2();
setTimeout(blockdown,1000);

</script>
<!-- //************************************
//    大体思路是:在执行方块左下右移动和方块转换的时候用blocktd模拟下一祯的位置,
//    看与现有的方块冲突不冲突,冲突则不执行该操作.
//***********************************  -->


 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页