JavaScript實現打地鼠遊戲
阿新 • • 發佈:2021-10-11
本文例項為大家分享了實現打地鼠遊戲的具體程式碼,供大家參考,具體內容如下
遊戲說明:
點選"開始遊戲"按鈕,在圖中隨機產生老鼠,老鼠消失前單擊老鼠進行擊打,打中一次即可獲得100的積分,沒有打中老鼠,扣取100積分
模組
<style> #div0{ text-align: center; width: 1360px; height: 600px; margin: 60px auto; background-image: url("images/bg.jpg"); position: relative; } #div_top{ text-align: left; color:brown; width: 360px; height: 100px; position: absolutwww.cppcns.come; left: 500px; } #div_left{ width: 350px; height: 320px; position: absolute; left: 300px; top: 150px; } #tab_data{ width:350px; height:320px; } #div_right{ width: 350px; height: 320px; position: absolute; right: 380px; top: 150px; } #tab{ width:320px; height:320px; } #tab td{ background-image:url("images/00.jpg"); background-repeat:no-repeat; background-position:center; } </style>
html模組
<div id="div0"> <div id="div_top"> 遊戲說明:<br/> 點選"開始遊戲"按鈕,在下圖中隨機產生老鼠,老鼠消失前單擊老鼠進行擊打,<br/> 打中一次即可獲得100的積分,沒有打中老鼠,扣取100積分。快快行動吧,考驗<br/>您的 反應和眼力!<br/> </div> <div id="div_left"> <table id="tab_data"> <tr> <th>遊戲時間:</th> <td><input type="text" name="text_time" value="1" />分鐘</td> </tr> <tr> <th>倒計時:</th> <td><input type="text" name="text_CD" value="60" disabled="disabled"/>秒</td> </tr> <tr> <th>出現間隔:</th> <td><input type="text" name="text_hide" value="1" />秒</td> </tr> <tr> <th>停留時間:</th> <td><input type="text" name="text_show" value="1" />秒</td> </tr> <tr> <th>得分情況:</th> <td><span id="span_score"></span></td> </tr> <tr> <th colspan="2"> <input type="button" value="開始遊戲" id="but_start"/> <input type="button" value="結束遊戲" id="but_end"/> </th> </tr> </table> </div> <div id="div_right"> </div> </div>
模組
<script> var collTd=[];//記錄所有的td var oTdMouse;//記錄被選中的td //定義變數記錄頁面中的標籤元素 var oButStart,oButEnd; var oTextTime,oTextHide,oTimeShow,oTimeCD; var oSpanScore; //定義變數記錄時間引數: var iAll,iCD,iShowwww.cppcns.com,iHide,iCount,iGet; var iCDId,iRandomId,iShow,iChangeId; window.onload=function(){ //建立表格 createTable(); //給標籤元素變數賦值 init(); //給按鈕註冊事件 oButStart.onclick=startGame; oButEnd.onclick=endGame; } //開始遊戲 function startGame(){ iAll=parseInt(oTextTime.value)*60; iCD=iAll; //每隔1秒鐘執行一次倒計時 iCDId=window.setInterval(setCD,1000); iShow=parseInt(oTextShow.value); iHide=parseInt(oTextHide.value); iCount=0; iGet=0; //每隔iShow+Hide隨機一個td randomId(); iRandomId=window.setInterval(randomId,(iShow+iHide)*1000); oButStart.disabled="disabled"; oButEnd.disabled=""; } //隨機td function randomId(){ iCount++; var index=parseInt(Math.random()*collTd.length); oTdMouse=collTd[index]; //更改背景圖片 oTdMouse.style.backgroundImage="url('images/01.jpg')"; //等待show時間結束後 把背景圖片設定回來 iShowId=window.setTimeout("oTdMouse.style.backgroundImage='url(images/00.jpg)';",iShow*1000); } //設定倒計時 function setCD(){ iCD--; oTextCD.value=iCD; //每隔一秒鐘記錄一下分數 var message="共"+iCount+"只,打中"+iGet+"只!"; oSpanScore.innerHTML=message.fontcolor("blue").bold(); if(iCD<=0){ endGame(); } } //結束遊戲 function endGame(){ oButEnd.disabled="disabled"; oButStart.disabled=""; window.clearInterval(iCDId); window.clearInterval(iRandomId); } //給標籤元素變數賦值 function init(){ oButStart=document.getElementById("but_start"); oButEnd=document.getElementById("but_end"); oTextTime=document.getElementsByName("text_time")[0]; oTextCD=document.getElementsByName("text_CD")[0]; oTextHide=document.getElementsByName("text_hide")[0]; oTextShow=document.getElementsByName("text_show")[0]; oSpanScore=document.getElementById("span_score"); oTextCD.value=60; oButEnd.disabled="disabled"; } //動態生成表格 function createTable(){ var oDivRight=document.getElementById("div_right"); var oTab=document.createElement("table"); oTab.id="tab"; for(var i=0;i<6;i++){ var oTr=document.createElement("tr"); for(var j=0;j<5;j++){ var oTd=document.createElement("td"); oTr.appendChild(oTd); collTd.push(oTd); //給所有的td新增點選事件 oTd.onclick=function(){ if(this==oTdMouse){ //判斷當前單元格的背景圖片是不是01.jpg if(this.style.backgroundImage.indexOf("01.jpgwww.cppcns.com")!=-1){ //得一分 iGet++; //背景圖片更改為02.jpg oTdMouse.style.backgroundImage="url('images/02.jpg')"; //等待0.2秒更改為00.jpg iChangeId=window.setTimeout("oTdMouse.style.backgroundImage='url(images/00.jpg)';",200); var message="共"+iCount+"只,打中"+iGet+"只!"; oSpanScore.innerHTML=message.fontcolor("blue").bold(); } } } } oTab.appendChild(oTr); } oDivRight.appendChild(oTab); } </script>
更多有趣的經典小遊戲實現專題,分享給大家:
C++經典小遊戲彙總
經典小遊戲彙總
python俄羅斯方塊遊戲集合
Script經典遊戲 玩不停
java經典小遊戲彙總
javascript經典小遊戲彙總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。