站长资源网络编程

HTML+JavaScript实现扫雷小游戏

整理:jimmy2026/7/29浏览2
简介本文实例为大家分享了JavaScript实现扫雷小游戏的具体代码,供大家参考,具体内容如下工具:Sublime Text / Dreamweaver /HbuilderSaoLe</div> <div class="news_infos"><div id="MyContent"><p>本文实例为大家分享了JavaScript实现扫雷小游戏的具体代码,供大家参考,具体内容如下</p> <p>工具:Sublime Text / Dreamweaver /Hbuilder</p> <p><a href="javascript:;" onclick="showimg('/UploadFiles/2021-04-02/201993073213758.jpg');"><img src="/UploadFiles/2021-04-02/201993073213758.jpg" alt="HTML+JavaScript实现扫雷小游戏" onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" border="0"/></a></p> <div class="htmlcode"> <pre class="brush:xhtml;"> <!doctype html> <html> <head> <meta charset="utf-8"> <title>SaoLei</title> <style type="text/css"> table { -webkit-touch-callout: none; /* iOS Safari */ -webkit-user-select: none; /* Chrome/Safari/Opera */ -khtml-user-select: none; /* Konqueror */ -moz-user-select: none; /* Firefox */ -ms-user-select: none; /* Internet Explorer/Edge */ user-select: none; /* Non-prefixed version, currently not supported by any browser */ } </style> </head> <body> <center style="margin:150px auto;"> <h2 style="font-family:Comic Sans Ms;">Mine Clearance(扫雷)&diams;</h2> <p>请设置行和列开始游戏</p> <p>游戏难度:<select id="level" onchange="changelevel()"><option>小白级</option><option>大神级</option></select></p> <p id="select_level"></p> 行: <input type="text" id="rows"> 列: <input type="text" id="cols"> <button id="add" onClick="add()">PlayGame</button> <br> <p id="tips"></p> <p id="leiNum"></p> <table border="2" id="tab" ></table> <p id="GScore"></p> </center> <script type="text/javascript"> var lei =new Array("&hearts;","0","&hearts;","&hearts;","&hearts;","&hearts;"); var tab =$("tab"); var GScore=$("GScore"); var score=0; var tip=$("tips"); var time; var i=3; var row =$("rows"); var col =$("cols"); var Total=0; var lei_count =0; var levels= $("level"); var select_level=$("select_level"); function add() { clear(); tip.innerHTML="游戏开始"; score=0; GScore.innerHTML="当前得分:"+score; lei_count=0; tab.innerHTML=""; Total=0; Total=parseInt(row.value)*parseInt(col.value); for(var i=0;i<row.value;i++) { var newTr =document.createElement("tr"); newTr.id=i;// newTr.style.background="black"; for(var j=0;j<col.value;j++) {// var rand=parseInt(Math.random()*lei.length); newTr.innerHTML+="<td ><button id='"+i+","+j+"' style ='width:25px;height:25px;background:green; color:green; border:1px blue solid' οnclick='myclick(this)' οnmοuseοver='changecolor(this)' οnmοuseοut='resetcolor(this)'>"+lei[rand]+"</button></td>"; if(lei[rand]=="0") { lei_count++; } } tab.appendChild(newTr); } Total=Total-lei_count; var leinum =$("leiNum"); leinum.innerHTML="本局雷数:"+lei_count; } function $(id) { return document.getElementById(id); } function change(obj) { if(obj.innerHTML=="0") { time=setInterval(times,1000); obj.style.backgroundColor="red"; obj.innerHTML=""; alert("Game Over!"); }else { obj.style.backgroundColor="white"; score=score+1; } GScore.innerHTML="当前得分:"+score; } function myclick(obj) { if(obj.style.background!="white") { change(obj); check(obj); Total--; if(Total==0) { alert("你赢了!总分:"+score); } } } function changecolor(obj) { obj.style.border="1px red solid "; } function resetcolor(obj) { obj.style.border="1px blue solid"; } function times() { tip.innerHTML="游戏结束,"+i+"秒后重新开始游戏"; if(i==0) { add(); } i--; } function clear() { clearInterval(time); i=3; } function check(obj) { var index=0; var len =obj.id.split(","); index=Number(len[1]);//下标 var boom =0; //左节点 if(index-1>=0) { if(obj.parentNode.previousSibling.childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.previousSibling.childNodes[0].style.background="black"; } } //右节点 if(index!=Number(col.value)-1){ if(obj.parentNode.nextSibling.childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.nextSibling.childNodes[0].style.background="black"; } } //上节点 if(obj.parentNode.parentNode.id!="0"){ if(obj.parentNode.parentNode.previousSibling.childNodes[index].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.previousSibling.childNodes[index].childNodes[0].style.background="black"; } } //下节点 if(obj.parentNode.parentNode.id!=Number(row.value)-1){ if(obj.parentNode.parentNode.nextSibling.childNodes[index].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.nextSibling.childNodes[index].childNodes[0].style.background="black"; } } //左上节点 if(index-1>=0 && obj.parentNode.parentNode.id!="0"){ if(obj.parentNode.parentNode.previousSibling.childNodes[index-1].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.previousSibling.childNodes[index-1].childNodes[0].style.background="black"; } } //右上节点 if(index!=Number(col.value)-1 && obj.parentNode.parentNode.id!="0"){ if(obj.parentNode.parentNode.previousSibling.childNodes[index+1].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.previousSibling.childNodes[index+1].childNodes[0].style.background="black"; } } //左下节点 if(index-1>=0 && obj.parentNode.parentNode.id!=Number(row.value)-1){ if(obj.parentNode.parentNode.nextSibling.childNodes[index-1].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.nextSibling.childNodes[index-1].childNodes[0].style.background="black"; } } //右下节点 if(index!=Number(col.value)-1 && obj.parentNode.parentNode.id!=Number(row.value)-1){ if(obj.parentNode.parentNode.nextSibling.childNodes[index+1].childNodes[0].innerHTML=="0") { boom++; if(levels.value=="小白级") obj.parentNode.parentNode.nextSibling.childNodes[index+1].childNodes[0].style.background="black"; } } if(boom>0) obj.innerHTML=boom; else obj.innerHTML="&nbsp;"; } function changelevel() { var info=levels.value; if(levels.value=="小白级") { info+="&nbsp;(自动排雷)"+""; } else { info+=""; } select_level.innerHTML="你已选择:"+info; } window.onload=changelevel; </script> } </body> </html></pre> </div> <p>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。</p></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="/3g/1/83635.html" title="Bootstrap实现模态框效果">Bootstrap实现模态框效果</a></p> <p>下一篇:<a href="/3g/1/83637.html" title="React+Redux实现简单的待办事项列表ToDoList">React+Redux实现简单的待办事项列表ToDoList</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li><a href="/3g/1/623703.html" title="群星《奔赴!万人现场 第2期》[FLAC/分轨]">群星《奔赴!万人现场 第2期》[FLAC/分轨]</a></li> <li><a href="/3g/1/623702.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623701.html" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐">群星《奇妙浪一夏 (上海迪士尼度假区音乐</a></li> <li><a href="/3g/1/623700.html" title="【古典音乐】詹姆斯·高威《季节》1993[WA">【古典音乐】詹姆斯·高威《季节》1993[WA</a></li> <li><a href="/3g/1/623699.html" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]">贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</a></li> <li><a href="/3g/1/623698.html" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE">小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE</a></li> <li><a href="/3g/1/623697.html" title="群星《欢迎来到我身边 电影原声专辑》[32">群星《欢迎来到我身边 电影原声专辑》[32</a></li> <li><a href="/3g/1/623696.html" title="群星《欢迎来到我身边 电影原声专辑》[FL">群星《欢迎来到我身边 电影原声专辑》[FL</a></li> <li><a href="/3g/1/623695.html" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低">雷婷《梦里蓝天HQⅡ》 2023头版限量编号低</a></li> <li><a href="/3g/1/623694.html" title="群星《2024好听新歌42》AI调整音效【WAV分">群星《2024好听新歌42》AI调整音效【WAV分</a></li> </ul> </div> </div> <div class="sidebar"> </div> <div class="sidebar"> 友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="http://www.paidiu.com/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a> </div> </article> <footer> <p>Design by <a href="http://m.ddrfans.com">DDR爱好者之家</a> <a href="http://m.ddrfans.com">http://m.ddrfans.com</a></p> </footer> <script src="/images3g/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>