我太傻了
不应该开两个帖把代码分开的
下边的代码续着上边的代码
//--------------------------------------------------------------------
function move(d)//d为运动方向,有四个:left:37,up:38,right:39,down:40
{
var LHP=Array(2);
LHP[0]=this.bodys[0].style.left;
LHP[1]=this.bodys[0].style.top;
var movement=new Array(2);
if(parseInt(this.bodys[0].style.top)==parseInt(this.bodys[1].style.top))
 if(parseInt(this.bodys[0].style.left)<parseInt(this.bodys[1].style.left))
 this.direction="left";
 else
 this.direction="right";
else 
 if(parseInt(this.bodys[0].style.top)<parseInt(this.bodys[1].style.top))
   this.direction="up";
 else
   this.direction="down";
switch(d)
{
case 37:
case 39:
switch(this.direction)
{
case "left": movement[0]=0;movement[1]=(38-d)*16;break;
case "right": movement[0]=0;movement[1]=(d-38)*16;break;
case "up":     movement[0]=(d-38)*16;movement[1]=0;break;
case "down": movement[0]=(38-d)*16;movement[1]=0;break;
}

var newTop=parseInt(this.bodys[0].style.top)+movement[1];
var newLeft=parseInt(this.bodys[0].style.left)+movement[0];
//alert(newLeft+","+newTop);
if(!this.check(newLeft,newTop))
{
 var a=0.0;
 while(a<25000.0)a=a*0.001/0.001*0.001/0.001+1.0;
 this.bodys[0].style.top=newTop;
 this.bodys[0].style.left=newLeft;
 this.onHeadMove(LHP);
 
  if(this.addFlag)
  {this.addBody(this.block.delBlock());this.addFlag=false;}
 if(newLeft==parseInt(this.block.getLatestBlock().style.left)&&newTop==parseInt(this.block.getLatestBlock().style.top))
  this.block.newBlock(this.makeBlockPosition());
 if(this.bodys[this.long-1].style.left==this.block.getOldestBlock().style.left&&this.bodys[this.long-1].style.top==this.block.getOldestBlock().style.top) 
  this.addFlag=true;
 
 return true;
}
else return false;
default:
switch(this.direction)
{
case "left": movement[0]=-16;movement[1]=0;break;
case "right": movement[0]=16;movement[1]=0;break;
case "up":     movement[0]=0;movement[1]=-16;break;
case "down": movement[0]=0;movement[1]=16;break;
}

var newTop=parseInt(this.bodys[0].style.top)+movement[1];
var newLeft=parseInt(this.bodys[0].style.left)+movement[0];
if(!this.check(newLeft,newTop))
{
 this.bodys[0].style.top=newTop;
 this.bodys[0].style.left=newLeft;
 this.onHeadMove(LHP);
   
 if(this.addFlag)
  {
 this.addBody(this.block.delBlock());
 this.addFlag=false;
 }
 if(newLeft==parseInt(this.block.getLatestBlock().style.left)&&newTop==parseInt(this.block.getLatestBlock().style.top))
  {this.block.newBlock(this.makeBlockPosition());}

 if(this.bodys[this.long-1].style.left==this.block.getOldestBlock().style.left&&this.bodys[this.long-1].style.top==this.block.getOldestBlock().style.top) 
  {
 this.addFlag=true;
 }
 return true;
}
else return false;
}

}
//--------------------------------------------------------------------
function addBody(newBlock)
{
this.wholeBody.appendChild(newBlock);
this.long++;
}
//--------------------------------------------------------------------
function makeBlockPosition()
{
var position=new Array(2);
position[0]=Math.round(Math.random()*48)*16;
position[1]=Math.round(Math.random()*26)*16;

for(i=0;i<this.long;i++)
if(position[0]==parseInt(this.bodys[i].style.left))
if(position[1]==parseInt(this.bodys[i].top))
{
 position[0]=Math.round(Math.random()*48)*16;
 position[1]=Math.round(Math.random()*26)*16;
 i=0;
}

return position;
}
//--------------------------------------------------------------------
function Snake(headPosition,long,velocity,state)//headPosition为数组包括legt和top单位px
{
this.block=new Block();
this.addFlag=false;
this.long=long;
this.velocity=velocity;
this.direction="right";
this.makeBlockPosition=makeBlockPosition;
this.check=check;
this.addBody=addBody;
this.onHeadMove=onHeadMove;
this.move=move;
document.write("<div id='area' style='position:absolute; left:7px; top:8px; width:784px; height:432px; z-index:1; border-width:1px; border-style:solid; border-color:#000000'>");
var tp=headPosition;
for(i=0;i<this.long;i++)
{
 if(state=="vertical")
 {
  document.write("<div id='body"+i+"' style='position:absolute; left:"+tp[0]+"px; top:"+tp[1]+"px; width:13px; height:13px; z-index:1;  background-color: #CCCCCC; layer-background-color: #CCCCCC;'></div>");
  tp[1]=parseInt(tp[1])+16;
 }
 if(state=="horizontal")
 {
  document.write("<div id='body"+i+"' style='position:absolute; left:"+tp[0]+"px; top:"+tp[1]+"px; width:13px; height:13px; z-index:1; background-color: #CCCCCC; layer-background-color: #CCCCCC;'></div>");
    tp[0]=tp[0]-16;
 }
}
document.write("</div>");
this.wholeBody=document.all("area");
this.bodys=document.all("area").childNodes;
this.block.newBlock(this.makeBlockPosition());
}
//--------------------------------------------------------------------
var hp=new Array(2);
hp[0]=432;
hp[1]=224;
var snake=new Snake(hp,5,80,"horizontal");
alert("Press the key to start!");
var timer=window.setInterval('snake.move();',snake.velocity);
</script>
</body>
</html>