-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
114 lines (96 loc) · 3.35 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const screens = document.querySelectorAll('.screen')
const play_btn = document.getElementById('play-btn')
const choose_insects = document.querySelectorAll('.choose-insect')
const game_container = document.getElementById('game-container')
const timeElement = document.getElementById('time')
const scoreElement = document.getElementById('score')
const messageElement = document.getElementById('message')
const continue_gameElement = document.getElementById('continue-game')
const yes_select_btn = document.getElementById('yes')
const no_select_btn = document.getElementById('no')
const continue_btn = document.getElementById('continue')
// const originalMessage = messageElement.innerHTML
let seconds = 0
let score = 0
let selected_insect = {}
let messageShow = [9,]
play_btn.addEventListener('click', () => {
screens[0].classList.add('up')
})
choose_insects.forEach(btn => {
btn.addEventListener('click', () => {
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)
console.log(src)
startGame()
})
})
function startGame() {
setInterval(increaseTime, 1000)
}
function increaseTime() {
let m = Math.floor(seconds / 60)
let s = seconds % 60
m = m < 10 ? `0${m}` : m
s = s < 10 ? `0${s}` : s
timeElement.innerHTML = `Time: ${m}:${s}`
seconds++
}
function createInsect() {
const insect = document.createElement('div')
insect.classList.add('insect')
const { x, y } = getRandomLocation()
insect.style.top = `${y}px`
insect.style.left = `${x}px`
insect.innerHTML = `<img src="${selected_insect.src}" alt="${selected_insect.alt}" style="transform: rotate(${Math.random() * 360}deg) "/>`
insect.addEventListener('click', catchInsect)
game_container.appendChild(insect)
}
function getRandomLocation() {
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 }
}
function catchInsect() {
this.classList.add('caught')
increaseScore()
setTimeout(() => this.remove(), 2000)
addInsects()
}
function increaseScore() {
let x = 10
console.log(x)
score += 1
if (score == x) {
messageElement.classList.add('visible')
yes_select_btn.addEventListener('click', yes)
no_select_btn.addEventListener('click', no)
}
scoreElement.innerHTML = `Score: ${score}`
x = x + 10
}
function addInsects() {
setTimeout(createInsect, 1000)
setTimeout(createInsect, 1500)
}
function yes() {
messageElement.classList.remove('visible')
continue_gameElement.classList.add('visible')
continue_btn.addEventListener('click', continue_game)
}
function no() {
messageElement.classList.remove('visible')
continue_gameElement.innerHTML = `<p>Congratulations, you earned <span>${score}</span> points!</p><button class="btn-select" id="continue">Continue Forever?</button>`
continue_gameElement.classList.add('visible')
const continue_btn = document.getElementById('continue')
continue_btn.addEventListener('click', continue_game)
}
function continue_game() {
setTimeout(continue_gameElement.classList.remove('visible'), 2000)
}