JavaScript学习笔记:迷宫游戏

10 阅读 作者:howard2005 2020-04-30

一、游戏运行效果

在这里插入图片描述

二、实现步骤

1、在HBuilder里创建项目MazeGame,添加maze.html

在这里插入图片描述

2、在脚本里创建迷宫数组用于设置单元格顶边与右边

<script type="text/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);
</script>

迷宫有13行,12列。
maze[0] —— 表示第1行
maze[0][0] ——用于设置第1行单元格的顶边
maze[0][1] ——用于设置第1行单元格的右边
maze[0][0][i] ——第1行第(i+1)个单元格的顶边,值为1——有顶边,值为0——无顶边
maze[0][1][i] ——第1行第(i+1)个单元格的右边,值为1——有右边,值为0——无右边

3、在body里绘制迷宫

<body>
	<p align=center style="font-weight: bold; font-size: 20px;">Use →, ←, ↑ and ↓ Keys to Play the G
	<table id='board' align='center' cellspacing=0 cellpadding=0>
		<script type="text/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" style="margin-top: 20px; font-size: 15px; color: blue; font-weight: bold;"></p>
</body>

按键事件会调用moveIt()方法。
此时,在浏览器里运行,效果如下:
在这里插入图片描述

4、设置迷宫样式

<style type="text/css">
	#board td {
		width: 25pt;
		height: 25pt;			
		color: red;
		text-align: center;
	}			
	#board td.start {
		font-size: 8pt;
		font-weight: bold;
		border-left: 2px black solid;
		background:  #9DA5C3;
		border-top: 2px black solid;
		text-align: center;
		color: red;
	}
	#board td.end {
		font-size: 8pt;
		font-weight: bold;
		text-align: center;
		color: green;
	}
	#message {
		margin: 0pt;
		padding: 0pt;
		text-align: center;
	}
</style>

此时,在浏览器里运行,效果如下:
在这里插入图片描述

5、在脚本里添加尝试下一步方法tryNext()

function tryNext(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;
			}

如果走错了,允许玩家反悔。

6、在脚本里添加响应按键事件的处理方法moveIt()

function moveIt() {
	if (progress) {
		switch (event.keyCode) {
			case 37: // Left
				if (maze[start.rows][1][start.cols - 1] == 0) {
					if (tryNext(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 (tryNext(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 (tryNext(board.rows[start.rows]原文地址:https://blog.csdn.net/howard2005/article/details/103462484
                        
广告一下
热门教程
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0
相关文章
【译】JavaScript数据结构(3):单向链表与双向链表 16
10个JavaScript难点 16
【译】苹果拒绝支持PWA,有损Web的未来 16
iView 一周年了,同时发布了 2.0 正式版,但这只是开始... 16
nodejs+mongodb构建一个简单登录注册功能 16
【译】JavaScript数据结构(4):树 16
组件化开发与黑箱 16
TypeScript - 不止稳,而且快 16
webpack3+anujs+ReactCSSTransitionGroup 16
原生js实现图片放大镜效果 16
WEB缓存探究第二弹——实战 16
纯笔记:vfork 的一些使用场景(顺便讲一下 fork 的原理) 16
Android APP 内部捐赠实现(支付宝&amp;微信) 16
WKWebView 的一些小总结 16
模型评价(一) AUC大法 16
开始使用GraphQL 16
Webpack模块化原理简析 16
gulp使用问题记录 16
使用Angular4动画为页面添彩 16
Python27 Matplotlib (win64 python2.7) 安装及简单使用 16