1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| <script type="text/javascript"> var x = 30, y = 16, zd = 99, xy = x * y; var M = {}; var falg = false, start = true; $(function () { myclock();
var k = 1, t = zd, bool, count = 0; var table = '<table border="1">'; for (var i = 0; i < y; i++) { table += "<tr>" for (var j = 0; j < x; j++) { var num = Math.random() * xy; num = parseInt(num, 10); if (num > t) { bool = k;//普通 xy--; } else { bool = -k;//雷 t--; count++; } table += "<td id='" + (bool) + "'></td>"; k++; }; table += "</tr>" }; table += "</table>"; $("table").append(table); $("td").addClass("load");
/*给所有方块注册鼠标进入事件,将背景变成黄色*/ $("td").mousemove(function () { if ($(this).attr("class") == "load") { $(this).addClass("move"); } });
/*给所有方块注册鼠标移出事件,去除鼠标进入时的样式*/ $("td").mouseout(function () { $(this).removeClass("move"); });
$("td").mousedown(function (e) { if (falg == true) { M.dialog13 = jqueryAlert({ 'icon': './img/warning.png', 'content': '踩到雷了,游戏结束!', 'closeTime': 2000, }); return false; };
if (start == false) { M.dialog13 = jqueryAlert({ 'icon': './img/error.png', 'content': '请先点开始游戏!', 'closeTime': 2000, }); return false; }
console.log(e.which); //右键为3 if (3 == e.which) { var t = this.id;/*获取单击块的id*/ //右键的三种状态切换 if ($(this).attr("class").indexOf("qizi") >= 0) { $(this).removeClass("qizi"); $(this).addClass("click"); } else if ($(this).attr("class").indexOf("load") >= 0) { $(this).removeClass("load"); $(this).addClass("qizi"); } else if ($(this).attr("class").indexOf("click") >= 0) { $(this).removeClass("click"); $(this).addClass("load"); } } else if (1 == e.which) {//左键为1 /*获取单击块的id*/ var t = this.id;
//当t>0时不为雷,计算周围有几个雷,显示数字 if (t > 0) { if ($(this).attr("class").indexOf("qizi") < 0) { $(this).removeClass("load"); $(this).removeClass("move");
//计算周围有几个雷 var sum = checkCount(t); $(this).text(sum); } } else {
if ($(this).attr("class").indexOf("qizi") < 0) { //当t<0时,表示点到了炸弹,引爆所有的炸弹 $(this).removeClass("load"); $(this).addClass("zhadang"); falg = true; M.dialog13 = jqueryAlert({ 'icon': './img/warning.png', 'content': '踩到雷了,游戏结束!', 'closeTime': 2000, }); //引爆.....所有炸弹 bobobo(); start = false; myclock = null; } } } });
//阻止浏览器默认右键点击事件 $("table").bind("contextmenu", function () { return false; }); ////开始游戏, $("#btnStart").click(function () { location.reload(); }) });
//计算周围有几颗雷 function checkCount(id) { //$("#clickLenard").append('<p>鼠标左键点击了:' + id + '</p>');
var arr = [-x - 1, -x, -x + 1, -1, 1, x - 1, x, x + 1]; var sum = 0; for (var i = 0; i < arr.length; i++) { var temp = (parseInt(id) + arr[i]); if (temp <= 0) continue; if (temp > x * y) continue; if (id % x == 1 && (i == 0 || i == 3 || i == 5)) continue; if (id % x == 0 && (i == 2 || i == 4 || i == 7)) continue; var a = $("#" + temp).length > 0 ? 0 : 1; sum += a; //$("#clickLenard").append('<p>周围:' + temp + "-->>" + a + "" + '</p>'); } return sum; }
//显示所有的地雷 function bobobo() { for (var i = 1; i <= x * y;) { var id = "#-" + i; $(id).addClass("zhadang"); i++ } }
/*计时开始*/ var times; function myclock() { var t = $("#time").val(); t = t.substring(0, t.length - 2); t = 1 + Number(t) + 'ms'; $("#time").attr('value', t); times = setTimeout("myclock()", 100); } </script>
|