-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (94 loc) · 2.84 KB
/
index.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
108
109
110
111
112
const startBtn = document.querySelector('#start');
const screens = document.querySelectorAll('.screen');
const timeList = document.querySelector('#time-list');
const timer = document.querySelector('#time');
const board = document.querySelector('#board');
let time = 0;
let score = 0;
const colors = ['#EA643D', '#FFAA0C', '#F2DB4E', '#4FEA5A', '#30EDB0', '#64D1EA', '#52A9F2', '#BB6CE1', '#E857C9', '#F9207F'];
const shoot = new Audio();
shoot.src = './assets/sound/blaster.mp3';
const gameSd = new Audio();
gameSd.src = './assets/sound/gameOver.mp3';
gameSd.loop = false;
startBtn.addEventListener('click', (event) => {
event.preventDefault();
screens[0].classList.add('up');
})
timeList.addEventListener('click', event => {
if (event.target.classList.contains('time-btn')) {
time = parseInt(event.target.getAttribute('data-time'));
screens[1].classList.add('up');
startGame();
}
})
board.addEventListener('click', event => {
if (event.target.classList.contains('circle')) {
score++;
event.target.remove();
createRandomCircle();
shoot.play();
}
})
function startGame() {
setInterval(decreaseTime, 1000);
createRandomCircle();
setTime(time);
}
function decreaseTime() {
if (time === 0) {
finishGame();
} else {
let current = --time;
if (current < 10) {
current = `0${current}`;
}
setTime(current);
}
}
function setTime(value) {
timer.innerHTML = `00:${value}`;
}
function finishGame() {
timer.parentNode.classList.add('hide');
gameSd.play();
board.innerHTML = `<div class="end_table">
<h1>Score: <span class="primary">${score}</span></h1>
<button class="btn-try-again">Try again</button>
</div>`
const btnTryAgain = document.querySelector('.btn-try-again');
btnTryAgain.addEventListener('click', tryAgain);
}
function createRandomCircle() {
const circle = document.createElement('div');
const size = getRandom(10, 60);
const { width, height } = board.getBoundingClientRect();
const h = getRandom(0, height - size);
const w = getRandom(0, width - size);
circle.classList.add('circle');
circle.style.height = `${size}px`;
circle.style.width = `${size}px`;
circle.style.top = `${h}px`;
circle.style.left = `${w}px`;
const color = getRandomColor();
circle.style.background = color;
board.append(circle);
}
function getRandom(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function getRandomColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
const tryAgain = () => {
window.location.reload();
}
function theGame() {
function shoot() {
const circle = document.querySelector('.circle');
if (circle) {
circle.click();
}
}
setInterval(shoot, 50);
}