爱易网
IT新闻
IT新闻
爱易资讯
网站搭建
云虚拟主机教程
云服务器教程
Apache教程
IIS教程
Nginx教程
网站策划
站长文章
推广教程
淘宝客教程
网页设计
HTML教程
XHTML教程
CSS教程
HTML5教程
CSS3教程
JavaSript基础
JQuery教程
Node.js教程
前端技术
Ajax教程
Js特效
Xml教程
平面设计
页面UI设计
photoshop教程
程序开发
AI人工智能
Asp教程
Php教程
Asp.Net教程
Net Core教程
C#教程
Java教程
Jsp教程
开发技术
微信小程序教程
Uniapp开发教程
微信公众号开发
Andriod教程
IOS教程
DOS教程
Python教程
Docker教程
Windows Container教程
数据库
MSSQL教程
MySQL教程
Redis教程
Access教程
Oracle教程
数据库教程
操作系统
Linux教程
Windows教程
MAC教程
Cisco教程
交换机教程
防火墙教程
搜索
爱易网页
JS特效
js随机生成迷宫图游戏特效
js随机生成迷宫图游戏特效
日期:2014-05-18 浏览次数:20513 次
以下是程序代码
<html> <head> <title>Maze</title> <style> .maze_outer { position:absolute; left:20px; top:20px; } .maze_inner { position:relative; } .maze_cell { position:absolute; width:16px; height:16px; overflow:hidden; border:1px solid black; } </style> <script> window.onload = init; function init(){ var mz = new Maze(); mz.create(); document.body.appendChild(mz.toHTMLObject()); } function Maze(){ this.size = [40,40]; this.inited = false; this.init = function(){ this.nodes = []; for (var y=0; y<this.size[1]; y++){ this.nodes[y] = []; for (var x=0; x<this.size[0]; x++){ this.nodes[y][x] = new Node(this, x, y); } } this.inited = true; } this.get_node = function(x,y){ return this.inited && x >=0 && x < this.size[0] && y >= 0 && y < this.size[1] && this.nodes[y][x]; } this.create = function(){ if (!this.inited) this.init(); var try_count = 0; var points = []; var most_rightbottom_point = this.nodes[0][0]; //var max_p_len = 0; var cp; points.push(this.nodes[0][0]); while(points.length > 0){ var np = random_init_cell(cp = points.pop()); if (cp.x >= most_rightbottom_point.x && cp.y >= most_rightbottom_point.y) most_rightbottom_point = cp; for (var i=0; i<np.length; i++) points.unshift(np[i]); //if (max_p_len < points.length) max_p_len = points.length; if (points.length == 0 && !this.nodes[this.size[1]-1][this.size[0]-1].inited && ++try_count < 1000){ most_rightbottom_point.inited = false; points.push(most_rightbottom_point); } } //alert("最大并发路径数:" + max_p_len+"\r\n尝试次数:"+try_count); function random_init_cell(p){ if (p.inited) return []; var re = []; for (var i=0, dir; i<Maze.DIR.length && (dir = Maze.DIR[i]); i++){ if (p[dir]() && !p.dir[dir] && Math.random() < ratio(dir, p)){ p.open(dir); re.push(p[dir]()); } } p.inited = true; return re; } function ratio(dir, p){ var c = p.branches_count()*0.5 + p[dir]().branches_count() * 10; return Math.pow(0.4,c); } } this.toHTMLObject = function (){ var obj_outer = document.createElement("div"); obj_outer.className = "maze_outer"; var obj_inner = document.createElement("div"); obj_inner.className = "maze_inner"; obj_outer.appendChild(obj_inner); if (!this.inited) this.init(); for (var y=0; y<this.size[1]; y++){ for (var x=0; x<this.size[0]; x++){ var nd = document.createElement("div"); nd.className = "maze_cell"; nd.x = x; nd.y = y; nd.style.left = x * 15 + "px"; nd.style.top = y * 15 + "px"; for (var i in this.nodes[y][x].dir){ if (this.nodes[y][x].dir[i]) nd.style["border" + i.charAt(0).toUpperCase() + i.substring(1)] = "1px solid white"; } obj_inner.appendChild(nd); } } return obj_outer; } } Maze.DIR = ["top", "left", "bottom", "right"]; function Node(maze, x, y){ this.maze = maze; this.x = x || 0; this.y = y || 0; this.dir = {}; this.inited = false; this.left = function(){ return this.maze.get_node(x-1, y); } this.right = function(){ return this.maze.get_node(x+1, y); } this.top = function(){ return this.maze.get_node(x, y-1); } this.bottom = function(){ return this.maze.get_node(x, y+1); } this.open = function(d){ if (d == "top"){ this.dir.top = this.top().dir.bottom = true; }else if (d == "right"){ this.dir.right = this.right().dir.left = true; }else if (d == "bottom"){ this.dir.bottom = this.bottom().dir.top = true; }else if (d == "left
上一篇:狡猾的按钮特效
下一篇:通过JS控制广告图片仅在特定范围内漂浮
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
京东确定IPO定价区间 最高募资16.9亿美元
亚马逊推特价闪购纪念入华十周年 总裁一职仍空缺
云储存有多远:跨过数据安全问题即可到达
挂号网高管团体离任 或因投资人失掉决计
618首日看京东阿里不同策略,鹿死谁手?
广电总局回应美剧下架:不是所有信息可公开
苹果CEO抱歉解读:是战略而非“屈服”
首个释放出搅局房地产信号的互联网公司不是小米而是58同城
雅安地震 绝不宽恕应用人们爱心的骗子!
推荐阅读
更多>
三星手机在国内陷入困境:华为中兴小米紧追
看,网络文学今日之繁盛;忆,纯真去哪了?
空气污染器市场乱象丛生 选购需慎重
英特尔技术高管离任 电视业务或受影响
新闻早8点》Facebook失手发布Snapchat类应用
华为任正非:家族成员不会成为接班人
中移动一季度净利大降:手机补贴增长过快
为啥苹果定要出廉价iPhone?都是安卓逼的
京东联席董事长赵国庆上市前夕离职
人们为什么总愿尝试新社交网络?
支付宝被曝严重破绽 隐私平安再受质疑
中移动“重启”自有品牌手机成败难料
马云寄厚望于物流 “菜鸟”行情暂难持续
FB上广告费用昂贵而且效果不及Twitter
从WPS到office365,中国企业能否赶超美国?
网友爆料称微软雇水军宣传主机Xbox One
中移动本月推手机钱包 iPhone遭边缘化
牟贵先挂帅 国美在线“价钱战”昔日开打
杨元庆去年总薪酬1.33亿元
谷歌新技术进入iOS平台:将叫板苹果Siri