js實(shí)現(xiàn)打地鼠小游戲
話不多說(shuō),請(qǐng)看代碼:
<!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(), //保存每個(gè)格子的地鼠
playing = false, //游戲是否開始
score = 0, //分?jǐn)?shù)
beat = 0, //鼠標(biāo)點(diǎn)擊次數(shù)
success = 0, //命中率
knock = 0, //鼠標(biāo)點(diǎn)中老鼠圖片的次數(shù)
countDown = 30, //倒計(jì)時(shí)
interId = null, //指定 setInterval()的變量
timeId = null; //指定 setTimeout()的變量
//游戲結(jié)束
function GameOver(){
timeStop();
playing = false;
clearMouse();
alert("游戲結(jié)束!\n 你獲得的分?jǐn)?shù)為:"+score+"\n 命中率為:"+success);
success = 0;
score = 0;
knock = 0;
beat = 0;
countDown = 30;
}
//顯示當(dāng)前倒計(jì)時(shí)所剩時(shí)間
function timeShow(){
document.form1.remtime.value = countDown;
if(countDown == 0){
GameOver();
return;
}else{
countDown = countDown-1;
timeId = setTimeout("timeShow()",1000);
}
}
//主動(dòng)停止所有計(jì)時(shí)
function timeStop() {
clearInterval(interId);
clearTimeout(timeId);
}
//隨機(jī)循環(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()實(shí)現(xiàn)3秒后隱藏老鼠圖片
}
}
//清除所有老鼠圖片
function clearMouse(){
for(var i=0;i<25;i++){
document.getElementById("td["+i+"]").innerHTML="";
}
}
//點(diǎn)擊事件函數(shù),判斷是否點(diǎn)中老鼠
function hit(id){
if(playing == false){
alert("請(qǐng)點(diǎn)擊開始游戲!");
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>分?jǐn)?shù):</label>
<input type="text" name="score" size="5">
<label>命中率:</label>
<input type="text" name="success" size="10">
<label>倒計(jì)時(shí):</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è)計(jì):
- 點(diǎn)擊“開始游戲”按鈕游戲開始,否則將提示“請(qǐng)點(diǎn)擊開始游戲”字樣
- 分?jǐn)?shù)、命中率顯示重置為“0”,倒計(jì)時(shí)開始(默認(rèn)為30秒)
- 老鼠圖片不斷顯示、隱藏,玩家可點(diǎn)擊鼠標(biāo)左鍵進(jìn)行游戲
- 當(dāng)30秒倒計(jì)時(shí)結(jié)束或者玩家主動(dòng)點(diǎn)擊“結(jié)束按鈕”時(shí),游戲結(jié)束并顯示游戲結(jié)果
實(shí)例中用到的圖片附件下載
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
JavaScript+H5實(shí)現(xiàn)微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了JavaScript+H5實(shí)現(xiàn)微信搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
JavaScript綁定事件監(jiān)聽函數(shù)的通用方法
這篇文章主要為大家詳細(xì)介紹了JavaScript綁定事件監(jiān)聽函數(shù)的通用方法,感興趣的朋友可以參考一下2016-05-05
javascript實(shí)現(xiàn)小型區(qū)塊鏈功能
這篇文章主要介紹了javascript實(shí)現(xiàn)小型區(qū)塊鏈功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
科訊商業(yè)版中用到的ajax空間與分頁(yè)函數(shù)
科訊商業(yè)版中用到的ajax空間與分頁(yè)函數(shù)...2007-09-09
下面就結(jié)合我自己的體會(huì)和所學(xué)習(xí)的東東和大家一起來(lái)學(xué)習(xí)在JS中如何使用面向?qū)ο蟮木幊獭?/div> 2011-08-08
JavaScript數(shù)組every方法的應(yīng)用場(chǎng)景實(shí)例
every方法確定數(shù)組中的每一項(xiàng)的結(jié)果都滿足所寫的條件的時(shí)候,就會(huì)返回true,否則返回false,這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組every方法應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-12-12
Javascript設(shè)計(jì)模式之觀察者模式的多個(gè)實(shí)現(xiàn)版本實(shí)例
這篇文章主要介紹了Javascript設(shè)計(jì)模式之觀察者模式的多個(gè)實(shí)現(xiàn)版本實(shí)例,本文給出3種實(shí)現(xiàn)版本代碼,同時(shí)給出了Jquery實(shí)現(xiàn)版本,需要的朋友可以參考下2015-03-03最新評(píng)論

