import { randomHole } from "./mole.js";
export const game = {
score: 0,
time: 30,
timer: null,
moleTimer: null,
// Получаем элементы DOM
holes: document.querySelectorAll('.hole'),
scoreDisplay: document.querySelector('#score'),
timeDisplay: document.querySelector('#time'),
startMoles() {
this.moleTimer = setInterval(() => {
randomHole(this.holes);
}, 700);
},
start() {
// Сброс настроек перед началом
this.score = 0;
this.time = 30;
this.scoreDisplay.textContent = this.score;
this.timeDisplay.textContent = this.time;
// Очистка старых таймеров, если они были
clearInterval(this.timer);
clearInterval(this.moleTimer);
this.startMoles();
this.timer = setInterval(() => {
this.time--;
this.timeDisplay.textContent = this.time;
if (this.time <= 0) {
clearInterval(this.timer);
clearInterval(this.moleTimer);
alert('Игра окончена! Ваши очки: ' + this.score);
// Убираем последнего крота с поля
this.holes.forEach(h => h.classList.remove('mole'));
}
}, 1000);
},
addEvents() {
this.holes.forEach(hole => {
hole.addEventListener('click', () => {
// Если в лунке есть крот (проверяем класс)
if (hole.classList.contains('mole')) {
this.score++;
this.scoreDisplay.textContent = this.score;
hole.classList.remove('mole'); // Крот исчезает при попадании
}
});
});
}
};