将以下内容保存为Joken.html双击即可运行
本文档仅供研究学习使用,请慎重操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var onStart = true;
var allwh = 600;
var snakewh = 20;
var isSnake = true; //用于初始化蛇
var isFood = true; //用于记录是否产生食物
var avoidBack = true; //用于防止回走
var firstDir = ""; //初始方向
var dir = ""; //按键方向
var changW;
var changH;
var snakeArray = new Array; //蛇的ID (内涵坐标)
var foodCount = 0;
var gameOver = false;
var maxScore = 0;
var nowScore =0;
//var speed = 0;
var XXYY = [0,0];
var Time = 400;
function loopbegin(){
//alert("Time = "+Time);
if(onStart){
t = setInterval("begin()",150);
onStart = false;
}}
function begin(){
removeSnakeFood();
initSnake(); //初始化蛇
createFood(); //初始化食物
meetSnakeFood(); //蛇与食物相遇
moveSnake();
checkSnakeOut();
checkSnakeEatSelf();
//getMessage();
}
function getMessage(){
var changS = 2;
var L = snakeArray.length;
if(L<5)
{ changS = 2;}
else if(L<10)
{ changS = 4;}
else if(L<15)
{ changS = 6;}
else if(L<20)
{ changS = 8;}
else
{changS = 10;}
nowScore += changS;
if(maxScore<nowScore){
maxScore = nowScore;
}
if(L<=20)
{ Time = Time - L;
};
if(Time<=80) { Time = 80; }
speed = parseInt(1000/Time*10)/10.0;
stopSnake();
loopbegin(); NowScore.innerHTML="当前得分:"+nowScore;
MaxScore.innerHTML="最高分:"+maxScore; Spend.innerHTML="当前速度每秒走 "+speed+" 格";}
function removeSnakeFood(){
if(gameOver){ isSnake = true; //用于初始化蛇
isFood = true; //清除食物
var all = ElementById("all");
js教程removechildfor(var i=0; i<=foodCount; i++){
var food = ElementById("food"+i);
veChild(food);}
snakeArray.length = 0; gameOver = false;
foodCount = 0;
//Time = 500;
//nowScore = 0;}}
function initSnake(){
if(isSnake){
var snakeT = ElementById('snakeT');
var snakeT_x = parseInt(Math.random()*(allwh/snakewh-8)+4)*snakewh;
var snakeT_y = parseInt(Math.random()*(allwh/snakewh-8)+4)*snakewh;
snakeT.style.left = snakeT_x+"px";
p = snakeT_y+"px";
snakeT.style.backgroundColor="#000000";
snakeT.style.borderColor = "#000000"; snakeArray.push(snakeT); //蛇头进站
var keyCode =parseInt(Math.random()*4)+37;
if(keyCode==37){
firstDir="left";
changW = -snakewh;
changH = 0;
}
else if(keyCode==38){
firstDir="top";
changW = 0;
changH = -snakewh; }
else if(keyCode==39){ firstDir="right";
changW = snakewh;
changH = 0;
}
else if(keyCode==40){ firstDir="bottom";
changW = 0;
changH = snakewh;
}
isSnake = false;
}} function createFood(){
var all = ElementById("all");
var food_x;
var food_y; if(isFood){
var food = ateElement("div");
var strW = [];
var strH = []; for(var i=0; i<snakeArray.length; i++){
var snakeNode_x = snakeArray[i].style.left;
var snakeNode_y = snakeArray[i].p;
snakeNode_x = snakeNode_x.substring(0,snakeNode_x.indexOf("px"));
snakeNode_y = snakeNode_y.substring(0,snakeNode_y.indexOf("px"));
strW[i] = snakeNode_x;
strH[i] = snakeNode_y;
}
var flag;
do{
flag = false;
food_x = parseInt(Math.random()*(allwh/snakewh-0)+0)*snakewh;
food_y = parseInt(Math.random()*(allwh/snakewh-0)+0)*snakewh;
for(var i=0; i<strW.length; i++){
if(food_x==strW[i]&&food_y==strH[i]){
flag = true;
break;
}}}
while(flag); food.style.left = food_x+"px";
p = food_y+"px";
food.style.width = snakewh+"px";
food.style.height = snakewh+"px";
food.style.backgroundColor='#FF0000'; food.style.borderColor='#FF0000';
food.style.position = 'absolute';
food.id ='food'+foodCount;
all.appendChild(food);
isFood = false; //产生食物后 firstFood变为 0 ; } }
function meetSnakeFood(){
var snake01_x =snakeArray[0].style.left;
var snake01_y =snakeArray[0].p;
var eatFood = ElementById('food'+foodCount);
var food_x= eatFood.style.left;
var food_y = p;
if(snake01_x==food_x&&snake01_y==food_y){ snakeArray.push(eatFood); //添加食物于蛇尾部
foodCount ++;
isFood = true; getMessage();
}}
function moveSnake(){
var snake01 = snakeArray[0];
var snake01_x =snake01.style.left;
var snake01_y =p;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论