欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實現(xiàn)打地鼠小游戲

 更新時間:2017年02月13日 08:38:21   作者:ywhluck  
本文主要分享了js實現(xiàn)打地鼠小游戲的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧

話不多說,請看代碼:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>打地鼠</title>
 <style type="text/css">
 #content {
 width:960px;
 margin:0 auto;
 text-align:center;
 margin-top:40px;
 }
 #form1 {
 margin:20px 0;
 }
 table {
 margin:0 auto;
 cursor:url(http://cdn.attach.qdfuns.com/notes/pics/201702/12/115915n79d7hvffengpdxe.png),auto;
 }
 td {
 width:95px;
 height:95px;
 background:#00ff33;
 }
 </style>
 <script type="text/javascript">
 var td = new Array(),  //保存每個格子的地鼠
 playing = false,  //游戲是否開始
 score = 0, //分數(shù)
 beat = 0, //鼠標點擊次數(shù)
 success = 0, //命中率
 knock = 0, //鼠標點中老鼠圖片的次數(shù)
 countDown = 30, //倒計時
 interId = null, //指定 setInterval()的變量
 timeId = null; //指定 setTimeout()的變量
 //游戲結(jié)束
 function GameOver(){
 timeStop();
 playing = false;
  clearMouse();
 alert("游戲結(jié)束!\n 你獲得的分數(shù)為:"+score+"\n 命中率為:"+success);
 success = 0;
 score = 0;
 knock = 0;
 beat = 0;
 countDown = 30;
 }
 //顯示當前倒計時所剩時間
 function timeShow(){
 document.form1.remtime.value = countDown;
 if(countDown == 0){
 GameOver();
 return;
 }else{
 countDown = countDown-1;
 timeId = setTimeout("timeShow()",1000);
 }
 }
 //主動停止所有計時
 function timeStop() {
 clearInterval(interId);
 clearTimeout(timeId); 
 }
 //隨機循環(huán)顯示老鼠圖片
 function show(){
 if(playing){
 var current = Math.floor(Math.random()*25);
 document.getElementById("td["+current+"]").innerHTML = '<img src="http://cdn.attach.qdfuns.com/notes/pics/201702/12/115915w6tluu1gq8l1b54h.png">';
 setTimeout("document.getElementById('td["+current+"]').innerHtml=''",3000); //使用 setTimeout()實現(xiàn)3秒后隱藏老鼠圖片
 }
 }
 //清除所有老鼠圖片
 function clearMouse(){
 for(var i=0;i<25;i++){
 document.getElementById("td["+i+"]").innerHTML="";
 }
 }
 //點擊事件函數(shù),判斷是否點中老鼠
 function hit(id){
 if(playing == false){
 alert("請點擊開始游戲!");
 return;
 }else{
 beat += 1;
 if(document.getElementById("td["+id+"]").innerHTML != ""){
 score += 1;
 knock += 1;
 success = knock/beat;
 document.form1.success.value = success;
 document.form1.score.value = score;
 document.getElementById("td["+id+"]").innerHTML = "";
 }else{
 score += -1;
 success = knock/beat;
 document.form1.success.value = success;
  document.form1.score.value = score;
 }
 }
 }
 //游戲開始
 function GameStart(){
 playing = true;
 interId = setInterval("show()",1000); 
 document.form1.score.value = score;
 document.form1.success.value = success;
 timeShow();
 } 
 </script>
</head>
<body>
 <div id="content">
 <input type="button" value="開始游戲" onclick="GameStart()" />
 <input type="button" value="結(jié)束游戲" onclick="GameOver()" />
 <form name="form1" id="form1">
  <label>分數(shù):</label>
  <input type="text" name="score" size="5">
  <label>命中率:</label>
  <input type="text" name="success" size="10">
  <label>倒計時:</label>
  <input type="text" name="remtime" size="5">
 </form> 
 <table>
  <tr>
  <td id="td[0]" onclick="hit(0)"></td>  
  <td id="td[1]" onclick="hit(1)"></td>
  <td id="td[2]" onclick="hit(2)"></td>
  <td id="td[3]" onclick="hit(3)"></td>
  <td id="td[4]" onclick="hit(4)"></td>
  </tr>
  <tr>
  <td id="td[5]" onclick="hit(5)"></td>
  <td id="td[6]" onclick="hit(6)"></td>
  <td id="td[7]" onclick="hit(7)"></td>
  <td id="td[8]" onclick="hit(8)"></td>
  <td id="td[9]" onclick="hit(9)"></td>
  </tr>
  <tr>
  <td id="td[10]" onclick="hit(10)"></td>
  <td id="td[11]" onclick="hit(11)"></td>
  <td id="td[12]" onclick="hit(12)"></td>
  <td id="td[13]" onclick="hit(13)"></td>
  <td id="td[14]" onclick="hit(14)"></td>
  </tr>
  <tr>
  <td id="td[15]" onclick="hit(15)"></td>
  <td id="td[16]" onclick="hit(16)"></td>
  <td id="td[17]" onclick="hit(17)"></td>
  <td id="td[18]" onclick="hit(18)"></td>
  <td id="td[19]" onclick="hit(19)"></td>
  </tr>
  <tr>
  <td id="td[20]" onclick="hit(20)"></td>
  <td id="td[21]" onclick="hit(21)"></td>
  <td id="td[22]" onclick="hit(22)"></td>
  <td id="td[23]" onclick="hit(23)"></td>
  <td id="td[24]" onclick="hit(24)"></td>
  </tr>
 </table>
 </div>
</body>
</html>

流程設計:

  • 點擊“開始游戲”按鈕游戲開始,否則將提示“請點擊開始游戲”字樣
  • 分數(shù)、命中率顯示重置為“0”,倒計時開始(默認為30秒)
  • 老鼠圖片不斷顯示、隱藏,玩家可點擊鼠標左鍵進行游戲
  • 當30秒倒計時結(jié)束或者玩家主動點擊“結(jié)束按鈕”時,游戲結(jié)束并顯示游戲結(jié)果

實例中用到的圖片附件下載

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關文章

  • 基于Javascript開發(fā)連連看游戲小程序

    基于Javascript開發(fā)連連看游戲小程序

    這篇文章主要介紹了基于Java開發(fā)連連看游戲小程序,連連看是在有限的時間內(nèi),只要把所有能連接的相同圖案,兩個一對地找出來,消除全部就成功了,文中提供了解決思路和部分實現(xiàn)代碼,需要的朋友可以參考下
    2023-03-03
  • JavaScript中數(shù)組雙重去重的方法總結(jié)

    JavaScript中數(shù)組雙重去重的方法總結(jié)

    這篇文章主要為大家學習介紹了JavaScript中數(shù)組雙重去重的幾個常用方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-07-07
  • JavaScript仿微博輸入框效果(案例分析)

    JavaScript仿微博輸入框效果(案例分析)

    這篇文章給大家分享一個小的JavaScript的案例,就是模仿微博輸入框的效果,非常不錯,對微博輸入框效果感興趣的朋友通過本文學習吧
    2016-12-12
  • js逆向解密之網(wǎng)絡爬蟲

    js逆向解密之網(wǎng)絡爬蟲

    在本篇內(nèi)容里小編給大家整理的是關于js逆向解密之網(wǎng)絡爬蟲的相關知識點內(nèi)容,需要的朋友們參考下。
    2019-05-05
  • 用JavaScript獲取頁面文檔內(nèi)容的實現(xiàn)代碼

    用JavaScript獲取頁面文檔內(nèi)容的實現(xiàn)代碼

    下面小編就為大家?guī)硪黄肑avaScript獲取頁面文檔內(nèi)容的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • JS中showModalDialog關閉子窗口刷新主窗口用法詳解

    JS中showModalDialog關閉子窗口刷新主窗口用法詳解

    這篇文章主要介紹了JS中showModalDialog關閉子窗口刷新主窗口用法,結(jié)合具體實例形式較為詳細的分析了showModalDialog常見用法與相關使用技巧,需要的朋友可以參考下
    2017-03-03
  • Web?Components入門教程示例詳解

    Web?Components入門教程示例詳解

    這篇文章主要為大家介紹了Web?Components入門教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • JavaScript省市區(qū)三級聯(lián)動菜單效果

    JavaScript省市區(qū)三級聯(lián)動菜單效果

    這篇文章主要為大家詳細介紹了JavaScript省市區(qū)三級聯(lián)動菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • setTimeout內(nèi)不支持jquery的選擇器的解決方案

    setTimeout內(nèi)不支持jquery的選擇器的解決方案

    在JS中無論是setTimeout還是setInterval,在使用函數(shù)名作為調(diào)用句柄時都不能帶參數(shù),而在許多場合必須要帶參數(shù),這就需要想方法解決。
    2015-04-04
  • Javascript利用canvas繪制兩點間曲線和箭頭

    Javascript利用canvas繪制兩點間曲線和箭頭

    這篇文章主要為大家詳細介紹了Javascript如何利用canvas實現(xiàn)在兩點間繪制曲線和矩形,并且在矩形中繪制文字,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-10-10

最新評論