-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs
More file actions
86 lines (74 loc) · 2.32 KB
/
js
File metadata and controls
86 lines (74 loc) · 2.32 KB
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
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelectorAll('.score');
const moles = document.querySelectorAll('.mole');
const countdownBoard = document.querySelector('.countdown');
const startButton = document.querySelectorAll('.startButton');
let lastHole;
let timeUp = false;
let timeLimit = 20000;
let score = 0;
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);
}
popOut();
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!! Thank you for protecting our planet! This is the way!';
}
}, 1000);
}
startButton.addEventListener('click', startGame);
function whack(e){
score++;
this.style.backgroundImage = 'url("baby-yoda-reaching.jpg")';
this.style.pointerEvents = 'none';
setTimeout(() => {
this.style.backgroundImage = 'url("baby-yoda-sleeping.png")';
}, 800);
scoreBoard.textContent = score;
}
moles.forEach(mole.addEventListener('click', whack));
//////
function startGame() {
scoreBoard.textContent = 0;
timeUp = false;
score = 0;
peep();
setTimeout(() => timeUp = true, 15000) //show random moles for 15 seconds
}
function whack(e){
if(!e.isTrusted) return; //** new thing I learned */
score++;
this.parentNode.classList.remove('up'); //this refers to item clicked
scoreBoard.textContent = score;
}