Advertisement
ZEdKasat

Untitled

Jan 4th, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <script>
  2.  
  3. function randomSquare() {
  4. squares.forEach(square => {
  5. square.classList.remove('mole')
  6. })
  7.  
  8. var randomSquare = squares[Math.floor(Math.random() * 9)]
  9. randomSquare.classList.add('mole')
  10.  
  11. hitPosition = randomSquare.id
  12. }
  13.  
  14. function moveMole() {
  15. timerId = setInterval(randomSquare, 500)
  16. }
  17.  
  18. function countDown() {
  19. currentTime -= 1
  20. timeLeft.textContent = currentTime
  21.  
  22. if (currentTime == 0) {
  23. clearInterval(countDownTimerId)
  24. clearInterval(timerId)
  25. alert('GAME OVER! Your final score is ' + result)
  26. }
  27. }
  28.  
  29. function checkHit() {
  30. if (this.id == hitPosition) {
  31. result += 1
  32. score.textContent = result
  33. hitPosition = null
  34. }
  35. }
  36.  
  37. var squares = document.querySelectorAll('.square')
  38. var mole = document.querySelector('.mole')
  39. var timeLeft = document.querySelector('#time-left')
  40. var score = document.querySelector('#score')
  41.  
  42. var result = 0
  43. var hitPosition
  44. var currentTime = 60
  45. var timerId = null
  46. var countDownTimerId = setInterval(countDown, 1000)
  47.  
  48. moveMole()
  49.  
  50. squares.forEach(square => {
  51. square.addEventListener('click', checkHit)
  52. })
  53.  
  54.  
  55.  
  56.  
  57. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement