本文实例讲述了JS实现的走迷宫小游戏。分享给大家供大家参考,具体如下:
先来看看运行效果截图:
完整实例代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JS打造的走迷宫游戏</title> </head> <body> <SCRIPT> function ShowMenu(bMenu) { document.all.idFinder.style.display = (bMenu) "none" : "block" document.all.idMenu.style.display = (bMenu) "block" : "none" idML.className = (bMenu) "cOn" : "cOff" idRL.className = (bMenu) "cOff" : "cOn" return false } </SCRIPT> <STYLE> <!-- A.cOn {text-decoration:none;font-weight:bolder} #article {font: 12pt Verdana, geneva, arial, sans-serif; background: white; color: black; padding: 10pt 15pt 0 5pt} #article P.start {text-indent: 0pt} #article P {margin-top:0pt;font-size:10pt;text-indent:12pt} #article #author {margin-bottom:5pt;text-indent:0pt;font-style: italic} #pageList P {padding-top:10pt} #article H3 {font-weight:bold} #article DL, UL, OL {font-size: 10pt} --> </STYLE> <SCRIPT> <!-- function addList(url,desc) { if ((navigator.appName=="Netscape") || (parseInt(navigator.appVersion)>=4)) { var w=window.open("","_IDHTML_LIST_","top=0,left=0,width=475,height=150,history=no,menubar=no,status=no,resizable=no") var d=w.document if (!w._init) { d.open() d.write("<TITLE>Loading...</TITLE><EM>Loading...</EM>") d.close() w.opener=self window.status="Personal Assistant (Adding): " + desc } else { window.status=w.addOption(url,desc) w.focus() } } else alert("Your browser does not support the personal assistant.") return false } // --> </SCRIPT> <STYLE TYPE="text/css"> #board TD {width: 15pt; height: 15pt; font-size: 2pt; } TD.foot {font-size: 10pt;} #board TD.start {font-size: 8pt; border-left: 2px black solid; background:yellow; border-top: 2px black solid;text-align: center; color: red} #board TD.end {font-size: 8pt; text-align: center; color: green} #message {margin: 0pt; padding: 0pt; text-align: center} </STYLE> <SCRIPT LANGUAGE="JavaScript"> var maze = new Array() var sides = new Array("Border-Top", "Border-Right") for (var rows=0; rows<13; rows++) maze[rows] = new Array() maze[0][0] = new Array(1,1,1,1,1,1,1,1,1,1,1,1) maze[0][1] = new Array(0,0,1,0,1,0,0,0,0,1,0,1) maze[1][0] = new Array(1,0,0,0,1,0,1,1,1,0,1,1) maze[1][1] = new Array(0,1,1,0,0,1,1,0,0,1,0,1) maze[2][0] = new Array(1,0,1,0,1,0,0,1,1,0,1,1) maze[2][1] = new Array(0,0,0,0,1,1,1,0,0,0,0,1) maze[3][0] = new Array(0,1,1,1,1,1,0,0,0,0,1,1) maze[3][1] = new Array(1,0,0,1,0,0,0,1,1,0,0,1) maze[4][0] = new Array(0,0,0,0,0,0,1,1,1,1,1,1) maze[4][1] = new Array(1,1,1,1,1,0,0,0,0,0,1,1) maze[5][0] = new Array(0,0,0,0,1,0,1,1,1,1,0,0) maze[5][1] = new Array(1,1,1,1,1,1,0,0,0,1,0,1) maze[6][0] = new Array(0,0,0,0,0,0,1,1,0,1,0,1) maze[6][1] = new Array(1,1,1,1,1,1,0,0,0,1,0,1) maze[7][0] = new Array(1,0,1,0,0,0,1,0,1,1,0,1) maze[7][1] = new Array(1,1,1,0,1,0,0,1,0,1,1,1) maze[8][0] = new Array(0,0,0,1,0,0,1,1,0,0,0,0) maze[8][1] = new Array(0,1,0,1,1,0,0,0,1,1,0,1) maze[9][0] = new Array(0,0,0,0,0,1,1,1,1,0,1,1) maze[9][1] = new Array(1,1,1,1,0,0,0,0,0,1,1,1) maze[10][0] = new Array(0,0,0,0,0,1,1,1,1,1,0,0) maze[10][1] = new Array(1,1,1,0,1,0,0,0,0,1,0,1) maze[11][0] = new Array(0,0,1,1,1,1,1,1,1,0,0,0) maze[11][1] = new Array(1,0,1,0,0,0,0,0,0,0,1,1) maze[12][0] = new Array(0,0,0,0,0,1,1,1,1,0,1,0) maze[12][1] = new Array(1,1,0,1,0,0,0,1,0,0,1,1) function testNext(nxt) { if ((board.rows[start.rows].cells[start.cols].style.backgroundColor=="yellow") && (nxt.style.backgroundColor=='yellow')) { message.innerText="I see you changed your mind." board.rows[start.rows].cells[start.cols].style.backgroundColor="" return false } return true } function moveIt() { if (!progress) return switch (event.keyCode) { case 37: // left if (maze[start.rows][1][start.cols-1]==0) { if (testNext(board.rows[start.rows].cells[start.cols-1])) message.innerText="Going west..." start.cols-- document.all.board.rows[start.rows].cells[start.cols].style.backgroundColor="yellow" } else message.innerText="Ouch... you can't go west." break; case 38: // up if (maze[start.rows][0][start.cols]==0) { if (testNext(board.rows[start.rows-1].cells[start.cols])) message.innerText="Going north..." start.rows-- document.all.board.rows[start.rows].cells[start.cols].style.backgroundColor="yellow" } else message.innerText="Ouch... you can't go north." break; case 39: // right if (maze[start.rows][1][start.cols]==0) { if (testNext(board.rows[start.rows].cells[start.cols+1])) message.innerText="Going east..." start.cols++ document.all.board.rows[start.rows].cells[start.cols].style.backgroundColor="yellow" } else message.innerText="Ouch... you can't go east." break; case 40: //down if (maze[start.rows+1]==null) return if (maze[start.rows+1][0][start.cols]==0) { if (testNext(board.rows[start.rows+1].cells[start.cols])) message.innerText="Going south..." start.rows++ document.all.board.rows[start.rows].cells[start.cols].style.backgroundColor="yellow" } else message.innerText="Ouch... you can't go south." break; } if (document.all.board.rows[start.rows].cells[start.cols].innerText=="end") { message.innerText="You Win!" progress=false } } </SCRIPT> <P ALIGN=center>请使用键盘上的→←↑↓键进行游戏</P><BR> <p><TABLE ID=board ALIGN=CENTER CELLSPACING=0 CELLPADDING=0> <SCRIPT LANGUAGE="JavaScript"> for (var row = 0; row<maze.length; row++) { document.write("<TR>") for (var col = 0; col<maze[row][0].length; col++) { document.write("<TD STYLE='") for (var cell = 0; cell<2; cell++) { if (maze[row][cell][col]==1) document.write(sides[cell]+": 2px black solid;") } if ((0==col) && (0!=row)) document.write("border-left: 2px black solid;") if (row==maze.length-1) document.write("border-bottom: 2px black solid;") if ((0==row) && (0==col)) document.write(" background-color:yellow;' class=start>start</TD>") else if ((row==maze.length-1) && (col==maze[row][0].length-1)) document.write("' class=end>end</TD>") else document.write("'> </TD>") } document.write("</TR>") } var start = new Object start.rows = 0 start.cols = 0 progress=true document.onkeydown = moveIt; </SCRIPT> </TABLE> <P ID="message"> </P> <br /> </body> </html>
PS:这里再为大家推荐另一款本站的迷宫在线游戏供大家参考,同样是基于JS实现的:
在线迷宫小游戏:
http://tools.jb51.net/games/migong
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数据结构与算法技巧总结》、《JavaScript数学运算用法总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》及《JavaScript遍历算法与技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。
JS,走迷宫,小游戏
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]