爱易网
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 浏览次数:20283 次
以下是程序代码
<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控制广告图片仅在特定范围内漂浮
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
传运营商高层将变化 专家称需体制革新
传苹果已告知鸿海:两款新iPhone下月发货
同程旅游融资后变土豪 如何烧钱占领市场?
神州电脑中止IPO面前:再被质疑财务造假
聚美向左,唯品向右,怎能耐得住高跟和红唇的诱惑!
陈天桥之鉴:产品创新被扼杀那些事
传360周鸿祎曾试图阻止百度收购91无线
阿朗力避步北电后尘 急切停止自我革新
虚拟运营市场躁动:山西煤老板兴味浓重
推荐阅读
更多>
易迅大众点评缩水执行“七天后悔权”被约谈
韩国研制无线充电系统:一次可充40部手机
玩游戏式的趣味戒烟App:Kwit
MIUI,有了它小米就无惧对手
京东确定IPO定价区间 最高募资16.9亿美元
眼球识别技术EyeVerify融资600万美元 360参投
优酷土豆兼并一年:依然处于烧钱形状
猎豹移动上市押海外市场 与小米有合作空间
神奇约炮应用被禁 改名后重返苹果应用商店
携程收买酷讯成定局:转型互联网+投资
中国雅虎邮箱今日关停 由盛及衰的背后
独立工具APP:为亿级用户的商业化而愁
全球网络遭史上最大规模攻击:攻击数据每秒300G
固话上网卡或将实名制 运营商泄露用户信息将被罚
传陌陌或12月赴美IPO 估值约30亿美元
iPhone 6 Plus要召回是谣言
摩托罗拉移动裁员关闭德州Moto X手机工厂
苹果20年以来首次发债 融资170亿美元
TD是完成中国梦的一个契机
酷派首抓4G时机大丰收,未来份额必降无疑