-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (97 loc) · 2.82 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const points = document.querySelector('.points');
const items = document.querySelectorAll('.item');
const moles = document.querySelectorAll('.mole');
const startBtn = document.querySelector('.start');
const resetBtn = document.querySelector('.reset');
const timer = document.querySelector('.time');
const gameField = document.querySelector('.container');
const scorePanel = document.querySelector('.score-panel');
const closeBtn = document.querySelector('.close');
let point = 0;
let time = 60;
let click = 0;
let interval;
let timeInterval;
const startGame = () => {
click = 0;
startBtn.classList.remove('block');
startBtn.classList.add('none');
timeInterval = setInterval(startTimer, 1000);
interval = setInterval(showMole, 800);
gameField.addEventListener('click', clickCounter);
};
const startTimer = () => {
time--;
timer.textContent = `${time}`;
if (time <= 0) {
endGame();
}
};
function showMole() {
let index = Math.floor(Math.random() * 9);
moles[index].classList.add('block');
moles[index].classList.remove('none');
moles.forEach((el) => {
if (el.classList.contains('block')) {
const removeMole = () => {
moles[index].classList.remove('block');
moles[index].classList.add('none');
};
setTimeout(removeMole, 500);
}
});
}
const addPoint = (e) => {
point++;
points.textContent = `${point}`;
e.target.classList.remove('block');
e.target.classList.add('none');
};
const reset = () => {
point = 0;
time = 60;
click = 0;
points.textContent = `${point}`;
timer.textContent = `${time}`;
startBtn.classList.add('block');
clearInterval(interval);
clearInterval(timeInterval);
};
const clickCounter = () => {
click++;
};
const endGame = () => {
const score = document.querySelector('.score');
const clicks = document.querySelector('.shots');
const onTarget = document.querySelector('.on-target');
const personalBestTxt = document.querySelector('.pb');
const procent = point / click;
let personalBest = localStorage.getItem('score');
if (
parseFloat(personalBest) < `${point}` ||
localStorage.getItem('score') === null
) {
localStorage.setItem('score', `${point}`);
}
clearInterval(timeInterval);
clearInterval(interval);
score.textContent = `Score: ${point}`;
clicks.textContent = `Shots: ${click}`;
onTarget.textContent = `On target: ${(procent.toFixed(2) * 100).toFixed(0)}%`;
personalBestTxt.textContent = 'PB: ' + localStorage.getItem('score');
scorePanel.classList.add('flex');
scorePanel.classList.remove('none');
};
const closeScorePanel = () => {
if (scorePanel.classList.contains('flex')) {
scorePanel.classList.remove('flex');
scorePanel.classList.add('none');
}
reset();
};
startBtn.addEventListener('click', startGame);
moles.forEach((el) => {
el.addEventListener('click', addPoint);
});
resetBtn.addEventListener('click', reset);
closeBtn.addEventListener('click', closeScorePanel);