JS扫雷游戏
JS扫雷游戏 (各浏览器兼容)
效果图:
HTML源码:
[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) {
补充:web前端 , JavaScript ,