-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (140 loc) · 3.99 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Buttons
// #region GAME LOGIC AND DATA
// data
let clickCount = 0
let height = 120
let width = 100
let inflationRate = 20
let maxsize = 300
let highestPopCount = 0
let currentPopCount = 0
let gameLength = 10000
let clockId = 0
let timeRemaining = 0
let currentPlayer = {}
let currentColor = "red"
let possibleColors = ["red", "green", "blue", "purple", "pink"]
function startGame() {
document.getElementById("game-controls").classList.remove("hidden")
document.getElementById("main-controls").classList.add("hidden")
document.getElementById("scoreboard").classList.add("hidden")
startClock()
setTimeout(stopGame, gameLength)
}
function startClock() {
timeRemaining = gameLength
drawClock()
clockId = setInterval(drawClock, 1000)
}
function stopClock() {
clearInterval(clockId)
}
function drawClock() {
let counntdownElem = document.getElementById('countdown')
counntdownElem.innerText = (timeRemaining / 1000).toString()
timeRemaining -= 1000
}
function inflate() {
clickCount++
height += inflationRate
width += inflationRate
checkBalloonPop()
draw()
}
function checkBalloonPop() {
if (height >= maxsize) {
console.log("pop the balloon")
let balloonElement = document.getElementById("balloon")
balloonElement.classList.remove(currentColor)
getRandomColor()
balloonElement.classList.add(currentColor)
document.getElementById("pop-sound").play()
currentPopCount++
height = 0
width = 0
}
}
function getRandomColor() {
let i = Math.floor(Math.random() * possibleColors.length);
currentColor = possibleColors[i]
}
function draw() {
let balloonElement = document.getElementById("balloon")
let clickCountElem = document.getElementById("click-count")
let popCountElem = document.getElementById('pop-count')
let highPopCountElem = document.getElementById('high-pop-count')
let playerNameElem = document.getElementById('player-name')
balloonElement.style.height = height + "px"
balloonElement.style.width = height + "px"
clickCountElem.innerText = clickCount.toString()
popCountElem.innerText = currentPopCount.toString()
highPopCountElem.innerText = currentPlayer.topScore.toString()
playerNameElem.innerText = currentPlayer.name
}
function stopGame() {
console.log("the game is over")
document.getElementById("main-controls").classList.remove("hidden")
document.getElementById("game-controls").classList.add("hidden")
document.getElementById("scoreboard").classList.remove("hidden")
clickCount = 0
height = 120
width = 100
if (currentPopCount > currentPlayer.topScore) {
currentPlayer.topScore = currentPopCount
savePlayers
}
currentPopCount = 0
stopClock()
draw()
drawScoreboard()
}
// #endregion
let players = []
loadPlayers()
function setPlayer(event) {
event.preventDefault()
let form = event.target
let playerName = form.playerName.value
currentPlayer = players.find(player => player.name == playerName)
if (!currentPlayer) {
currentPlayer = { name: playerName, topScore: 0 }
players.push(currentPlayer)
savePlayers()
}
console.log(currentPlayer)
form.reset()
document.getElementById("game").classList.remove("hidden")
form.classList.add("hidden")
draw()
drawScoreboard()
}
function changePlayer() {
document.getElementById("player-form").classList.remove("hidden")
document.getElementById("game").classList.add("hidden")
}
function savePlayers() {
window.localStorage.setItem("players", JSON.stringify(players))
}
function loadPlayers() {
let playersData = JSON.parse(window.localStorage.getItem("players"))
if (playersData) {
players = playersData
}
}
function drawScoreboard() {
let template = ""
players.sort((p1, p2) => p2.topScore - p1.topScore)
players.forEach(player => {
template += `
<div class="d-flex space-between">
<span>
<i class="fa fa-user"></i>
${player.name}
</span>
<span>Score: ${player.topScore}</span>
</div>
`
})
document.getElementById("players").innerHTML = template
}
drawScoreboard()