-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
119 lines (99 loc) · 2.42 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
115
116
117
118
119
const startBtn = $('#start')
const screens = $('.screen')
const timeList = $('#time-list')
const timeEl = $('#time')
const board = $('#board')
let time = 0;
let score = 0;
startBtn.on('click', function (event) {
event.preventDefault()
screens.first().addClass('up')
});
timeList.on('click', function (event) {
if ($(event.target).hasClass('time-btn')) {
time = parseInt($(event.target).attr('data-time'))
$(screens[1]).addClass('up')
startGame()
}
})
board.on('click', function (event) {
if ($(event.target).hasClass('circle')) {
score++
createRandomCircles()
$(event.target).remove()
}
})
function startGame() {
setInterval(decreaseTime, 1000)
createRandomCircles()
setTime(time)
}
function decreaseTime() {
if (time === 0) {
finishGame()
} else {
let current = --time
if (current < 10) {
current = `0${current}`
}
setTime(current)
}
}
function setTime(value) {
timeEl.html(`00:${value}`)
}
function finishGame() {
timeEl.parent().addClass('hide')
board.html(`
<h1>Your score:
<span class="primary">${score}</span>
</h1>
<button class="time-btn" onclick="window.location.reload()">
Restart
</button>
`)
}
function createRandomCircles() {
const circle = $('<div></div>');
let size;
if ($(document.body).innerWidth() <= 516) {
size = getRandomNumber(15, 30)
} else if ($(document.body).innerWidth() <= 320) {
size = getRandomNumber(20, 40)
} else {
size = getRandomNumber(20, 60)
}
const height = board.height()
const width = board.width()
const x = getRandomNumber(0, width - size)
const y = getRandomNumber(0, height - size)
circle.css({
'top': `${x}px`,
'left': `${y}px`,
'width': `${size}px`,
'height': `${size}px`
});
circle.addClass('circle')
board.append(circle)
const color = getRandomColor()
circle.css('background', color)
}
function getRandomNumber(min, max) {
return Math.round(Math.random() * (max - min) + min)
}
function getRandomColor() {
const colors = ['#1abc9c', '#4efc53', '#3498db', '#9b59b6', '#ff3f34', '#f1c40f', '#f57e33', '#48dbfb']
return colors[Math.floor(Math.random() * colors.length)];
}
/*
// to use the hack, simply call the winTheGame function
winTheGame()
function winTheGame() {
function kill() {
const circle = document.querySelector('.circle')
if (circle) {
circle.click()
}
}
setInterval(kill, 50)
} */