Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- function randomSquare() {
- squares.forEach(square => {
- square.classList.remove('mole')
- })
- var randomSquare = squares[Math.floor(Math.random() * 9)]
- randomSquare.classList.add('mole')
- hitPosition = randomSquare.id
- }
- function moveMole() {
- timerId = setInterval(randomSquare, 500)
- }
- function countDown() {
- currentTime -= 1
- timeLeft.textContent = currentTime
- if (currentTime == 0) {
- clearInterval(countDownTimerId)
- clearInterval(timerId)
- alert('GAME OVER! Your final score is ' + result)
- }
- }
- function checkHit() {
- if (this.id == hitPosition) {
- result += 1
- score.textContent = result
- hitPosition = null
- }
- }
- var squares = document.querySelectorAll('.square')
- var mole = document.querySelector('.mole')
- var timeLeft = document.querySelector('#time-left')
- var score = document.querySelector('#score')
- var result = 0
- var hitPosition
- var currentTime = 60
- var timerId = null
- var countDownTimerId = setInterval(countDown, 1000)
- moveMole()
- squares.forEach(square => {
- square.addEventListener('click', checkHit)
- })
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement