-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (42 loc) · 1.14 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const squares=document.querySelectorAll('.square')
const timeLeft=document.querySelector('#time-left')
const score=document.querySelector('#score')
const molePosition=document.querySelector('.mole')
const hit=document.querySelector('#hit-sound')
const gameOver=document.querySelector('#game-over')
let result=0
let hitPosition
let currentTime=60
let timerId=null
function randomSquares(){
squares.forEach(square=>{
square.classList.remove('mole')
})
let randomPosition=squares[Math.floor(Math.random()*9)]
randomPosition.classList.add('mole')
hitPosition=randomPosition.id
}
squares.forEach(square=>{
square.addEventListener('mousedown', ()=>{
if(square.id==hitPosition){
result++;
hit.play()
score.textContent=result
hitPosition=null
}
})
})
function countDown(){
currentTime--;
timeLeft.textContent=currentTime
if(currentTime==0){
gameOver.play()
clearInterval(countDownTimerId)
clearInterval(timerId)
}
}
function moveMole(){
timerId=setInterval(randomSquares,800)
}
moveMole()
let countDownTimerId= setInterval(countDown,1000)