-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (183 loc) · 5.67 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* global $*/
/////////////////////////////////////////////////////////////////////////////
//////////////////////////// VARIABLES //////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Set window sessionStorage to keep track of high scores (lowest time)
sessionStorage.setItem("lowestTime", Infinity);
sessionStorage.setItem("lowestTimeStr", "0:00");
// DOM elements
const timer = $('#timer');
const board = $('#board');
const playButton = $('#play');
// timer variables
timer.hide();
let timeStr;
let time;
// board variables
const boardWidth = board.width();
const boardHeight = board.height();
// bubble variables
let bubblesLeft;
let bubbles;
// interval variables
let updateInterval;
let timerInterval;
// game constants
const NUM_BUBBLES = 10;
const POINTS_PER_BUBBLE = 3;
const SPEED = 10;
const ACCELERATION = 5;
alert("Pop the bubbles as fast as you can. Each bubble takes 3 taps to pop!")
/////////////////////////////////////////////////////////////////////////////
//////////////////////////// GAME SETUP /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// init();
/*
* Reset the clock, the bubbles and the intervals
* Create a new set of bubbles
* Start the intervals
*/
function init() {
// Create a new array for the bubbles
bubbles = [];
// Create the bubble DOM elements and add them to the board
// Add them to the array
for (let i = 0; i < NUM_BUBBLES; i++) {
const bubble = makeBubble(POINTS_PER_BUBBLE);
bubbles.push(bubble);
board.append(bubble);
}
bubblesLeft = bubbles.length;
// start the timer
timer.text("0:00");
time = 0;
timerInterval = setInterval(updateTimer, 1000);
// hide the play button and show the timer
playButton.hide();
timer.show();
//start game running
updateInterval = setInterval(update, 50);
}
/////////////////////////////////////////////////////////////////////////////
//////////////////////////// GAME LOGIC /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/*
* On each update tick update each bubble's position and check for
* collisions with the walls.
*/
function update() {
for (let i = 0; i < bubbles.length; i++) {
let bubble = bubbles[i];
updateBubblePosition(bubble);
keepInBounds(bubble);
}
if (bubblesLeft === 0) {
endGame();
playButton.show();
timer.hide();
}
}
function updateBubblePosition(bubble) {
bubble.x += (bubble.directionX * bubble.speed);
bubble.y += (bubble.directionY * bubble.speed);
bubble.css('left', bubble.x);
bubble.css('top', bubble.y);
}
function keepInBounds(bubble) {
let xMax = boardWidth - bubble.width();
let yMax = boardHeight - bubble.height();
let xMin = yMin = 0;
if (bubble.x > xMax) {
bubble.directionX = -1;
bubble.x = xMax;
}
else if (bubble.x < xMin) {
bubble.directionX = 1;
bubble.x = xMin;
}
if (bubble.y > yMax) {
bubble.directionY = -1;
bubble.y = yMax;
}
else if (bubble.y < yMin) {
bubble.directionY = 1;
bubble.y = yMin;
}
}
/* Making Bubbles */
function makeBubble(pointsPerBubble) {
// make the bubble Object
let bubble = $('<div>')
.addClass('bubble')
.css('background-color', randomRGB())
.text(pointsPerBubble);
// set bubble movement properties
bubble.x = Math.round(Math.random() * boardWidth);
bubble.y = Math.round(Math.random() * boardHeight);
bubble.speed = SPEED;
bubble.directionX = Math.round(Math.random()) ? 1 : -1;
bubble.directionY = Math.round(Math.random()) ? 1 : -1;
// initialize bubble points total
bubble.points = pointsPerBubble;
// set bubble click behavior
bubble.on('click', () => handleBubbleClick(bubble));
bubble.on('touchstart', () => handleBubbleClick(bubble));
return bubble;
}
/*
* When clicking on a bubble increase the bubble's speed,
* decrement its point value, and if the point value hits 0
* remove it
*/
function handleBubbleClick(bubble) {
bubble.speed += ACCELERATION;
bubble.points--;
if (bubble.points === 0) {
popBubble(bubble);
}
bubble.text(bubble.points);
}
/////////////////////////////////////////////////////////////////////////////
////////////////////////// HELPER FUNCTIONS /////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/*
* Each rgb value will be between 0 and 255
*/
function randomRGB() {
let r = Math.round(50 + Math.random() * 205);
let g = Math.round(50 + Math.random() * 205);
let b = Math.round(50 + Math.random() * 205);
return "rgb(" + r + "," + g + "," + b + ")";
}
/*
* When removing a bubble, animate the width and height down to
* 0 over 0.1 seconds and after the animation completes, remove
* the bubble from the DOM and decrement the bubblesLeft
*/
function popBubble(bubble) {
bubble.animate({ height: 0, width: 0 }, 100, function() {
bubble.remove();
bubblesLeft--;
});
}
function updateTimer() {
time++;
let secondsOnes = (time % 60) % 10;
let secondsTens = Math.floor((time % 60) / 10);
let minutes = Math.floor(time / 60);
timeStr = minutes + ":" + secondsTens + secondsOnes;
timer.text(timeStr);
}
function endGame() {
clearInterval(updateInterval);
clearInterval(timerInterval);
let lowestTime = sessionStorage.getItem("lowestTime");
let lowestTimeStr = sessionStorage.getItem("lowestTimeStr");
if (time < lowestTime) {
sessionStorage.setItem("lowestTime", time);
sessionStorage.setItem("lowestTimeStr", timeStr);
lowestTime = time;
lowestTimeStr = timeStr;
}
alert("Game Over\nTime: " + timeStr + "\nHigh Score: " + lowestTimeStr);
}