JavaScript实现打地鼠小游戏(2)

要实现保存历史最佳纪录那么要使用本地存储localStorage对象。在这儿我们主要使用该对象的
localStorage.setItem(“键名”,”值”),将值以键值对的形式存储到本地。localStorage.getItem(“键名”);通过键名获得值。

在这里我们只需将利用var preScore = localStorage.getItem(“键名”)获得历史的最佳记录,然后与当前的count进行对比,如果preScore大于当前的count,则不更新历史最佳记录。如果preScore小于了当前的count,那么使preScore=count;再本轮游戏结束后进行存储。

添加功能三、设置倒计时器,控制游戏结束

那么本轮游戏什么时候可以结束,那么就要设置一个计时器。在这里我写了一个一分钟的倒计时器。当sencond等于0,时游戏结束,然后再对
localStorage.setItem(“bestScore”,preScore),进行更新。
添加功能二和三的代码如下:

function die(){ try{ image[ranK].src = "./images/shang.gif"; image[ranK].removeAttribute("onclick"); image[ranK].style.marginTop = "3px"; count++; score.innerHTML =count; if( count > preScore) { score1.innerHTML = count; preScore = count; } localStorage.setItem("bestScore",preScore); }catch(ex){ } } //设置一个定时器 var seconds = 59; var speed = 1000; function countDown(seconds,speed){ var txt = "剩余时间:" + ((seconds < 10) ? "0" + seconds : seconds) +"秒"; div.innerHTML = txt; var timeId = setTimeout("countDown(seconds--,speed)",speed); //如果时间到了 if(seconds == 0){ //对最佳记录进行存储,不管当前有没有打破记录 localStorage.setItem("bestScore",preScore); clearTimeout(timeId); if(cover.style.display == "none"){ cover.style.display = "block"; //当没有打到一只老鼠的话显示该信息 if(count === undefined) text.innerHTML = "当前成绩为:"+0+"分"+"<br/>"+"游戏结束!"; //超越了历史记录,此时localStorage.getItem("bestScore")保存的值时当前的count值。显示该信息 else if(count == localStorage.getItem("bestScore")) { text.innerHTML = "当前成绩为:"+count+"分"+"<br/>"+"恭喜您打破纪录"+"<br/>"+"游戏结束!"; } else text.innerHTML = "当前成绩为:"+count+"分"+"<br/>"+"游戏结束!"; } } } countDown(seconds,speed);

效果图如下:

JavaScript实现打地鼠小游戏

最后附上完整的源代码

<!DOCTYPE html> <html> <head> <mata charset = "utf-8" /> <title>打地鼠</title> <script src = "http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <style type="text/css"> *{ margin: 0px; padding: 0px; } #background{ background: url(./images/beijing.jpg); } h2{ font-family: "微软雅黑"; font-size: 30px; padding-left: 100px; } table td{ width: 100px; height: 100px; } img{ margin-top: 31px; } #content{ z-index: 1; margin-left: 40%; } #count{ width: 320px; height: 40px; background-color:#E8E8E8; padding-left: 0px; text-align: center; color: red; font-size: 18px; line-height: 30px; font-family: "微软雅黑"; } #count .score{ text-align: center; color: lightgreen; font-size: 20px; line-height: 30px; font-family: "微软雅黑"; } #history #id{ text-align: center; color: lightgreen; font-size: 20px; line-height: 30px; font-family: "微软雅黑"; } #history{ position: absolute; top:60px; right:240px; width: 180px; height: 90px; background-color: #E8E8E8; color: lightblue; font-size:20px; line-height: 80px; text-align: center; } .clock{ position: absolute; top:0px; right:240px; width: 180px; height: 60px; background-color: lightblue; color: red; font-size: 20px; text-align: center; line-height: 60px; } #cover{ margin-top: 0px; position: absolute; z-index: 2; width: 800px; height: 600px; top: 0px; left: 0px; right: 0px; bottom: 0px; margin: auto; background :rgba(128,128,128,0.5); color: red; font-size: 30px; text-align: center; vertical-align: middle; } </style> </head> <body> <div id = "content"> <h2>打地鼠</h2><br/> <table id = "background"> <tr> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> </tr> <tr> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> </tr> <tr> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> <td><img src="https://www.jb51.net/article/images/keng.gif" ></td> </tr> </table> <div id = "count">当前得分 :<span class = "score"> 0 </span>分</div> <div id = "history">最佳记录: <span id = "score1"> 0 </span>分</div> <div class = "clock"></div> </div> <div id = "cover"> <span id = "text"></span></div> <script type="text/javascript"> //1 先获取到所有的坑(图片) var content = document.getElementById('content'); var cover = document.getElementById('cover'); var image = document.getElementsByTagName('img'); var countMouse = document.getElementById('count'); var score = countMouse.querySelector(".score"); var history = document.getElementById("history"); var score1 = document.getElementById("score1"); var div = document.querySelector(".clock"); var text = document.getElementById("text"); var preScore = 0; var ranK; var count = 0; // localStorage.removeItem("bestScore"); // localStorage.removeItem("scoreshow"); preScore = localStorage.getItem("bestScore"); console.log(preScore); score1.innerHTML = preScore; //让老鼠随机出现在一个坑里 setTimeout("chulai()",Math.random()*400+800); function chulai(){ try{ ranK = Math.floor(Math.random() * 9); image[ranK].src = "./images/dishu.gif"; image[ranK].style.marginTop = "0px"; image[ranK].setAttribute("onclick","die()"); setTimeout("paole()", Math.random()*400+800); }catch(ex){} } //2 老鼠回去了 function paole(){ try{ image[ranK].src = "https://www.jb51.net/article/images/keng.gif"; image[ranK].style.marginTop = "31px"; image[ranK].removeAttribute("onclick"); setTimeout("chulai()", Math.random()*400+800); }catch(ex){} } //3 老鼠被打死了 function die(){ try{ image[ranK].src = "./images/shang.gif"; image[ranK].removeAttribute("onclick"); image[ranK].style.marginTop = "3px"; count++; score.innerHTML =count; if( count > preScore) { score1.innerHTML = count; preScore = count; } localStorage.setItem("bestScore",preScore); }catch(ex){ } } //设置一个定时器 var seconds = 59; var speed = 1000; function countDown(seconds,speed){ var txt = "剩余时间:" + ((seconds < 10) ? "0" + seconds : seconds) +"秒"; div.innerHTML = txt; var timeId = setTimeout("countDown(seconds--,speed)",speed); if(seconds == 0){ //localStorage.setItem("scoreshow",count); localStorage.setItem("bestScore",preScore); clearTimeout(timeId); if(cover.style.display == "none"){ cover.style.display = "block"; if(count === undefined) text.innerHTML = "当前成绩为:"+0+"分"+"<br/>"+"游戏结束!"; else if(count == localStorage.getItem("bestScore")){ text.innerHTML = "当前成绩为:"+count+"分"+"<br/>"+"恭喜您打破纪录"+"<br/>"+"游戏结束!"; } else text.innerHTML = "当前成绩为:"+count+"分"+"<br/>"+"游戏结束!"; } } } countDown(seconds,speed); </script> </body> </html>

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyspsy.html