代码驿站移动版
频道导航
HTML/Xhtml
CSS
JavaScript
HTML5
PHP教程
ASP.NET
正则表达式
AJAX
ThinkPHP
Yii
MySQL
MariaDB
Oracle
MongoDB
Redis
DedeCMS
PHPCMS
帝国CMS
WordPress
Discuz
其它CMS
Zend Studio
Sublime
Notepad
Dreamweaver
Windows
Linux
Nginx
Apache
IIS
CentOS
Ubuntu
Debian
网站优化
工具资源
PHP源码
ASP.NET源码
其它源码
图标素材
按钮素材
字体素材
DedeCMS模板
帝国CMS模板
PHPCMS模板
WordPress模板
Discuz!模板
单页模板
开发软件下载
服务器软件下载
广告投放
联系我们
版权申明
软件编程
网页前端
移动开发
数据库
服务器
脚本语言
PHP代码
JAVA代码
Python代码
Android代码
当前位置:
主页
>
网页前端
>
JavaScript代码
>
javascript 实现的多浏览器支持的贪吃蛇webgame
时间:2021-05-06 09:40:29 | 栏目:
JavaScript代码
| 点击:次
写的太累了 ,写了3个小时!大家可以玩玩看
界面不美,尽管批
<!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=gb2312" /> <title>www.jb51.net 脚本之家 贪吃蛇</title> <style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; } #global { margin:auto; position: relative; width: 800px; height:500px; border: thick double #ff0000; background-color:#666666 } #global #p { background-color: #999999; position: absolute; height: 500px; width: 500px; left:0px; top:0px; border-right-width: thick; border-right-style: double; border-right-color: #FF0000; } #global #p #k { background-color: #0000FF; position: absolute; height: 25px; width: 25px; left:0px; top:0px; } #global #p .xin { position: absolute; height: 25px; width: 25px; } --> </style> <script language="javascript" type="text/javascript"> //获取新生元素位置 var bucunzai=true;//记录新元素是不是移动 var xinkx; var xinky; //页面载入后k元素定位 function lo(){ var objectp=document.getElementById("k") objectp.style.left="250px" objectp.style.top="250px" //K元素最终样式代码 k_zz=objectp.currentStyle?document.getElementById("k").currentStyle:document.defaultView.getComputedStyle(objectp,null); } //记录位置的数组 var k_x=new Array() var k_y=new Array() //记录蛇的长度 var k_number=0 //方向键函数 document.onkeydown=pmove; var fx=0//获取上一次的按键直 function pmove(){ if(bucunzai){ xin(); bucunzai=false; } objectp=document.getElementById("k") kcode=window.event?window.event:arguments[0]; if(kcode.keyCode==39&&fx!=39&&fx!=37){ fx=kcode.keyCode; try{ clearInterval(kbottom); } catch(e){ } try{ clearInterval(kright); } catch(e){ } try{ clearInterval(ktop); } catch(e){ } try{ clearInterval(kleft); } catch(e){ } kleft=setInterval(function(){if(parseInt(objectp.style.left)>=475){alert("超出了");clearInterval(kleft);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]=k_zz.top;objectp.style.left=(parseInt(objectp.style.left)+25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) } if(kcode.keyCode==37&&fx!=39&&fx!=37){ fx=kcode.keyCode; try{ clearInterval(kbottom); } catch(e){ } try{ clearInterval(kright); } catch(e){ } try{ clearInterval(ktop); } catch(e){ } try{ clearInterval(kleft); } catch(e){ } kright=setInterval(function(){if(parseInt(objectp.style.left)<=0){alert("超出了");clearInterval(kright);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]=k_zz.top;objectp.style.left=(parseInt(objectp.style.left)-25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) } if(kcode.keyCode==40&&fx!=40&&fx!=38){ fx=kcode.keyCode; try{ clearInterval(kbottom); } catch(e){ } try{ clearInterval(kright); } catch(e){ } try{ clearInterval(ktop); } catch(e){ } try{ clearInterval(kleft); } catch(e){ } kbottom=setInterval(function(){if(parseInt(objectp.style.top)>=475){alert("超出了");clearInterval(kbottom);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]=k_zz.top;objectp.style.top=(parseInt(objectp.style.top)+25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) } if(kcode.keyCode==38&&fx!=40&&fx!=38){ fx=kcode.keyCode; try{ clearInterval(kbottom); } catch(e){ } try{ clearInterval(kright); } catch(e){ } try{ clearInterval(ktop); } catch(e){ } try{ clearInterval(kleft); } catch(e){ } ktop=setInterval(function(){if(parseInt(objectp.style.top)<=0){alert("超出了");clearInterval(ktop);}else{if(k_x.length>=400){k_x.shift();k_y.shift();};k_x[k_x.length]=k_zz.left;k_y[k_y.length]=k_zz.top;objectp.style.top=(parseInt(objectp.style.top)-25)+"px";}if(k_zz.left==xinkx&&k_zz.top==xinky){k_number++;xin();}k_kmove();},100) } } //生成新的元素代码 function xin(){ var xinkleft=parseInt(Math.random()*20)*25+"px"; var xinktop=parseInt(Math.random()*20)*25+"px"; var xinkcolor1=parseInt(Math.random()*14).toString(16) var xinkcolor2=parseInt(Math.random()*14).toString(16) var xinkcolor3=parseInt(Math.random()*14).toString(16) var xinkcolor4=parseInt(Math.random()*14).toString(16) var xinkcolor5=parseInt(Math.random()*14).toString(16) var xinkcolor6=parseInt(Math.random()*14).toString(16) var xink=document.createElement("div") xink.id="k"+k_number xink.className="xin" xink.style.left=xinkleft xink.style.top=xinktop xink.style.backgroundColor="#"+xinkcolor1+xinkcolor2+xinkcolor3+xinkcolor4+xinkcolor5+xinkcolor6 var pp=document.getElementById("p") pp.appendChild(xink) xinkx=xinkleft xinky=xinktop } //新的元素跟随移动函数 function k_kmove(){ if(k_number==0){} else{ for(var i=0;i<k_number;i++){ var move=document.getElementById("k"+i) move.style.top=k_y[k_y.length-(i+1)] move.style.left=k_x[k_x.length-(i+1)] } } } </script> </head> <body onload="lo();"> <div id="global"> <div id="p" style="top:0px; left:0px;"> <div id="k"></div> </div> </div> </body> </html>
[Ctrl+A 全选 注:
引入外部Js需再刷新一下页面才能执行
]
您可能感兴趣的文章:
JavaScript常见事件处理程序实例总结
Javascript的匿名函数小结
url传递的参数值中包含&时,url自动截断问题的解决方法
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
兼容浏览器的js事件绑定函数(详解)
相关文章
10-19
javascript上下左右定时滚动插件
11-23
Webpack中loader打包各种文件的方法实例
11-27
Javascript实例教程(19) 使用HoTMetal(2)
10-05
javascript判断图片是否加载完成的方法推荐
11-22
在javascript中执行任意html代码的方法示例解读
JQuery
VUE
AngularJS
MSSql
MySQL
MongoDB
Redis
Linux
Tomcat
Nginx
网站首页
广告投放
联系我们
版权申明
联系站长