爱易网
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 浏览次数:20483 次
以下是程序代码
<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控制广告图片仅在特定范围内漂浮
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
阿里巴巴450亿勾画“现代服务业”藏宝图
黑狐病毒疯传播 电脑管家首推 专杀工具
赴美开店,阿里11Main准备好了吗?
支付宝被曝严重破绽 隐私平安再受质疑
云储存有多远:跨过数据安全问题即可到达
凑足了升级ios8所需要的5.7G空间,没料到“苹果”却变成“板砖”,iOS8还能一起愉快玩耍吗?
惠普董事长莱恩辞职 为巨额财务减记埋单
阿里物流搭上邮政:京东,我们在终点等你!
中国移动承诺再降4G资费 网络漫游至50个国家
推荐阅读
更多>
中国电信内外受困 去电信化思绪待改
支付宝全面封杀比特币:充值码淘宝店或遭封禁
充斥着假消息负能量的“秘密”缺乏普适性
电信业营改增今起实施 运营商短期利润骤降
2019双12阿里云老用户怎么参加拼团活动 老用户拼团方法
谷歌董事长成花花公子:情史足以媲美涂鸦
只有切到刚需痛点,漫画APP才能走下去
谷歌欲收买WhatsApp添加砝码 对方否认
2013年至今中国互联网行业十大并购案汇总
国内首个大数据系列指数今日发布
微软确认 Surface 计划更多尺寸和LTE 版本
三星手机在国内陷入困境:华为中兴小米紧追
墨迹天气:产品创新以攻为守方能对抗巨头
唯品会荣登“最贵中概股”的欢喜与哀愁
传搜狗估值12亿美元 与360谈判进入白热化
巨人网络回应央视 否认应用黑客停止诈骗
广东移动高层受调查 中移动或现更大反腐风暴
富士康欲用机器人产iPhone 6
“组合拳”怎么打 拆解小米新玩法
顺丰O2O便利店将正式启动:涉及金融服务