JavaScript打地鼠

下载地址

JavaScript实现简单的打地鼠小游戏代码

1641485701.png

dd:

很高兴回答您的问题。以下是一个简单的JavaScript打地鼠游戏的例子: ```javascript <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>打地鼠游戏</title> <style> .hole { width: 100px; height: 100px; background-color: #ccc; border-radius: 50%; position: relative; display: inline-block; margin: 10px; } .mole { width: 80px; height: 80px; background-color: #333; border-radius: 50%; position: absolute; top: 10px; left: 10px; display: none; } </style> </head> <body> <h1>打地鼠游戏</h1> <div id="score">得分:0</div> <div id="time">时间:30</div> <div id="holes"> <div class="hole"></div> <div class="hole"></div> <div class="hole"></div> <div class="hole"></div> <div class="hole"></div> <div class="hole"></div> </div> <script> var score = 0; var time = 30; var timer; var holes = document.getElementById('holes'); var scoreBoard = document.getElementById('score'); var timeBoard = document.getElementById('time'); function showMole() { var randomHole = Math.floor(Math.random() * holes.children.length); var hole = holes.children[randomHole]; var mole = hole.querySelector('.mole'); if (!mole) { mole = document.createElement('div'); mole.className = 'mole'; mole.addEventListener('click', function() { score++; scoreBoard.innerHTML = '得分:' + score; this.parentNode.removeChild(this); }); hole.appendChild(mole); } mole.style.display = 'block'; setTimeout(function() { mole.style.display = 'none'; }, 1000); } function startGame() { score = 0; time = 30; scoreBoard.innerHTML = '得分:' + score; timeBoard.innerHTML = '时间:' + time; clearInterval(timer); timer = setInterval(function() { time--; timeBoard.innerHTML = '时间:' + time; if (time <= 0) { clearInterval(timer); alert('游戏结束,得分:' + score); } }, 1000); showMole(); setTimeout(arguments.callee, 1000); } startGame(); </script> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值