|
| 1 | +const holes = document.querySelectorAll('.hole'); |
| 2 | +const scoreBoard = document.querySelector('.score'); |
| 3 | +const moles = document.querySelectorAll('.mole'); |
| 4 | +const gameOverPanel = document.querySelector('.game-over-panel'); |
| 5 | + |
| 6 | +const button = document.querySelector('.btn-start'); |
| 7 | +const timerDisplay = document.querySelector('.timer'); |
| 8 | +const hiScoreDisplay = document.querySelector('.hi-score'); |
| 9 | +const scoreReset = document.querySelector('.reset'); |
| 10 | + |
| 11 | +const gameMode = document.querySelector('.game-mode'); |
| 12 | + |
| 13 | +// get & set the hiscore value in the UI |
| 14 | +let currentHiScore = localStorage.getItem('hiScore') || 0; |
| 15 | +hiScoreDisplay.textContent = currentHiScore; |
| 16 | + |
| 17 | +// set the difficulty of the game |
| 18 | +let difficulty = { min: 500, max: 1000 }; |
| 19 | + |
| 20 | +// setup the rest of the game |
| 21 | +const gameDuration = 10000; // 10 seconds |
| 22 | +let countdown; |
| 23 | +let lastHole; |
| 24 | +let timeUp = false; |
| 25 | +let score = 0; |
| 26 | + |
| 27 | +const randomTime = function randomTime(min, max) { |
| 28 | + return Math.round(Math.random() * (max - min) + min); |
| 29 | +}; |
| 30 | + |
| 31 | +const randomHole = function randomHole(holes) { |
| 32 | + const idx = Math.floor(Math.random() * holes.length); |
| 33 | + const hole = holes[idx]; |
| 34 | + |
| 35 | + if (hole === lastHole) { |
| 36 | + // same hole, run it again! |
| 37 | + return randomHole(holes); |
| 38 | + } |
| 39 | + |
| 40 | + lastHole = hole; |
| 41 | + return hole; |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | +// Show the moles! |
| 46 | +const peep = function peep() { |
| 47 | + const time = randomTime(difficulty.min, difficulty.max); |
| 48 | + const hole = randomHole(holes); |
| 49 | + |
| 50 | + hole.classList.add('up'); |
| 51 | + |
| 52 | + setTimeout(() => { |
| 53 | + hole.classList.remove('up'); |
| 54 | + if (!timeUp) peep(); |
| 55 | + }, time); |
| 56 | +}; |
| 57 | + |
| 58 | +// Whack the moles! |
| 59 | +const bonk = function bonk(e) { |
| 60 | + if (!e.isTrusted) return; // stop cheating! |
| 61 | + this.parentNode.classList.remove('up'); |
| 62 | + score++; |
| 63 | + scoreBoard.textContent = score; |
| 64 | +}; |
| 65 | + |
| 66 | +// start and show the remaining game duration |
| 67 | +const startCountdown = function startCountdown() { |
| 68 | + clearInterval(countdown); // clear exiting timers |
| 69 | + const now = Date.now(); |
| 70 | + const then = now + gameDuration; |
| 71 | + |
| 72 | + displayTimeLeft(gameDuration / 1000); |
| 73 | + |
| 74 | + countdown = setInterval(() => { |
| 75 | + const secondsLeft = Math.round((then - Date.now()) / 1000); |
| 76 | + |
| 77 | + if (secondsLeft < 0) { |
| 78 | + clearInterval(countdown); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + displayTimeLeft(secondsLeft); |
| 83 | + |
| 84 | + }, 1000); |
| 85 | +}; |
| 86 | + |
| 87 | +const displayTimeLeft = function displayTimeLeft(seconds) { |
| 88 | + let secondsRemaining = seconds; |
| 89 | + |
| 90 | + const minutes = Math.floor(secondsRemaining / 60); |
| 91 | + secondsRemaining = secondsRemaining % 60; |
| 92 | + const displayMins = minutes < 10 ? `0${minutes}` : minutes; |
| 93 | + const displaySeconds = secondsRemaining < 10 ? `0${secondsRemaining}` : secondsRemaining; |
| 94 | + const display = `${displayMins}:${displaySeconds}`; |
| 95 | + |
| 96 | + timerDisplay.textContent = display; |
| 97 | +}; |
| 98 | + |
| 99 | +const getHiScore = function getHiScore() { |
| 100 | + return localStorage.getItem('hiScore'); |
| 101 | +}; |
| 102 | + |
| 103 | +// Save hi-score to localStorage |
| 104 | +const saveHiScore = function saveHiScore() { |
| 105 | + if (score > parseInt(currentHiScore)) { |
| 106 | + localStorage.setItem('hiScore', JSON.stringify(score)); |
| 107 | + hiScoreDisplay.textContent = score; |
| 108 | + hiScoreDisplay.classList.add('new-score'); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +const resetHiScore = function resetHiScore(){ |
| 113 | + localStorage.setItem('hiScore', JSON.stringify(0)); |
| 114 | + hiScoreDisplay.textContent = 0; |
| 115 | + currentHiScore = 0; |
| 116 | +}; |
| 117 | + |
| 118 | +// Set the game difficulty (Easy, Hard, Expert) |
| 119 | +const toggleDifficulty = function toggleDifficulty() { |
| 120 | + // easy mode range is min: 300, max: 1000 |
| 121 | + // hard mode range is min: 150, max: 800 |
| 122 | + // expert mode range is min: 100, max: 500 |
| 123 | + switch (this.value) { |
| 124 | + case 'easy': |
| 125 | + difficulty.min = 500; |
| 126 | + difficulty.max = 1000; |
| 127 | + break; |
| 128 | + case 'hard': |
| 129 | + difficulty.min = 200; |
| 130 | + difficulty.max = 800; |
| 131 | + break; |
| 132 | + case 'expert': |
| 133 | + difficulty.min = 100; |
| 134 | + difficulty.max = 500; |
| 135 | + break; |
| 136 | + default: |
| 137 | + difficulty.min = 500; |
| 138 | + difficulty.max = 1000; |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +// Start a new game |
| 143 | +const startGame = function startGame() { |
| 144 | + gameOverPanel.classList.remove('active'); |
| 145 | + hiScoreDisplay.classList.remove('new-score'); |
| 146 | + button.setAttribute('disabled', 'disabled'); |
| 147 | + scoreBoard.textContent = 0; |
| 148 | + timeUp = false; |
| 149 | + score = 0; |
| 150 | + currentHiScore = getHiScore(); |
| 151 | + peep(); |
| 152 | + startCountdown(); |
| 153 | + setTimeout(() => { |
| 154 | + endGame(); |
| 155 | + }, gameDuration); |
| 156 | +}; |
| 157 | + |
| 158 | +// Update somethings when game ends after set duration |
| 159 | +const endGame = function endGame() { |
| 160 | + // TIME'S UP!!! |
| 161 | + timeUp = true; |
| 162 | + // save the score |
| 163 | + saveHiScore(); |
| 164 | + // re-enable the New Game button |
| 165 | + button.removeAttribute('disabled'); |
| 166 | + // show "Game over" message; |
| 167 | + gameOverPanel.classList.add('active'); |
| 168 | +}; |
| 169 | + |
| 170 | +button.addEventListener('click', startGame); |
| 171 | +gameMode.addEventListener('change', toggleDifficulty); |
| 172 | +scoreReset.addEventListener('click', resetHiScore); |
| 173 | + |
| 174 | +moles.forEach(mole => mole.addEventListener('click', bonk)); |
0 commit comments