iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
自我挑戰組

JavaScript 30天 跟著一起做一遍!!!系列 第 29

[Day29] Whack A Mole Game

[Day29] Whack A Mole Game

打地鼠

需要用到的技巧與練習目標

const scroeBord=document.querySelector('.score')
let lastHole
let timeUp = false;
let score=0
function randTime(min,max){
return Math.round(Math.radom()*(max - min)+min)
}


function randomHole(holes){
const idx=Math.flooor(Math.random()*holse.lenght);

const hole=holes[idx];
if(hole === lastHole){
return randomHole(holes);
}
lastHole=hole;
return hole
}

function peep(){
const time= randomTime(200,1000)

const hole=randomHole(holes)
hole.classList.add('up')

setTimeout(()=>{
hole.classList.remove('up')
if(!timeUp)peep()

},time)

}

function startGame(){
scoreBoard.textContent =0
timeUp=false;
score=0
peep()
setTimeoue(()=> timeUp=true,10000)
}

function bonk(e){
if(!e.isTrusted) return;
score++
this.classList.remove('up')
scoreBoard.textContent=score
}

moles.forEach(mole =>mole.addEventListener('click',bank))


上一篇
Countdown Clock
下一篇
[Day30] Speech Synthesis
系列文
JavaScript 30天 跟著一起做一遍!!!30

尚未有邦友留言

立即登入留言