视频1
视频21
视频41
视频61
视频文章1
视频文章21
视频文章41
视频文章61
推荐1
推荐3
推荐5
推荐7
推荐9
推荐11
推荐13
推荐15
推荐17
推荐19
推荐21
推荐23
推荐25
推荐27
推荐29
推荐31
推荐33
推荐35
推荐37
推荐39
推荐41
推荐43
推荐45
推荐47
推荐49
关键词1
关键词101
关键词201
关键词301
关键词401
关键词501
关键词601
关键词701
关键词801
关键词901
关键词1001
关键词1101
关键词1201
关键词1301
关键词1401
关键词1501
关键词1601
关键词1701
关键词1801
关键词1901
视频扩展1
视频扩展6
视频扩展11
视频扩展16
文章1
文章201
文章401
文章601
文章801
文章1001
资讯1
资讯501
资讯1001
资讯1501
标签1
标签501
标签1001
关键词1
关键词501
关键词1001
关键词1501
专题2001
全部频道
首页
科技
教育
生活
旅游
时尚
美容
美食
健康
体育
游戏
汽车
元宇宙
家电
财经
综合
宠物
javascript扫雷游戏_javascript技巧
2020-11-27 20:42:41
责编:小采
点击下载本文
文档为doc格式
"); for (var i = 0; i < rownum; i++) { buffer.append("
"); for(var j = 0; j < colnum; j++) buffer.append("
"); buffer.append("
"); } buffer.append(""); var workarea = document.getElementById("workarea"); workarea.innerHTML = buffer.toString(); mine(); // lay mines at the work area startTick(); // starting time } // periodically update the elapsed time function displayelapsedTime() { elapseSpan.innerHTML = elapseTime++; if (elapseTime > 999) stopTick(); } // start up the timer for sweeping mine function startTick() { if (tick) stopTick(); elapseTime = 0; statedNum = 0; elapseSpan = document.getElementById("elapse"); document.getElementById("message").innerHTML = ""; tick = setInterval(displayelapsedTime, 1000); // start tick } // stop the timer function stopTick() { clearInterval(tick); tick = null; } // lay mines at the work area function mine() { var arr = new Array(totalCellNum); for(var i = 0; i < totalCellNum; i++) arr[i] = i; for(var i = 0; i < minenum; i++) { var suffix = Math.floor(arr.length * Math.random()); var rnd = arr[suffix]; var row = Math.floor(rnd / colnum); var col = rnd % colnum; var cell = document.getElementById(row + "_" + col); cell.mine = "●"; arr.splice(suffix, 1); } calcMineForAll(); } // calculating mine number for each cell function calcMineForAll() { for(var i = 0; i < rownum; i++) { for(var j = 0; j < colnum; j++) { calcMine(i, j); } } } // calculating mine number for given cell function calcMine(row, col) { var cell = document.getElementById(row + "_" + col); var nums = 0; if(row > 0) { if(col > 0) { if(document.getElementById((row - 1) + "_" + (col - 1)).mine) //top left nums++; } if(col < colnum - 1) { if(document.getElementById((row - 1) + "_" + (col + 1)).mine) //top right nums++; } if(document.getElementById((row - 1) + "_" + col).mine) // top nums++; } if(row < rownum - 1) { if(col > 0) { if(document.getElementById((row + 1) + "_" + (col - 1)).mine) //bottom left nums++; } if(col < colnum - 1) { if(document.getElementById((row + 1) + "_" + (col + 1)).mine) //bottom right nums++; } if(document.getElementById((row + 1) + "_" + col).mine) // bottom nums++; } if(col > 0) if(document.getElementById(row + "_" + (col - 1)).mine) //left nums++; if(col < colnum - 1) if(document.getElementById(row + "_" + (col + 1)).mine) //right nums++; //if(typeof(cell.mine) == "undefined") cell.nums = nums; } // count number of marked cell around given coordinate function countMarkNum(row, col) { var nums = 0; if(row > 0) { if(col > 0) { if(document.getElementById((row - 1) + "_" + (col - 1)).state == "mark") //top left nums++; } if(col < colnum - 1) { if(document.getElementById((row - 1) + "_" + (col + 1)).state == "mark") //top right nums++; } if(document.getElementById((row - 1) + "_" + col).state == "mark") // top nums++; } if(row < rownum -1) { if(col > 0) { if(document.getElementById((row + 1) + "_" + (col - 1)).state == "mark") //bottom left nums++; } if(col < colnum - 1) { if(document.getElementById((row + 1) + "_" + (col + 1)).state == "mark") //bottom right nums++; } if(document.getElementById((row + 1) + "_" + col).state == "mark") // bottom nums++; } if(col > 0) if(document.getElementById(row + "_" + (col - 1)).state == "mark") //left nums++; if(col < colnum - 1) if(document.getElementById(row + "_" + (col + 1)).state == "mark") //right nums++; return nums; } // click the cell function clickMe(cell) { if(typeof(cell.state) == "undefined") { if (cell.mine == "●") { // if incaution touch the mine, game immediate over gameover(cell); return; } if (cell.nums > 0) { cell.state = "up"; statedNum++; cell.className = "up"; cell.innerHTML = getColoredNum(cell.nums); } else { var a = cell.id.split("_"); var row = parseInt(a[0]); var col = parseInt(a[1]); disclose(row, col); } isWin(); } } // touch the mine, game over function gameover(element) { for(var i = 0; i < rownum; i++) { for(var j = 0; j < colnum; j++) { var cell = document.getElementById(i + "_" + j); if (cell.state == "up") continue; if (cell.mine) { if (typeof(cell.state) == "undefined") { cell.innerHTML = "●"; cell.style.cssText = "font-family:宋体"; } } } } element.state = "up"; element.innerHTML = "●"; element.style.cssText = "color:#AA0000;font-family:宋体;font-size:14px"; stopTick(); document.getElementById("message").innerHTML = "
Sorry, you lost the game!
"; } // get colored number, difference number will be colored difference color function getColoredNum(nums) { var color; switch(nums) { case 1: color = "#0000EE"; break; case 2: color = "#00AA00"; break; case 3: color = "#990000"; break; case 4: color = "#FF0000"; break; case 5: color = "#550000"; break; case 6: color = "#550055"; break; case 7: color = "#223366"; break; default: color = "#000000"; } return "
" + nums + "
"; } function disclose(row, col) { var cell = document.getElementById(row + "_" + col); if(cell && typeof(cell.mine) == "undefined" && typeof(cell.state) == "undefined") { cell.state = "up"; cell.className = "up"; statedNum++; if (cell.nums == 0) { if(row > 0) { disclose(row - 1, col); if(col > 0) // top left disclose(row - 1, col - 1); if(col < colnum -1) // top right disclose(row - 1, col + 1); } if(row < rownum - 1) { disclose(row + 1, col); if(col > 0) // bottom left disclose(row + 1, col - 1); if(col < colnum -1) // bottom right disclose(row + 1, col + 1); } if(col > 0) disclose(row, col - 1); // left if(col < colnum -1) disclose(row, col + 1); // right } else { cell.innerHTML = getColoredNum(cell.nums); } } } // mark or unmark a cell function markMe(cell) { if (cell.state == "mark") { cell.innerHTML = " "; cell.state = undefined; statedNum--; } else if (typeof(cell.state) == "undefined") { cell.innerHTML = "F"; cell.state = "mark"; statedNum++; isWin(); } } // determine the game is win function isWin() { if (statedNum == totalCellNum) { document.getElementById("message").innerHTML = "
Congratulations, you won the game!
"; stopTick(); } } // when left and right key down function doubleClick(cell) { if(typeof(cell.state) == "undefined") { clickMe(cell); return; } var a = cell.id.split("_"); var row = parseInt(a[0]); var col = parseInt(a[1]); if (cell.innerHTML.indexOf(">" + countMarkNum(row, col) + "") > -1) { if(row > 0) { clickMe(document.getElementById((row - 1) + "_" + col)); if(col > 0) // top left clickMe(document.getElementById((row - 1) + "_" + (col - 1))); if(col < colnum -1) // top right clickMe(document.getElementById((row - 1) + "_" + (col + 1))); } if(row < rownum - 1) { clickMe(document.getElementById((row + 1) + "_" + col)); if(col > 0) // bottom left clickMe(document.getElementById((row + 1) + "_" + (col - 1))); if(col < colnum -1) // bottom right clickMe(document.getElementById((row + 1) + "_" + (col + 1))); } if(col > 0) clickMe(document.getElementById(row + "_" + (col - 1))); // left if(col < colnum -1) clickMe(document.getElementById(row + "_" + (col + 1))); // right } } // process all mouse key down event document.onmousedown = function() { var e = event ? event : arguments[0] ; var cell = e.srcElement ? e.srcElement : e.target; if(cell.parentNode.name == "cell") cell = cell.parentNode ; if(cell.name == "cell") { if (e.button == 1) //left click clickMe(cell); else if (e.button == 2) //right click markMe(cell); else if (e.button == 3) //left and right click doubleClick(cell); } return true; } // --> script>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
下载本文
显示全文
专题
动视 51dongshi.net 版权所有
Copyright © 2019-2025