![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>wujinjian </title>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var snake_arr=new Array();
var snakeBodyW_H=20; //蛇身大小
var direction="right";
var sd; //速度
var footDivObj;
var mapDivObj;
var footnum=0;
var footMaxnum=10; //吃到10颗食物过关
var timer;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function resetStart(_sd,mode)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
document.getElementById("start_bt_id").disabled=true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(mode=="first")
document.getElementById("tx_id").value="第 1 关";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for(var i=0;i <snake_arr.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
document.body.removeChild(snake_arr[i]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
snake_arr.splice(0,snake_arr.length); //删除所有的元素
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
footnum=0;
direction="right";
isStop=false;
sd=_sd;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
startGame();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function addSnakeBody()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
snake_arr[snake_arr.length]=createSnakeBody();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function createSnakeBody()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var snakeDiv=document.createElement("div");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
snakeDiv.style.position="absolute";
snakeDiv.style.left="120px";
snakeDiv.style.top="300px";
snakeDiv.style.width=snakeBodyW_H+"px";
snakeDiv.style.height=snakeBodyW_H+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
document.body.appendChild(snakeDiv);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return snakeDiv;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function startGame()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//初始化蛇身的个数,这里是3个
addSnakeBody();
addSnakeBody();
addSnakeBody();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
snakeMove();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function snakeMove()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
timer=setTimeout("snakeMove()",sd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var last=snake_arr[snake_arr.length-1];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//把最后一个元素移到第一个
for(var i=snake_arr.length-1;i>0;i--)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
snake_arr[i]=snake_arr[i-1];
snake_arr[i].style.backgroundColor="red";
snake_arr[i].style.border="white solid 1px";
}
snake_arr[0]=last;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var first=snake_arr[0];
var second=snake_arr[1];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
first.style.backgroundColor="blue";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var _left=second.style.left.replace("px","")-0;
var _top=second.style.top.replace("px","")-0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(footDivObj==null)
footDivObj=document.getElementById("food_div_id");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(mapDivObj==null)
mapDivObj=document.getElementById("map_id");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(direction=="left")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
first.style.left=_left-snakeBodyW_H+"px";
first.style.top=_top+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//判断是否吃到食物
isDivSuperpose(first,footDivObj);
//判断失败
isKill(first,mapDivObj);
}
else if(direction=="up")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
first.style.left=_left+"px";
first.style.top=_top-snakeBodyW_H+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
isDivSuperpose(first,footDivObj);
isKill(first,mapDivObj);
}
else if(direction=="right")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
first.style.left=_left+snakeBodyW_H+"px";
first.style.top=_top+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
isDivSuperpose(first,footDivObj);
isKill(first,mapDivObj);
}
else if(direction=="down")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
first.style.left=_left+"px";
first.style.top=_top+snakeBodyW_H+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
isDivSuperpose(first,footDivObj);
isKill(first,mapDivObj);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function keyDown(e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(e.keyCode==37) //向左
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(direction!=="right")
direction="left";
}
else if(e.keyCode==38) //向上
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(direction!=="down")
direction="up";
}
else if(e.keyCode==39) //向右
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(direction!=="left")
direction="right";
}
else if(e.keyCode==40) //向下
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(direction!=="up")
direction="down";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
function isKill(snakeDivObj,_mapDivObj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var snakeDivLeft = snakeDivObj.style.left.replace("px","")-0;
var snakeDivTop = snakeDivObj.style.top.replace("px","")-0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var _mapDivLeft = _mapDivObj.style.left.replace("px","")-0;
var _mapDivTop = _mapDivObj.style.top.replace("px","")-0;
var _mapDivWidth = _mapDivObj.style.width.replace("px","")-0;
var _mapDivHeight = _mapDivObj.style.height.replace("px","")-0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(snakeDivLeft+snakeBodyW_H>=_mapDivLeft+_mapDivWidth)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
clearTimeout(timer);
alert("失败!");
document.getElementById("start_bt_id").disabled=false;
}
else if(snakeDivLeft <=_mapDivLeft)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
clearTimeout(timer);
alert("失败!");
document.getElementById("start_bt_id").disabled=false;
}
else if(snakeDivTop <=_mapDivTop)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
clearTimeout(timer);
alert("失败!");
document.getElementById("start_bt_id").disabled=false;
}
else if(snakeDivTop+snakeBodyW_H>=_mapDivTop+_mapDivHeight)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
clearTimeout(timer);
alert("失败!");
document.getElementById("start_bt_id").disabled=false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//食物
function food()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var randomnum1=Math.random();
var randomnum2=Math.random();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var foodx=Math.round(870 * randomnum1)+50;
var foody=Math.round(430 * randomnum2)+50;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(footDivObj==null)
footDivObj=document.getElementById("food_div_id");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
footDivObj.style.left=foodx+"px";
footDivObj.style.top=foody+"px";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
footnum=footnum+1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(footnum==footMaxnum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
clearTimeout(timer);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert("恭喜你进入下一关");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
sd=sd-10; //速度
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
document.getElementById("tx_id").value="第 "+(document.getElementById("tx_id").value.split(" ")[1]-0+1)+" 关";
resetStart(sd,"next");
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//判断蛇是否吃到食物
function isDivSuperpose(snakeDivObj,_footDivObj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var rv=withInDivSuperpose(snakeDivObj,_footDivObj);
if(rv=="-111")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var rv2=withInDivSuperpose(_footDivObj,snakeDivObj);
if(rv2=="-111")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
// no
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//吃到食物
food();
addSnakeBody();
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//吃到食物
food();
addSnakeBody();
}
}
function withInDivSuperpose(snakeDivObj,_footDivObj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*div_1*/
//左上角
var divLeftTopX_1=snakeDivObj.style.left.replace("px","")-0;
var divLeftTopY_1=snakeDivObj.style.top.replace("px","")-0;
//右下角
var divRightBottomX_1=snakeDivObj.style.width.replace("px","")-0 + divLeftTopX_1;
var divRightBottomY_1=snakeDivObj.style.height.replace("px","")-0 + divLeftTopY_1;
//左下角
var divLeftBottomX_1=divLeftTopX_1;
var divLeftBottomY_1=divRightBottomY_1;
//右上角
var divRightTopX_1=divRightBottomX_1;
var divRightTopY_1=divLeftTopY_1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*div_2*/
//左上角
var divLeftTopX_2=_footDivObj.style.left.replace("px","")-0;
var divLeftTopY_2=_footDivObj.style.top.replace("px","")-0;
//右下角
var divRightBottomX_2=_footDivObj.style.width.replace("px","")-0 + divLeftTopX_2;
var divRightBottomY_2=_footDivObj.style.height.replace("px","")-0 + divLeftTopY_2;
//左下角
var divLeftBottomX_2=divLeftTopX_2;
var divLeftBottomY_2=divRightBottomY_2;
//右上角
var divRightTopX_2=divRightBottomX_2;
var divRightTopY_2=divLeftTopY_2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(divLeftTopX_2>=divLeftTopX_1 && divLeftTopY_2>=divLeftTopY_1 && divLeftTopX_2 <=divRightBottomX_1 && divLeftTopY_2 <=divRightBottomY_1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//alert("111");
return "111";
}
else if(divRightTopX_2 <=divRightTopX_1 && divRightTopY_2>=divRightTopY_1 && divRightTopX_2>=divLeftBottomX_1 && divRightTopY_2 <=divLeftBottomY_1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//alert("222");
return "222";
}
else if(divRightBottomX_2>=divLeftTopX_1 && divRightBottomY_2>=divLeftTopY_1 && divRightBottomX_2 <=divRightBottomX_1 && divRightBottomY_2 <=divRightBottomY_1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//alert("333");
return "333";
}
else if(divLeftBottomX_2 <=divRightTopX_1 && divLeftBottomY_2>=divRightTopY_1 && divLeftBottomX_2 >=divLeftBottomX_1 && divLeftBottomY_2 <=divLeftBottomY_1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//alert("444");
return "444";
}
else if(divLeftTopX_2 <=divLeftTopX_1 && divLeftTopY_2 <=divLeftTopY_1 && divRightBottomX_2>=divRightBottomX_1 && divRightBottomY_2>=divRightBottomY_1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//alert("555");
return "555";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return "-111";
}
</script>
</head>
<body onkeydown="keyDown(event)" style="overflow:hidden" scroll="no">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div id="map_id" style="position:absolute;left:50px;top:50px;height:480px;width:920px;background-color:rgb(223,223,223)">
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div id="food_div_id" style="position:absolute;left:300px;top:230px;height:20px;width:20px;border:white solid 1px;background-color:red;">
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<input type="button" id="start_bt_id" value="Start" onclick="resetStart(100,'first')">
<input type="text" id="tx_id" readonly>
</body>
</html>
posted @
2009-08-31 15:23
不必太用力
阅读(
173)
评论()
编辑
收藏
举报