Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
// Haal alle HTML-elementen op die nodig zijn voor het spel
const square = document.querySelectorAll('.square')
const mole = document.querySelector('.mole')
const timeleft = document.querySelector('#time-left')
const score = document.querySelector('#score')
const highScore = document.querySelector('#highScore')
// Initialisatie van variabelen
let result = 0
let hitPosition
let currentTime = 60
let timerid = null
let miss = 0
let previousHighScore = 0;
// Roep de functie aan om de vorige highscore weer te geven
displayPreviousHighScore();
// Functie voor het willekeurig plaatsen van de mol op het speelveld
function randomSquare() {
square.forEach(square => {
square.classList.remove('mole')
})
let randomSquare = square[Math.floor(Math.random() * 9)]
randomSquare.classList.add('mole')
hitPosition = randomSquare.id
}
// Voeg een eventlistener toe aan elk vakje (square) om gebruikersinteractie te detecteren
square.forEach(square => {
square.addEventListener('mousedown', () => {
if (square.id == hitPosition) {
result++
score.textContent = result
hitPosition = null
}
else{
miss++
Misses.textContent = miss
}
})
})
// Functie om de mol periodiek te verplaatsen
function moveMole() {
timerId = setInterval(randomSquare, 750)
}
// Functie voor toekomstige functionaliteit (leeg op dit moment)
function crymole() {
// Mogelijke toekomstige uitbreiding van het spel
}
// Start het bewegen van de mol
moveMole()
// Functie voor het aftellen van de speeltijd
function countDown() {
currentTime--
timeleft.textContent = currentTime
if (currentTime == 0 || miss>=10) {
clearInterval(countDownTimerId)
clearInterval(timerid)
if (confirm('GAME OVER! final score:' + (result - miss))) {
// cookie schrijven en het spel opnieuw starten
let finalScore = result - miss;
if (finalScore > previousHighScore) {
setHighScore(finalScore);
displayPreviousHighScore();
}
location.reload();
}
}
}
// Start het aftellen
let countDownTimerId = setInterval(countDown, 1000)
// Functie om de highscore in een cookie op te slaan
function setHighScore(score) {
// Stel de cookie 'highScore' in met de opgegeven score en een vervaldatum (bijv. 365 dagen)
document.cookie = 'highScore=' + score + ';expires=' + new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000).toUTCString() + ';path=/';
}
// Functie om de highscore uit de cookie te halen
function getHighScore() {
// Lees de waarde van de cookie 'highScore' en retourneer de highscore, of 0 als de cookie niet bestaat
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)highScore\s*=\s*([^;]*).*$)|^.*$/, "$1");
return cookieValue ? parseInt(cookieValue) : 0;
}
// Functie om de cookie-waarde van de vorige highscore te lezen en weer te geven
function displayPreviousHighScore() {
highScore.textContent = getHighScore();
previousHighScore = getHighScore();
}