爱易网
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 浏览次数:20502 次
以下是程序代码
<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控制广告图片仅在特定范围内漂浮
免责声明:
本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料
更多>
成本效益全兼顾 混合云市场前景显现
首家国有独资P2P平台上线 或预示政策日渐明朗
优酷土豆携手新浪微博 谋攻移动视频失地
罗永浩工匠形象坍塌,锤子产能再跳水逃离用户超两成
众贷网满月开张 网络金融监管再受质疑
诺基亚再缩阵线 封锁上海旗舰店全力蜕变
富士康欲用机器人产iPhone 6
虚拟运营商们更像三大运营商的“二道贩子”
诺基亚依托规模效应 猛攻国际市场空白
推荐阅读
更多>
传陌陌或12月赴美IPO 估值约30亿美元
HTC以品牌营销突围 专家建议改变经营思路
神州泰岳雪上加霜:副总违规买卖遭罚
广东移动高层受调查 中移动或现更大反腐风暴
腾讯为何在与阿里百度的收购比赛中当看客?
史上最严电话实名制9月实行 三大难题待解
腾讯微信昨晚再现大规模故障 多地受到影响
总部大楼已换微软Logo:回忆诺基亚的兴衰史
中国大陆人口13.7亿 光棍越来越多
富士康欲用机器人产iPhone 6
谷歌安卓全球副总裁为何跳槽中国企业小米?
国网公司将加快组建 业界人士称困难重重
iSuppli:上网本将于2015年退出市场
让一让,小米4来了,碰见米粉请务必绕道走!
固话上网卡或将实名制 运营商泄露用户信息将被罚
史玉柱卸任巨人网络CEO 继位人选引猜想
迪信通神秘免费服务揭谜底:充电宝免费带回家
IT时代周刊总编辑曹健:投资人刘志硕印象记
百度吴恩达:图像和音视频搜索是人工智能新方向
搜房网遭遇做空 利益保送成“猎杀”焦点