-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbubbleshooter.html
372 lines (333 loc) · 9.6 KB
/
bubbleshooter.html
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html lang="en">
<head>
<h1>PLAYERVPN</h1>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home | Schoology</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div align='center'>
<canvas id="game-canvas" style='maring:0 auto; background: #eee' width="480" height="320"></canvas>
</div>
<script type='application/javascript'
src="https://rawgit.com/nicholas-maltbie/BubbleShooterJS/master/grid.js">
</script>
<script type='application/javascript'
src="https://rawgit.com/nicholas-maltbie/BubbleShooterJS/master/ball.js">
</script>
<script type='application/javascript'
src="https://rawgit.com/nicholas-maltbie/BubbleShooterJS/master/shooter.js">
</script>
<script type='application/javascript'
src="https://rawgit.com/nicholas-maltbie/BubbleShooterJS/master/manager.js">
</script>
<script>
var canvas = document.getElementById("game-canvas");
var ctx = canvas.getContext("2d");
var rectangle = canvas.getBoundingClientRect();
var mouse = {};
var game_grid = null;
var game_width = canvas.width;
var game_height = canvas.height;
var fixed = false;
var defaultZ = canvas.style.zIndex
var initial_colors = ['red', 'blue', '#eddd2d', '#54e202']
var add_colors = ['#00d8ff', 'magenta', '#c46907']
var game_colors = initial_colors.slice(0)
mouse.x = 0;
mouse.y = 0;
mouse.down = 0;
mouse.prev_down = 0;
mouse.held = 0;
//setup mouse listener
document.addEventListener('mousemove', mouse_move, false)
canvas.addEventListener('mousedown', function(evt) {mouse.down = 1}, false)
canvas.addEventListener('mouseup', function(evt) {mouse.down = 0; click_buttons(evt)}, false)
document.addEventListener('touchmove', touch_move, false)
canvas.addEventListener('touchstart', function(evt) {mouse.down = 1; touch_move(evt)}, false)
canvas.addEventListener('touchend', function(evt) {mouse.down = 0; click_buttons(evt)}, false)
//setup rescale listener
window.onresize = function(evt) {rescale()};
var delay = 10 //delay between frames, 10 ms
//Get start time
var prev_time = new Date().getTime()
//List of all things to draw on the screen.
var game_objects = {}
var object_layers = {}
var buttons = {}
var layers = []
var added = 0
var sf = 1
//Max elapsed time per frame
var max_elapsed = 25
//call setup function
setup()
function get_color()
{
return game_colors[Math.floor(Math.random() * game_colors.length)];
}
function touch_move(e)
{
var touch = e.touches[0];
rectangle = canvas.getBoundingClientRect();
var x = touch.clientX - rectangle.left;
var y = touch.clientY - rectangle.top;
mouse.x = x / sf;
mouse.y = y / sf;
}
//function to track mouse movement
function mouse_move(e)
{
rectangle = canvas.getBoundingClientRect();
var x = e.clientX - rectangle.left;
var y = e.clientY - rectangle.top;
mouse.x = x / sf;
mouse.y = y / sf;
}
function click_buttons(evt) {
if(mouse.down == 0 && mouse.prev_down == 1) {
button_ids = Object.keys(buttons);
for(var index = 0; index < button_ids.length; index++) {
button = buttons[button_ids[index]]
if (button.intersect(mouse.x, mouse.y)) {
button.fn()
}
}
}
}
//Adds a button to the screen
function add_button(button, layer=0)
{
id = add_object(button, layer)
buttons[id] = button
return id
}
//Removes a button from the screen
function remove_button(id)
{
console.log(id)
if(id in buttons) {
remove_object[id]
delete buttons[id]
}
}
//Adds an object and returns the object's id
function add_object(object, layer=0)
{
game_objects[added] = object
object.id = added
object.layer = layer;
//check if the layer exists, if not create it
if (!(layer in object_layers)) {
object_layers[layer] = {}
layers.push(layer);
layers.sort(function(a, b) {
aNum = parseInt(a)
bNum = parseInt(b);
if (aNum == bNum)
return 0
if (aNum > bNum)
return 1
return 0
})
}
//Add object to layer
object_layers[layer][object.id] = 0
added += 1
return object.id
}
//Removes an object from the draw hash table
function remove_object(id)
{
//find object layer and delete it from layer
for (var index = 0; index < layers.length; index++) {
layer = layers[index]
if(id in object_layers[layer]) {
delete object_layers[layer][id]
}
}
//remove object from game_objects
return delete game_objects[id]
}
//resets the game
function reset()
{
game_colors = initial_colors.slice(0);
game_manager.remove_self()
ball_shooter.remove_self()
game_grid.remove_self()
//add ball shooter
ball_shooter = new shooter(game_width / 2, game_height - 20, 10, 75, 400, get_color);
add_object(ball_shooter, -1)
ball_shooter.load(get_color);
//Create game grid
game_grid = new grid(22, 10, 1, 14, 10);
//add game grid
add_object(game_grid)
//add a ball to the grid
game_grid.add_rows(get_color, 5)
game_manager = new manager(ball_shooter, game_grid)
add_object(game_manager, 10)
}
//draw function
function draw()
{
//clear canvas at start of frame
clear();
//get current time
var date = new Date()
var time = date.getTime()
//calculate elapsed (in seconds)
var elapsed = (time - prev_time) / 1000.0
elapsed = Math.min(elapsed, max_elapsed)
//iterate over the game objects and draw them all
//start out at layer 0, then progress up
layers.forEach( function(layer) {
//get keys
object_keys = Object.keys(object_layers[layer])
for(var index = 0; index < object_keys.length; index++)
{
if (object_keys[index] in game_objects)
game_objects[object_keys[index]].draw(elapsed)
}
})
//update mouse
mouse.prev_down = mouse.down
if (mouse.down)
{
mouse.held += elapsed
}
else
{
mouse.held = 0
}
//update previous time
prev_time = time
}
function rescale() {
canvas.width = game_width
canvas.height = game_height
sf = 1
canvas.style.zIndex = defaultZ
canvas.style.position = 'relative'
ctx.scale(1, 1)
canvas.style.left = 0
canvas.style.top = 0
if(fixed) {
width = window.innerWidth;
height = window.innerHeight;
temp_sf = Math.min(width / canvas.width, height / canvas.height)
sf = temp_sf
canvas.width = canvas.width * sf
canvas.height = canvas.height * sf
ctx.scale(sf, sf)
canvas.style.position = 'fixed'
canvas.style.zIndex = '999'
canvas.style.left = window.innerWidth / 2 - canvas.width / 2
canvas.style.top = window.innerHeight / 2 - canvas.height / 2
}
}
//This function will clear the canvas between frames
function clear()
{
ctx.clearRect(0, 0, game_width, game_height);
}
var ball_shooter
var game_grid
var game_manager
//setup the scene
function setup()
{
//add ball shooter
ball_shooter = new shooter(game_width / 2, game_height - 20, 10, 75, 400, get_color);
add_object(ball_shooter, -1)
ball_shooter.load(get_color);
//Create game grid
game_grid = new grid(22, 10, 1, 14, 10);
//add game grid
add_object(game_grid)
//add a ball to the grid
game_grid.add_rows(get_color, 5)
game_manager = new manager(ball_shooter, game_grid)
add_object(game_manager, 10)
rescale()
}
function draw_button(x, y, content, gap=10, text_size=30, border_radius = 10,
border_thickness=3, font="Comic Sans MS", fill='#eee', text_color='red',
fill_hover='#ccc', text_hover='#ddd', fill_down='#aaa', text_down='#bbb',
border='black', text_border='red')
{
ctx.textAlign = "center";
pressed = false
ctx.font = text_size + "px " + font;
var retry = content
var box_width = ctx.measureText(retry).width
if(mouse.x >= x - box_width / 2 - gap &&
mouse.x <= x - box_width / 2 + box_width + gap &&
mouse.y >= y && mouse.y <= y + text_size + gap)
{
fill = fill_hover
text_color = text_hover
if(mouse.down) {
fill = fill_down
text_color = text_down
}
if(mouse.prev_down && !mouse.down)
{
pressed = true;
}
}
ctx.fillStyle = fill;
fillRoundRect(x - box_width / 2 - gap, y,
box_width + gap * 2, text_size + gap, border_radius)
ctx.fillStyle = border
roundRect(x - box_width / 2 - gap, y,
box_width + gap * 2, text_size + gap, border_radius, border_thickness)
ctx.fillStyle = text_color
ctx.fillText(retry, x, y + text_size)
ctx.fillStyle = text_border
ctx.lineWidth = 1
ctx.strokeText(retry, x, y + text_size)
return pressed;
}
function fillRoundRect(x, y, w, h, radius)
{
var r = x + w;
var b = y + h;
ctx.beginPath();
ctx.lineWidth="4";
ctx.moveTo(x+radius, y);
ctx.lineTo(r-radius, y);
ctx.quadraticCurveTo(r, y, r, y+radius);
ctx.lineTo(r, y+h-radius);
ctx.quadraticCurveTo(r, b, r-radius, b);
ctx.lineTo(x+radius, b);
ctx.quadraticCurveTo(x, b, x, b-radius);
ctx.lineTo(x, y+radius);
ctx.quadraticCurveTo(x, y, x+radius, y);
ctx.fill();
}
function roundRect(x, y, w, h, radius, thickness=4)
{
var r = x + w;
var b = y + h;
ctx.beginPath();
ctx.lineWidth=thickness;
ctx.moveTo(x+radius, y);
ctx.lineTo(r-radius, y);
ctx.quadraticCurveTo(r, y, r, y+radius);
ctx.lineTo(r, y+h-radius);
ctx.quadraticCurveTo(r, b, r-radius, b);
ctx.lineTo(x+radius, b);
ctx.quadraticCurveTo(x, b, x, b-radius);
ctx.lineTo(x, y+radius);
ctx.quadraticCurveTo(x, y, x+radius, y);
ctx.stroke();
}
setInterval(draw, delay)
</script>
</body>
</html>