-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
91 lines (78 loc) · 2.17 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const playBtn = document.getElementById("play-btn");
const screens = document.querySelectorAll(".screen");
const choose_insect_btns = document.querySelectorAll(".insect-select");
const timeEl = document.getElementById('Time');
const scoreEl = document.getElementById('Score');
const game_container = document.querySelector(".game_container");
let seconds = 0;
let score = 0;
let selected_insect = {};
playBtn.addEventListener('click', function () {
screens[0].classList.add('up');
});
choose_insect_btns.forEach(btn => {
btn.addEventListener('click', function () {
const img = btn.querySelector('img');
const src = img.getAttribute('src');
const alt = img.getAttribute('alt');
selected_insect = {
src,
alt
};
screens[1].classList.add('up');
setTimeout(CreateInsect, 1000);
Startgame();
});
});
function incTime() {
let m = Math.floor(seconds / 60);
let s = seconds % 60;
m = m < 10 ? `0${m}` : m;
s = s < 10 ? `0${s}` : s;
timeEl.innerHTML = `Time: ${m}:${s}`;
seconds++;
}
function AddInsect() {
setTimeout(CreateInsect, 1000);
setTimeout(CreateInsect, 1500);
}
function CreateInsect() {
const insect = document.createElement('div');
const {
x,
y
} = GetRandomLoc();
insect.classList.add('insect');
insect.style.left = `${x}px`;
insect.style.top = `${y}px`;
insect.innerHTML = `<img src="${selected_insect.src}" arc="${
selected_insect.alt
}" style="transform: rotate(${Math.random() * 360}deg)"/>`;
insect.addEventListener('click', catchInsect);
game_container.appendChild(insect);
}
function catchInsect() {
incScore();
this.classList.add("catched");
setTimeout(() => {
this.remove();
}, 2000);
AddInsect();
}
function incScore() {
score++;
scoreEl.innerHTML = `Score: ${score}`;
}
function Startgame() {
setInterval(incTime, 1000);
}
function GetRandomLoc() {
const width = window.innerWidth;
const height = window.innerHeight;
const x = Math.random() * (width - 200) + 100;
const y = Math.random() * (height - 200) + 100;
return {
x,
y
};
}