贪吃蛇 Snake v1.0 body{ } table{ border-collapse: collapse; border:solid #333 1px; } td{ height: 10px; width: 10px; font-size: 0px; } .filled{ background-color:blue; } function $(id){return document.getElementById(id);} /** * javascript贪吃蛇 v1.0 * author: sunxing007 * 05/14/2009 * 转载请保留 author: sunxing007 谢谢 **/ //贪吃蛇类 var Snake = { tbl: null, /** * body: 蛇身,数组放蛇的每一节, * 数据结构{x:x0, y:y0, color:color0}, * x,y表示坐标,color表示颜色 **/ body: [], //当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它 direction: 0, //定时器 timer: null, //速度 speed: 200, //初始化 init: function(){ var colors = ['red','orange','yellow','green','blue','purple','#ccc']; this.tbl = $("main"); var x = 0; var y = 0; var colorIndex = 0; //产生初始移动方向 this.direction = Math.floor(Math.random()*4); //产生20个松散节点 for(var i=0; i<20; i++){ x = Math.floor(Math.random()*50); y = Math.floor(Math.random()*50); colorIndex = Math.floor(Math.random()*7); if(!this.isCellFilled(x,y)){ this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex]; } } //产生蛇头 while(true){ x = Math.floor(Math.random()*50); y = Math.floor(Math.random()*50); if(!this.isCellFilled(x,y)){ this.tbl.rows[y].cells[x].style.backgroundColor = "black"; this.body.push({x:x,y:y,color:'black'}); break; } } //alert(this.body[0].x); //添加键盘事件 document.onkeydown= function(){ var code = event.keyCode; switch(code){ case 37:{//left //阻止蛇倒退走 if(Snake.direction==1){ break; } Snake.direction = 3; break; } case 38:{//up if(Snake.direction==2){ break; } Snake.direction = 0; break; } case 39:{//right if(Snake.direction==3){ break; } Snake.direction = 1; break; } case 40:{//down if(Snake.direction==0){ break; } Snake.direction = 2; break; } } } }, //移动 move: function(){ this.timer = setInterval(function(){ Snake.erase(); Snake.moveOneStep(); Snake.paint(); }, this.speed); }, //移动一节身体 moveOneStep: function(){ if(this.checkNextStep()==-1){ clearInterval(this.timer); alert("Game over!\nPress F5 to Restart."); /** $('btn').disabled = false; $('btn').onclick = function(){ Snake.restart(); }; **/ return; } if(this.checkNextStep()==1){ var _point = this.getNextPos(); var _x = _point.x; var _y = _point.y; var _color = this.getColor(_x,_y); this.body.unshift({x:_x,y:_y,color:_color}); return; } //window.status = this.toString(); var point = this.getNextPos(); var color = this.body[0].color; this.body.pop(); this.body.unshift({x:point.x,y:point.y,color:color}); //调试 //window.status = this.toString(); }, //探寻下一步将走到什么地方 getNextPos: function(){ var x = this.body[0].x; var y = this.body[0].y; var color = this.body[0].color; //向上 if(this.direction==0){ y--; } //向右 else if(this.direction==1){ x++; } //向下 else if(this.direction==2){ y++; } //向左 else{ x--; } //返回一个坐标 return {x:x,y:y}; }, //检查将要移动到的下一步是什么 checkNextStep: function(){ var point = this.getNextPos(); var x = point.x; var y = point.y; if(x<0||x>=50||y<0||y>=50){ return -1;//触边界,游戏结束 } for(var i=0; i<this.body.length; i++){ if(this.body[i].x==x&&this.body[i].y==y){ return -1;//碰到自己的身体 } } if(this.isCellFilled(x,y)){ return 1;//有东西 } return 0;//空地 }, //擦除蛇身 erase: function(){ for(var i=0; i<this.body.length; i++){ this.eraseDot(this.body[i].x, this.body[i].y); } }, //绘制蛇身 paint: function(){ for(var i=0; i<this.body.length; i++){ this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color); } }, //擦除一节 eraseDot: function(x,y){ this.tbl.rows[y].cells[x].style.backgroundColor = ""; }, paintDot: function(x,y,color){ this.tbl.rows[y].cells[x].style.backgroundColor = color; }, //得到一个坐标上的颜色 getColor: function(x,y){ return this.tbl.rows[y].cells[x].style.backgroundColor; }, //用于调试 toString: function(){ var str = ""; for(var i=0; i<this.body.length; i++){ str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - "; } return str; }, //检查一个坐标点有没有被填充 isCellFilled: function(x,y){ if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){ return false; } return true; }, restart: function(){ if(this.timer){ clearInterval(this.timer); } for(var i=0; i<this.tbl.rows.length;i++){ for(var j=0; j<this.tbl.rows.length; i++){ this.tbl.rows[i].cells[j].style.backgroundColor = ""; } } this.body = []; this.init(); } //; }; /******************************************* * javascript贪吃蛇 v1.0 * author: sunxing007 * 05/14/2009 * 转载请保留 author: sunxing007 字样,谢谢 *******************************************/ Begin [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]