-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (61 loc) · 1.94 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
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const moles = document.querySelectorAll('.mole');
const countdownBoard = document.querySelector('.countdown');
const startButton = document.querySelector('.startButton');
let lastHole;
let timeUp = false;
let timeLimit = 20000;
let score = 0;
let countdown;
function pickRandomHole(holes) {
const randomHole = Math.floor(Math.random() * holes.length);
const hole = holes[randomHole];
if(hole === lastHole){
return pickRandomHole(holes);
}
lastHole = hole;
return hole;
}
function popOut(){
const time = Math.random() * 1300 + 400;
const hole = pickRandomHole(holes);
hole.classList.add('up');
setTimeout(function(){ // !!!!!!!!!
hole.classList.remove('up');
if (!timeUp) popOut();
}, time);
}
function startGame(){
countdown = timeLimit/1000;
scoreBoard.textContent = 0;
scoreBoard.style.display = 'block';
countdownBoard.textContent = countdown;
timeUp = false;
score = 0;
popOut();
setTimeout(function(){
timeUp = true;
}, timeLimit);
let startCountdown = setInterval(function(){
countdown -= 1;
countdownBoard.textContent = countdown;
if (countdown < 0){
countdown = 0;
clearInterval(startCountdown);
countdownBoard.textContent = 'Times UP!!! nice try ! This is the way!';
}
}, 1000);
}
startButton.addEventListener('click', startGame);
function whack(e){
score++;
this.style.backgroundImage = 'url("yoda2.png")';
this.style.pointerEvents = 'none'
setTimeout(() => {
this.style.backgroundImage = 'url("yoda1.png")';
this.style.pointerEvents = 'all';
}, 800)
scoreBoard.textContent = score;
}
moles.forEach(mole => mole.addEventListener('click', whack));