日期:2014-05-16 浏览次数:20416 次
欢迎大家提出宝贵的意见和建议,欢迎批评指正!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>贪吃蛇</title> <style type="text/css"> div#gameBoard{ position:relative; width: 1000px; height: 500px; background: gray; margin: 0 auto;} div#gameControl{ width: 998px; height: 50px; line-height: 50px; border: 1px solid gray; margin: 0 auto;} div#gameControl span#banner{ float: left;} div#gameControl div#buttons{ float: right;} div.block{ width: 20px; height: 20px; float: left;} div.snake_block{ background-color: red; position: absolute; } div.food_block{ background-color: yellow; position: absolute; } </style> <script type="text/javascript"> var SNAKE = {};//命名空间 SNAKE.Config = { MAX_ROWS : 25,//定义面板的最大行数 MAX_COLS : 50 //定义面板的最大列数 }; /** 定义蛇类 */ SNAKE.Snake = function (oBoard){ this.snakeBody = [];//蛇身体 this.isDead = false;//标识蛇是否死亡 this.dir = 4; //1:上 2:下 3:左 4:右 this.isAlive = true; // (20,40),(40, 40),(60, 40) for(var i = 3 ; i > 0 ; i--){ //(20*i, 40) var oDivBlock = document.createElement("div"); oDivBlock.className = "block snake_block"; oDivBlock.style.left = 20*i + "px"; oDivBlock.style.top = 40 + "px"; oBoard.container.appendChild(oDivBlock); this.snakeBody.push(oDivBlock);//将产生的每一个蛇的div block添加到蛇的身体数组中 } this.snakeHead = this.snakeBody[0]; this.moveOn = function(){ var nextPos = {}; nextPos.left = parseInt(this.snakeHead.style.left); nextPos.top = parseInt(this.snakeHead.style.top); //var snakeTail = this.snakeBody.pop(); switch(this.dir){ case 1://↑ nextPos.top -= 20; break; case 2: //↓ nextPos.top += 20; break; case 3://← nextPos.left -= 20; break; case 4://→ nextPos.left += 20; break; } var oNewHead = document.createElement("div"); oNewHead.className = "block snake_block"; oNewHead.style.left = nextPos.left + "px"; oNewHead.style.top = nextPos.top + "px"; this.snakeBody.unshift(oNewHead); oBoard.container.appendChild(oNewHead); var snakeTail = this.snakeBody.pop(); snakeTail.parentNode.removeChild(snakeTail); this.snakeHead =this.snakeBody[0]; this.eat(oBoard.food); };//蛇游走 this.eat = function(food){ var oFoodBlock = food.getFoodBlock(); //alert("head"+this.snakeHead.style.left +",food"+ oFoodBlock.style.left) if(parseInt(this.snakeHead.style.left) == parseInt(oFoodBlock.style.left) && parseInt(this.snakeHead.style.top) == parseInt(oFoodBlock.style.top)){ //吃到食物 this.increaseBody(oFoodBlock); oFoodBlock.parentNode.removeChild(oFoodBlock); food.locate(); } else { //撞到自己的身体 for(var i = 1 ; i < this.snakeBody.length ; i++){ var oSnakeBlock = this.snakeBody[i]; if( parseInt(this.snakeHead.style.left) == parseInt(oSnakeBlock.style.left) && parseInt(this.snakeHead.style.top) == parseInt(oSnakeBlock.style.top) ){ alert("die"); this.die(); return; } } //撞墙 if(parseInt(this.snakeHead.style.left) == -20 || parseInt(this.snakeHead.style.left) == 1000 || parseInt(this.snakeHead.style.top) == -20 || parseInt(this.snakeHead.style.top) == 500){ alert("die"); this.die(); return; } } };//蛇吃东西 this.increaseBody = function(oFoodBlock){ var oNewBlock = document.createElement("div"); oNewBlock.style.left = oFoodBlock.st