-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballons.js
74 lines (58 loc) · 1.77 KB
/
ballons.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
document.addEventListener('DOMContentLoaded', function () {
//Balloons game
const balloons = document.querySelectorAll('.balloon');
const scoreBoard = document.querySelector('.score');
const newGame = document.querySelector('#newGame');
const blackBalloon = document.querySelectorAll('.blackBalloon');
let lastBalloon;
let timeUp = false;
let score = 0;
function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function randomBalloon(balloons) {
const idx = Math.floor(Math.random() * balloons.length);
const balloon = balloons[idx];
if (balloon === lastBalloon) {
return randomBalloon(balloons);
}
lastBalloon = balloon;
return balloon;
}
function flyingBalloon() {
const time = randomTime(500, 1000);
const balloon = randomBalloon(balloons);
balloon.classList.add('up');
setTimeout(() => {
balloon.classList.remove('up');
if (!timeUp) flyingBalloon();
}, time);
}
function start() {
scoreBoard.textContent = 0;
timeUp = false;
score = 0;
flyingBalloon();
newGame.style.display = 'none';
setTimeout(() => {
timeUp = true;
newGame.style.display = 'block';
}, 18000)
}
document.getElementById("newGame").addEventListener("click", start);
function clickedBalloon(e) {
if (!e.isTrusted) return;
score++;
this.parentNode.classList.remove('up');
scoreBoard.textContent = score;
}
function hideBalloon(e) {
if (!e.isTrusted) return;
this.style.display = 'none';
setTimeout(() => {
this.style.display = 'block';
}, 1000)
}
blackBalloon.forEach(mole => mole.addEventListener('click', clickedBalloon));
blackBalloon.forEach(mole => mole.addEventListener('click', hideBalloon));
});