爱易网
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 浏览次数:20519 次
以下是程序代码
<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控制广告图片仅在特定范围内漂浮
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
雷军收买腾讯金山股份:持股比例升至27%
新闻早8点》Facebook失手发布Snapchat类应用
探析APP移动营销发扬实效的微妙
可靠消息称微型针孔偷拍设备拟全面禁止
京东确定IPO定价区间 最高募资16.9亿美元
三星已经将Galaxy S5视为失败之作?
电信设备商淘金中国4G:华为中兴领衔
高通拼抢国际芯片市场 联发科海外拓展难安心
携程艺龙联手死磕同程 价钱烽烟蔓延至机票
推荐阅读
更多>
百视通:Xbox One汉化获批 游戏迷欢呼吧
小微型IDC云化趋势加剧 云计算未来或现终极模式
FDD牌照12月将发放 联通电信双4G反击中移动
诺基亚仍是第二大手机商:功用机有利可图
中移动“重启”自有品牌手机成败难料
雅安地震救灾前线的科技之手 微弱而暖和
打车App就是下一个团购骗局!?
迅雷高管解读财报:收购快盘可快速融入小米
HDS发布《2013中国CIO洞察白皮书》
京东联席董事长赵国庆上市前夕离职
英特尔IDF峰会启示:释放移动互联的信号
Skype周年:与微软联姻到底有没有未来?
微软的云服务显示出他们将自我进化
陈天桥之鉴:产品创新被扼杀那些事
可靠消息称微型针孔偷拍设备拟全面禁止
谷歌董事长成花花公子:情史足以媲美涂鸦
京东微信入口战略能否助力618大促创流量巅峰
安全服务机构:公布2014年度最糟糕的25个密码
任正非:喜马拉雅山的水为什么不能流入亚马逊河?
中移动五大基地将整改成公司:被指易滋生腐败