-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
65 lines (50 loc) · 1.64 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
const game = {};
game.randomTime = (min, max) => {
return Math.round(Math.random() * (max - min) + min);
}
game.randomHole = (holes) => {
const index = Math.floor(Math.random() * game.holesArray.length);
const hole = game.holesArray[index];
if (hole === game.lastHole) {
return game.randomHole(hole);
} //prevents repeating holes
game.lastHole = hole;
return hole;
}
game.peep = () => {
const time = game.randomTime(500, 1000);
const hole = game.randomHole(game.holesArray);
hole.classList.add('up');
setTimeout(() => {
hole.classList.remove('up');
if (!game.timeUp) game.peep();
}, time);
}
game.startGame = () => {
game.score.textContent = 0; // for restarts
game.timeUp = false; // for restarts
game.counter = 0; // for restarts
game.peep();
setTimeout(() => game.timeUp = true, 10000);
}
game.clickHandler = (event) => {
if(event.isTrusted === false){
return; // cheating cheaters
} else {
game.counter++;
event.currentTarget.classList.remove('up'); // .up is removed from .hole
game.score.textContent = game.counter;
}
}
game.init = () => {
game.holesArray = Array.from(document.querySelectorAll('.hole'));
game.digArray = Array.from(document.querySelectorAll('.dig'));
game.score = document.querySelector('.score');
game.button = document.querySelector('.button');
game.lastHole;
game.timeUp = false;
game.counter = 0;
game.button.addEventListener('click', game.startGame);
game.digArray.forEach(dig => dig.addEventListener('click', game.clickHandler));
}
game.init();