`
guoxuepeng
  • 浏览: 38597 次
文章分类
社区版块
存档分类
最新评论

纯JS扫雷游戏(各浏览器兼容)

 
阅读更多

纯JS扫雷游戏 (各浏览器兼容)

效果图:


HTML源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title>扫雷</title>
<style type="text/css">
#container {
	width: 670px;
	height: 670px;
	background-color: #eeffee;
	margin: 10px auto;
}

.block {
	width: 20px;
	height: 20px;
	background-color: white;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.block2 {
	width: 10px;
	height: 10px;
	background-color: red;
	float: left;
}

.openedBlockNormal {
	width: 20px;
	height: 20px;
	background-color: green;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.openedBlockBomb {
	width: 20px;
	height: 20px;
	background-color: red;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}

.openedBlockEmpty {
	width: 20px;
	height: 20px;
	background-color: yellow;
	border: 1px solid;
	float: left;
	margin: 0 0 0 0px;
	text-align: center;
}
</style>
<script type="text/javascript">
	var EventUtil = new Object;
	EventUtil.addEvent = function(oTarget, sEventType, funName) {
		if (oTarget.addEventListener) {//for DOM;
			oTarget.addEventListener(sEventType, funName, false);
		} else if (oTarget.attachEvent) {
			oTarget.attachEvent("on" + sEventType, funName);
		} else {
			oTarget["on" + sEventType] = funName;
		}
	};
	EventUtil.removeEvent = function(oTarget, sEventType, funName) {
		if (oTarget.removeEventListener) {//for DOM;
			oTarget.removeEventListener(sEventType, funName, false);
		} else if (oTarget.detachEvent) {
			oTarget.detachEvent("on" + sEventType, funName);
		} else {
			oTarget["on" + sEventType] = null;
		}
	};
</script>
<script type="text/javascript" src="bomb.js"></script>
</head>
<body>

	<div id="containers" style="cursor: pointer"></div>
</body>

<script>
	// this class stands for eight direction of every block.
	function Direction() {
		this.up = null;
		this.right = null;
		this.down = null;
		this.left = null;
		this.leftUp = null;
		this.rightUp = null;
		this.leftDown = null;
		this.rightDown = null;
	}
	// every block object stands for one block in the page.
	function Block(className) {
		// blocks around.
		this.neighbors = new Direction();// [up,rightUp,right,rightDown,down,leftDown,left,leftUp]
		// html element. div used here.
		this.html = null;
		// position of this block.
		this.index = -1;
		// is this block a bomb?
		this.isBomb = false;
		// how many bombs around of this blocks.
		this.bombNumAround = -1;
		// html css style
		this.className = className;
		// is it already opened?
		this.isOpen = false;
		// do some preparation for this block;
		this.init();
	}
	// calculate how many bombs of around.
	Block.prototype.calcBombAround = function() {
		if (!this.isBomb) {
			var bombNumber = 0;
			for ( var p in this.neighbors) {
				if (typeof (this.neighbors[p]) != "function") {
					if (null != this.neighbors[p] && this.neighbors[p].isBomb) {
						bombNumber++;
					}

				}
			}
			this.bombNumAround = bombNumber;
		}
	};
	// return html element.
	Block.prototype.getHtml = function() {
		return this.html;
	};
	Block.prototype.init = function() {
		var that = this;
		// create a html element.
		this.html = document.createElement("div");
		// adding mouse over handler for this block. change background color for
		// this block.
		EventUtil.addEvent(this.html, "mouseover", function(evt) {
			var element = evt.target ? evt.target : evt.srcElement;
			element.style.backgroundColor = "#eeAB6F";
		});
		// remove the style which were added in mouseover handler.
		EventUtil.addEvent(this.html, "mouseout", function(evt) {
			var element = evt.target ? evt.target : evt.srcElement;
			element.style.backgroundColor = "";
			element.style.cursor = "";
		});
		// user click some block.
		EventUtil.addEvent(this.html, "mousedown", function(evt) {
			// right click button.
			if (evt.button == 2) {
				// if this block already indicate as a bomb, then change to normal
				// block.
				if (that.getStyle() == "openedBlockBomb") {
					that.changeStyle("block");
				} else if (that.getStyle() == "block") {
					// if this block is a normal block, indicate as a bomb.
					that.changeStyle("openedBlockBomb");
				}
			} else {// left click!
				// open it
				that.open();
			}

		});

		// setting defalut style name.
		this.changeStyle(this.className);
	};
	// change css style.
	Block.prototype.changeStyle = function(styleName) {
		this.html.setAttribute("class", styleName);
		this.html.setAttribute("className", styleName);
	};
	// getting csss style
	Block.prototype.getStyle = function() {
		var style = this.html.getAttribute("class");
		if (style == null || typeof (style) == "undefined") {
			style = this.html.getAttribute("className");
		}
		return style;
	};
	Block.prototype.open = function() {
		// if there is no bomb around, change style as an empty block.
		if (this.bombNumAround == 0) {
			this.changeStyle("openedBlockEmpty");
		} else if (this.bombNumAround > 0) {
			// setting bomb number to inner html.
			this.html.innerHTML = this.bombNumAround;
			this.changeStyle("openedBlockNormal");
		} else {
			// setting style as a bomb.
			this.changeStyle("openedBlockBomb");
			alert("bomb!!");
		}

		this.isOpen = true;
		// if 0 ==bomb number,them open other block around.
		if (this.bombNumAround == 0) {
			var directions = new Array();
			directions.push("up");
			directions.push("right");
			directions.push("down");
			directions.push("left");
			directions.push("leftUp");
			directions.push("rightUp");
			directions.push("leftDown");
			directions.push("rightDown");
			for ( var i = 0; i < directions.length; i++) {
				var blockInDirection = this.neighbors[directions[i]];
				if (null != blockInDirection
						&& typeof (blockInDirection) != "undefined"
						&& !blockInDirection.isBomb && !blockInDirection.isOpen) {
					blockInDirection.open();
				}
			}
		}
	};

	function Container(parent, width, heigth, rows, columns, bomb) {
		this.bombNumber = bomb;
		this.parent = parent;
		this.width = width;
		this.heigth = heigth;
		this.rows = rows;
		this.columns = columns;
		this.childObject = new Array();
		this.html = null;

	}
	Container.prototype.init = function() {
		this.html = document.createElement("div");
		this.html.id = "container";

		var bombArray = new Object();
		// how many bombs in all.
		var bombNumber = 100;
		var index = this.rows * this.columns + 1;
		var loopNumber = 0;
		// randomly generate the position of bomb.
		while (true) {
			if (loopNumber >= bombNumber) {
				break;
			}
			var randomBombPosition = Math.floor(Math.random() * index);
			if (bombArray[randomBombPosition] != true) {
				bombArray[randomBombPosition] = true;
				loopNumber++;
			}

		}
		// generate block objects.
		for ( var i = 0; i < this.rows * this.columns; i++) {
			var block = new Block("block");

			if (bombArray[i] == true) {
				block.isBomb = true;
			}

			this.childObject.push(block);
			this.html.appendChild(block.html);
		}
		// generating the relation of blocks. neighbors around.
		for ( var j = 0; j < this.rows * this.columns; j++) {
			block = this.childObject[j];
			block.neighbors.up = this.childObject[j - this.columns];
			block.neighbors.right = this.childObject[j + 1];
			block.neighbors.down = this.childObject[j + this.columns];
			block.neighbors.left = this.childObject[j - 1];
			block.neighbors.leftUp = this.childObject[j - this.columns - 1];
			block.neighbors.rightUp = this.childObject[j - this.columns + 1];
			block.neighbors.leftDown = this.childObject[j + this.columns - 1];
			block.neighbors.rightDown = this.childObject[j + this.columns + 1];
			if (j / this.columns == 0) {
				block.neighbors.up = null;
				block.neighbors.leftUp = null;
				block.neighbors.rightUp = null;
			} else if (j / this.columns == this.rows - 1) {
				block.neighbors.down = null;
				block.neighbors.leftDown = null;
				block.neighbors.rightDown = null;
			}
			if (j % this.columns == 0) {
				block.neighbors.left = null;
				block.neighbors.leftUp = null;
				block.neighbors.leftDown = null;
			} else if (j % this.columns == this.columns - 1) {
				block.neighbors.right = null;
				block.neighbors.rightUp = null;
				block.neighbors.rightDown = null;
			}
			block.calcBombAround();
		}

	};
</script>
<script type="text/javascript">
	document.oncontextmenu = function() {
		return false;
	};
	var parent = document.getElementById("containers");
	var container = new Container(parent, "600px", "600px", 30, 30, 0);
	container.init();
	parent.appendChild(container.html);
</script>

</html>


分享到:
评论

相关推荐

    JavaScript 编写的扫雷游戏 (具备Windows扫雷的全部功能)

    该软件完全用JavaScript编写,具有除扫雷英雄榜以外的全部Windows扫雷游戏的功能。 可选择初级、中级、高级以自定义雷区,兼容IE、Firefox及Safri浏览器, 有计时功能,鼠标右键可标记地雷。点击笑脸可重新开始...

    javaScript的扫雷代码

    JavaScript扫雷是一款基于Web的扫雷游戏,利用了JavaScript这门客户端脚本语言来实现。JavaScript,简称JS,是Web开发中的重要组成部分,主要用于网页和网络应用的动态化和交互性。在这里,我们主要探讨如何使用...

    web 版扫雷(源码) 浏览器直接运行, 仿真XP界面。

    3. **浏览器兼容性**: 由于使用了HTML和JavaScript,这个扫雷游戏应该能在大部分现代浏览器上运行,包括Chrome、Firefox、Safari和Edge等。然而,开发者需要注意确保代码兼容性,以支持不同浏览器的特性差异。 4. *...

    扫雷代码javascrip版(网页)

    10. **浏览器兼容性**:考虑到JavaScript代码运行在浏览器环境中,开发者可能已经考虑了不同浏览器的兼容性问题,以确保游戏在主流浏览器上都能正常运行。 11. **HTML和CSS**:虽然主要讨论JavaScript,但一个完整...

    扫雷小游戏,仅供娱乐游玩。

    【扫雷小游戏,仅供娱乐游玩】是一款利用纯CSS技术实现的网页版扫雷游戏,它以其独特的编程方式展示了HTML和CSS在游戏开发中的潜力。在这个项目中,我们不仅可以看到基本的HTML结构,还能深入理解CSS如何用于布局、...

    扫雷-小游戏-NAS-WebStation-HTML5

    压缩包内的文件名为“扫雷”,可以推测这可能包含的是游戏的主要代码文件或者是一个打包后的完整应用,包括HTML、CSS、JavaScript等资源,用于构建和运行扫雷游戏。这些文件可能还包括配置文件、图片、音频等游戏...

    min3d:基于 WebGL 的 3D 扫雷游戏益智游戏

    MIN3D是一款采用WebGL技术开发的3D扫雷游戏,它将经典的二维扫雷体验带入了三维空间,为玩家提供了更为丰富的视觉效果和挑战性。WebGL是一种基于OpenGL标准的JavaScript API,用于在任何兼容的浏览器中进行硬件加速...

    超级无聊的web扫雷

    工具方面,开发者可能使用预处理器如Sass或Less来编写更高效、可维护的CSS,或者使用Babel将ES6+代码转换为浏览器兼容的版本。除此之外,Webpack或Rollup这样的模块打包工具可以将分散的源码文件整合成一个或多个可...

    所有常见的javascript特效

    7. **系统检测类**:JavaScript能检测浏览器类型、版本、屏幕尺寸等,以实现跨平台兼容性或自适应布局。例如,`navigator.userAgent`可以获取浏览器信息。 8. **页面特效类**:这些包括页面加载动画、滚动效果、...

    Java Script从入门到精通

    六、浏览器兼容性和调试 1. 浏览器差异:了解主流浏览器(Chrome、Firefox、Safari、Edge等)之间的差异,以及如何处理兼容性问题。 2. 开发者工具:学会使用浏览器的开发者工具进行调试,包括查看元素、追踪网络...

    JavaScript特效代码

    在学习和使用这些JavaScript特效代码时,开发者需要注意兼容性问题,因为不同的浏览器可能对某些JavaScript特性支持程度不同。此外,优化性能也是关键,避免因过多的JavaScript代码导致页面加载缓慢。 为了更好地...

    HTML5锅打灰太狼网页版游戏.zip

    HTML5的核心优势在于其跨平台兼容性,能够无缝运行在不同的浏览器和设备上,无需插件支持。游戏可能采用了HTML5的Canvas元素,这是一个用于图形绘制的二维画布,开发者可以通过JavaScript来控制其上的像素,实现动态...

    React-MineSweeper:用React编写的MineSweeper

    开发人员使用`npm install`命令安装所有必要的依赖包,这通常包括React库、ReactDOM(用于将React组件渲染到DOM中)和其他辅助工具,如Webpack(模块打包工具)和Babel(用于转换ES6+语法,确保浏览器兼容性)。...

    java开源包1

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包11

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包2

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包3

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包6

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包5

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

    java开源包10

    Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页...

Global site tag (gtag.js) - Google Analytics