我太傻了
不应该开两个帖把代码分开的
下边的代码续着上边的代码
//--------------------------------------------------------------------
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>
不应该开两个帖把代码分开的
下边的代码续着上边的代码
//--------------------------------------------------------------------
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>
解决方案 »
- 行路 求教HTML5 audio 中的ended事件问题。
- jquery validate 得到焦点提示输入信息 失去焦点报错 怎么写
- 仿CSDN论坛的左侧导航树型菜单功能
- Javascript王者归来 160页例子 问题咨询
- 看js框架源码里面好多语法看不懂,希望大家能推荐一款较全的javascript语法手册或教程
- 关闭thickbox弹出层后,页面上的文本框无法聚焦
- 为何nodevalue为null
- javaScript 操作 excel 为什么要让客户端把本站点添加到可信任站点才能执行
- 关于javascript数组长度的问题。
- 请教JQuery easyui-tree的一个问题
- 怎样能够读出textarea的文本内容
- 在javascript中可不可以調用sql語句?
http://61.163.238.19/tpeng/fengcai/zuopin.htm写得不怎么好,不是面向对象的方法,不过好像没什么问题。
对了,用了cw,ie6.0sp1的注意了