经过几个工作闲暇时间的整理和网上各前辈写JS蛇的思路,自己用js写了一个贪吃蛇!
网上的思路有很多,有的人的思路是建立二唯数组,然后去循环建立表格,然后保存所有活动范围的坐标!而我的思路我是用到了DOM元素的循环遍历,蛇用了p标签,食物用了span标签,希望各位能点评一下,哪些地方需要修改什么的,核心是简化代码和效率!让我的代码更能体现出一些面向对象的思想,欢迎各位严厉批评指正!非常感谢!
需要补充一个功能那就是在我这个代码基础上如何判断蛇头!这个问题我一直没能想出来!
代码如下:
<script type="text/javascript">
var snake={
st:500,//速度初始为0.5秒移动一次,数值越小速度越快!
num:0,
start:function(){//初始化,建立外围DIV框架,键盘事件,食物或蛇的初始数量和位置
div=document.createElement('div');
div.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:20px;width:400px;height:400px;border:1px solid #000;";
div.id='kj';
div1=document.createElement('div');
div1.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:430px;width:400px;height:100px;";
document.body.appendChild(div);
document.body.appendChild(div1);
div1.innerHTML='键盘上↑↓←→代表方向控制,小键盘上0加速,1减速,空格暂停!';
document.onkeydown=function(e){snake.dir(e||window.event);}
this.createshe(0,200);
this.createshe(20,200);
this.createfood();
},
pause:function(){//游戏暂停
clearTimeout(this.tt);
},
createfood:function(){//产生选区内的一个随机坐标的食物(SPAN标签)!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
this.p=document.getElementsByTagName("p");//获得所有蛇对象
while(this.checkbody(this.x,this.y))
{//进行循环判断,让随即生成的食物位置不能和蛇的位置重合,重合就重刷随机数!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
}
this.food=document.createElement("span");
this.food.style.cssText="position:absolute;width:20px;height:20px;background:green;border:1px solid #ccc;";
this.food.style.left=this.x+"px";
this.food.style.top=this.y+"px";
document.getElementById('kj').appendChild(this.food);
},
createshe:function(a,b){//用P标签创建蛇!
var sna=document.createElement("p");
sna.style.cssText="position:absolute;margin:0;padding:0;width:20px;height:20px;background:red;border:1px solid #ccc;";
sna.style.left=parseInt(a)+"px";
sna.style.top=parseInt(b)+"px";
this.x1=parseInt(a);
this.y1=parseInt(b);
document.getElementById('kj').appendChild(sna);
},
dir:function(e){//方向控制
var edir;
if(typeof(this.tt)!='undefined'){clearTimeout(this.tt);}//防止方向键多次键入导致速度不断增加!
if(typeof(direction)!='undefined')
{edir=Math.abs(direction-e.keyCode)==2?direction:e.keyCode;}//反向无效!
else
{edir=e.keyCode;}
switch(edir)
{
case 37:this.gox = -20;this.goy=0; direction=37; this.move(); break;//左
case 39:this.gox = 20; this.goy=0; direction=39; this.move(); break;//右
case 38:this.gox = 0; this.goy=-20;direction=38; this.move(); break;//上
case 40:this.gox = 0; this.goy=20; direction=40; this.move(); break;//下
case 32:this.pause(); break;//暂停
case 96:this.st=this.st-100;this.move();break;//加速
case 97:this.st=this.st+100;this.move();break;//减速
}
},
move:function(){//响应键盘进行移动的事件
w=this.x1+this.gox;//每吃一次食物获得一次坐标
h=this.y1+this.goy;
if(w<0||w>=400||h<0||h>=400||this.checkbody(w,h))//进行边界检测和自身的碰撞检测
{
clearTimeout(this.tt);
alert("Game over!吃了"+this.num+"个食物!");
window.location.reload();//游戏结束重新刷新页面!
}
if(this.x1==this.x&&this.y1==this.y)
{//判断蛇头是否遇到食物的位置,如果是就移除食物,重新生成,否则就是删除最开始建立的蛇,也就是蛇尾!
document.getElementById('kj').removeChild(this.food);
this.num++;
this.createfood();
}
else
{
document.getElementById('kj').removeChild(this.p[0]);
}
this.createshe(w,h);
this.tt=setTimeout("snake.move()",this.st);
},
checkbody:function(a,b){//检查蛇是否碰到自身的方法
this.p=document.getElementsByTagName("p");
for(var i=0,j=this.p.length;i<j;i++)
{
if(this.p[i].style.left==a+"px"&&this.p[i].style.top==b+"px")return true;
}
}
}
window.onload=function(){
snake.start();
}
</script>
网上的思路有很多,有的人的思路是建立二唯数组,然后去循环建立表格,然后保存所有活动范围的坐标!而我的思路我是用到了DOM元素的循环遍历,蛇用了p标签,食物用了span标签,希望各位能点评一下,哪些地方需要修改什么的,核心是简化代码和效率!让我的代码更能体现出一些面向对象的思想,欢迎各位严厉批评指正!非常感谢!
需要补充一个功能那就是在我这个代码基础上如何判断蛇头!这个问题我一直没能想出来!
代码如下:
<script type="text/javascript">
var snake={
st:500,//速度初始为0.5秒移动一次,数值越小速度越快!
num:0,
start:function(){//初始化,建立外围DIV框架,键盘事件,食物或蛇的初始数量和位置
div=document.createElement('div');
div.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:20px;width:400px;height:400px;border:1px solid #000;";
div.id='kj';
div1=document.createElement('div');
div1.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:430px;width:400px;height:100px;";
document.body.appendChild(div);
document.body.appendChild(div1);
div1.innerHTML='键盘上↑↓←→代表方向控制,小键盘上0加速,1减速,空格暂停!';
document.onkeydown=function(e){snake.dir(e||window.event);}
this.createshe(0,200);
this.createshe(20,200);
this.createfood();
},
pause:function(){//游戏暂停
clearTimeout(this.tt);
},
createfood:function(){//产生选区内的一个随机坐标的食物(SPAN标签)!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
this.p=document.getElementsByTagName("p");//获得所有蛇对象
while(this.checkbody(this.x,this.y))
{//进行循环判断,让随即生成的食物位置不能和蛇的位置重合,重合就重刷随机数!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
}
this.food=document.createElement("span");
this.food.style.cssText="position:absolute;width:20px;height:20px;background:green;border:1px solid #ccc;";
this.food.style.left=this.x+"px";
this.food.style.top=this.y+"px";
document.getElementById('kj').appendChild(this.food);
},
createshe:function(a,b){//用P标签创建蛇!
var sna=document.createElement("p");
sna.style.cssText="position:absolute;margin:0;padding:0;width:20px;height:20px;background:red;border:1px solid #ccc;";
sna.style.left=parseInt(a)+"px";
sna.style.top=parseInt(b)+"px";
this.x1=parseInt(a);
this.y1=parseInt(b);
document.getElementById('kj').appendChild(sna);
},
dir:function(e){//方向控制
var edir;
if(typeof(this.tt)!='undefined'){clearTimeout(this.tt);}//防止方向键多次键入导致速度不断增加!
if(typeof(direction)!='undefined')
{edir=Math.abs(direction-e.keyCode)==2?direction:e.keyCode;}//反向无效!
else
{edir=e.keyCode;}
switch(edir)
{
case 37:this.gox = -20;this.goy=0; direction=37; this.move(); break;//左
case 39:this.gox = 20; this.goy=0; direction=39; this.move(); break;//右
case 38:this.gox = 0; this.goy=-20;direction=38; this.move(); break;//上
case 40:this.gox = 0; this.goy=20; direction=40; this.move(); break;//下
case 32:this.pause(); break;//暂停
case 96:this.st=this.st-100;this.move();break;//加速
case 97:this.st=this.st+100;this.move();break;//减速
}
},
move:function(){//响应键盘进行移动的事件
w=this.x1+this.gox;//每吃一次食物获得一次坐标
h=this.y1+this.goy;
if(w<0||w>=400||h<0||h>=400||this.checkbody(w,h))//进行边界检测和自身的碰撞检测
{
clearTimeout(this.tt);
alert("Game over!吃了"+this.num+"个食物!");
window.location.reload();//游戏结束重新刷新页面!
}
if(this.x1==this.x&&this.y1==this.y)
{//判断蛇头是否遇到食物的位置,如果是就移除食物,重新生成,否则就是删除最开始建立的蛇,也就是蛇尾!
document.getElementById('kj').removeChild(this.food);
this.num++;
this.createfood();
}
else
{
document.getElementById('kj').removeChild(this.p[0]);
}
this.createshe(w,h);
this.tt=setTimeout("snake.move()",this.st);
},
checkbody:function(a,b){//检查蛇是否碰到自身的方法
this.p=document.getElementsByTagName("p");
for(var i=0,j=this.p.length;i<j;i++)
{
if(this.p[i].style.left==a+"px"&&this.p[i].style.top==b+"px")return true;
}
}
}
window.onload=function(){
snake.start();
}
</script>
解决方案 »
- 怪事都让我碰上了!JS问题
- 怎么才能让$.getScript只在没有载入过这个js文件的情况下加载JS?
- 抓狂当中....怎么获得input标签在页面上的绝对坐标
- Opera浏览器打印iframe内容时不正常;
- 自动转换图片的问题!高手们指教下如何解决!
- 有关checkbox 选中给 一个隐藏的文本框赋值, 然后用document.form1.cannum.value 取值 结果有时候取到了undefined,有的时候正确的
- 怎么样让一个单选按钮可以控制两个下拉列表框的显示属性。
- 求一本中文版的JavaScript手册
- 如何限定 文本框 输入格式为日期形式(23-01-2003等)
- 树形结构
- javascript取中文的unicode值
- 求助:JS代码问题,郁闷死我了????
我提了一个建议,就是只用一个大的DIV作为大框,蛇和蛋都可以用绝对定位或相对定位的小DIV或SPAN来做。
现在看楼主这个,方法和我的建议一样。
整体代码也很短。一下也看不出有什么可以改得更好的。
要说简化代码,使JQ这样轻量级的框架当然是个好选择。不过这对效率又会有轻微影响,如果苛求的话,可以对JQ进行剪裁。但更主要的,是会影响本小游戏的独立性。
至于楼主说的蛇头,不知是什么意思,通过里面的W,H难道不可以得到蛇头坐标吗?
checkbody:function(a,b){//检查蛇是否碰到自身的方法
this.p=document.getElementsByTagName("p");
for(var i=0,j=this.p.length;i<j;i++)
{
if(i=j-1)this.p[i].style.background='purple';//非蛇头用紫色表示!但是这只能达到间接判断的效果,无法直接表示蛇头!
if(this.p[i].style.left==a+"px"&&this.p[i].style.top==b+"px")return true;
}
}
问题是如何直接设置蛇头的位置呢?
而且,你这样加上变色之后,这个函数就失效了。可以从中间的身体穿过去了。
如果先不说这个BUG,你可以通过和这个类似的方法,给蛇身的P设置自定义属性(不象这个颜色一样表现出来),需要的时候,在createshe之后判断一下P集里这个自定义属性,就能区别出蛇身和蛇头。
要增加和设置自定义属性,很简单:
假如创建oP是一个P元素后,只要用oP.自定义属性=5; 这样就可以了。过后就可以访问“oP.自定义属性”来得到你赋予的值。
反方向键盘依然在起作用。每按一次反方向键,MOVE方法里的代码就多执行一次,并不能彻底禁止!按多了就自然起到了加速度的作用,虽然前面用了禁止反方向键,但是只能禁止方向,不能禁止其加速的影响!这个大家可以去测试一下就知道了!
checkbody:function(a,b){//检查蛇是否碰到自身的方法
this.p=document.getElementsByTagName("p");
for(var i=0,j=this.p.length;i<j;i++)
{
if(i=j-1)this.p[i].style.background='purple';//非蛇头用紫色表示!但是这只能达到间接判断的效果,无法直接表示蛇头!
if(this.p[i].style.left==a+"px"&&this.p[i].style.top==b+"px")return true;
}
}
if(i=j-1)this.p[i].style.background='purple';//非蛇头用紫色表示!但是这只能达到间接判断的效果,无法直接表示蛇头!这句已经达到了效果,通过设置蛇身的紫颜色以区别于蛇头的红色!
而蛇头其实就是开始的时候去设置了:这句即可:
sna.style.cssText="position:absolute;margin:0;padding:0;width:20px;height:20px;background:red;border:1px solid #ccc;";
这个就是设置蛇头的,虽然看着别扭,但是确实已经达到效果!
现在关键是如何彻底禁止反方向键了!
<script type="text/javascript">
var snake={
st:500,//速度初始为0.5秒移动一次,数值越小速度越快!
num:0,
start:function(){//初始化,建立外围DIV框架,键盘事件,食物或蛇的初始数量和位置
div=document.createElement('div');
div.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:20px;width:400px;height:400px;border:1px solid #000;";
div.id='kj';
div1=document.createElement('div');
div1.style.cssText="position:absolute;margin:0;padding:0;left:300px;top:430px;width:400px;height:100px;";
document.body.appendChild(div);
document.body.appendChild(div1);
div1.innerHTML='键盘上↑↓←→代表方向控制,小键盘上0加速,1减速,空格暂停!';
document.onkeydown=function(e){
e=e||window.event;
snake.direction=Math.abs(snake.direction-e.keyCode)!=2?e.keyCode:snake.direction;
if(Math.abs(snake.direction-e.keyCode)==2)return false;//彻底禁止反方向键,这句是核心!
snake.dir();
}
this.createshe(0,200);
this.createshe(20,200);
this.createfood();
},
pause:function(){//游戏暂停
clearTimeout(this.tt);
},
createfood:function(){//产生选区内的一个随机坐标的食物(SPAN标签)!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
this.p=document.getElementsByTagName("p");//获得所有蛇对象
while(this.checkbody(this.x,this.y))
{//进行循环判断,让随即生成的食物位置不能和蛇的位置重合,重合就重刷随机数!
this.x=Math.round(Math.random()*19)*20;
this.y=Math.round(Math.random()*19)*20;
}
this.food=document.createElement("span");
this.food.style.cssText="position:absolute;width:20px;height:20px;background:green;border:1px solid #ccc;";
this.food.style.left=this.x+"px";
this.food.style.top=this.y+"px";
document.getElementById('kj').appendChild(this.food);
},
createshe:function(a,b){//用P标签创建蛇!
var sna=document.createElement("p");
sna.style.cssText="position:absolute;margin:0;padding:0;width:20px;height:20px;background:red;border:1px solid #ccc;";
sna.style.left=parseInt(a)+"px";
sna.style.top=parseInt(b)+"px";
this.x1=parseInt(a);
this.y1=parseInt(b);
document.getElementById('kj').appendChild(sna);
},
dir:function(){//方向控制
//var edir;
if(typeof(this.tt)!='undefined'){clearTimeout(this.tt);}//防止方向键多次键入导致速度不断增加!
//alert(snake.direction);
switch(snake.direction)
{
case 37:this.gox = -20;this.goy=0; this.move(); break;//左
case 39:this.gox = 20; this.goy=0; this.move(); break;//右
case 38:this.gox = 0; this.goy=-20; this.move(); break;//上
case 40:this.gox = 0; this.goy=20; this.move(); break;//下
case 32:this.pause(); break;//暂停
case 96:this.st=this.st-100;this.move();break;//加速
case 97:this.st=this.st+100;this.move();break;//减速
}
},
move:function(){//响应键盘进行移动的事件
w=this.x1+this.gox;//每吃一次食物获得一次坐标
h=this.y1+this.goy;
if(w<0||w>=400||h<0||h>=400||this.checkbody(w,h))//进行边界检测和自身的碰撞检测
{
clearTimeout(this.tt);
alert("Game over!吃了"+this.num+"个食物!");
window.location.reload();//游戏结束重新刷新页面!
}
if(this.x1==this.x&&this.y1==this.y)
{//判断蛇头是否遇到食物的位置,如果是就移除食物,重新生成,否则就是删除最开始建立的蛇,也就是蛇尾!
document.getElementById('kj').removeChild(this.food);
this.num++;
this.createfood();
}
else
{
document.getElementById('kj').removeChild(this.p[0]);
}
this.createshe(w,h);
this.tt=setTimeout("snake.dir()",this.st);
},
checkbody:function(a,b){//检查蛇是否碰到自身的方法
this.p=document.getElementsByTagName("p");
for(var i=0,j=this.p.length;i<j;i++)
{
if(this.p[i].style.left==a+"px"&&this.p[i].style.top==b+"px")return true;
}
}
}
window.onload=function(){
snake.start();
}
</script>