-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch3-example.js
More file actions
854 lines (712 loc) · 28.2 KB
/
match3-example.js
File metadata and controls
854 lines (712 loc) · 28.2 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
// ------------------------------------------------------------------------
// How To Make A Match-3 Game With HTML5 Canvas
// inspiration Rembound
window.onload = function() {
// Get the canvas and context
var canvas = document.getElementById("viewport");
var context = canvas.getContext("2d");
// Timing and frames per second
var lastframe = 0;
var fpstime = 0;
var framecount = 0;
var fps = 0;
var moi = new Audio();
moi.src = "food.mp3";
var lite = new Image();
lite.src = "img/tile.png";
var lite2 = new Image();
lite2.src = "img/tile2.png";
// Mouse dragging
var drag = false;
// Level object
var level = {
x: 250, // X position
y: 113, // Y position
columns: 8, // Number of tile columns
rows: 8, // Number of tile rows
tilewidth: 40, // Visual width of a tile
tileheight: 40, // Visual height of a tile
tiles: [], // The two-dimensional tile array
selectedtile: { selected: false, column: 0, row: 0 }
};
// All of the different tile colors in RGB
var tilecolors =
[[0],
[0],
[0],
[0],
[0],
[0],
[0]];
// Clusters and moves that were found
var clusters = []; // { column, row, length, horizontal }
var moves = []; // { column1, row1, column2, row2 }
// Current move
var currentmove = { column1: 0, row1: 0, column2: 0, row2: 0 };
// Game states
var gamestates = { init: 0, ready: 1, resolve: 2 };
var gamestate = gamestates.init;
// Score
var score = 0;
// Animation variables
var animationstate = 0;
var animationtime = 0;
var animationtimetotal = 0.2;
// Show available moves
var showmoves = false;
// The AI bot
var aibot = false;
// Game Over
var gameover = false;
// Gui buttons
var buttons = [ { x: 30, y: 240, width: 150, height: 50, text: "New Game"},
{ x: 30, y: 300, width: 150, height: 50, text: "Show Moves"},
{ x: 30, y: 360, width: 150, height: 50, text: "Enable AI Bot"}];
// Initialize the game
function init() {
// Add mouse events
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", onMouseDown);
canvas.addEventListener("mouseup", onMouseUp);
canvas.addEventListener("mouseout", onMouseOut);
// Initialize the two-dimensional tile array
for (var i=0; i<level.columns; i++) {
level.tiles[i] = [];
for (var j=0; j<level.rows; j++) {
// Define a tile type and a shift parameter for animation
level.tiles[i][j] = { type: 0, shift:0 }
}
}
// New game
newGame();
// Enter main loop
main(0);
}
// Main loop
function main(tframe) {
// Request animation frames
window.requestAnimationFrame(main);
// Update and render the game
update(tframe);
render();
}
// Update the game state
function update(tframe) {
var dt = (tframe - lastframe) / 1000;
lastframe = tframe;
// Update the fps counter
updateFps(dt);
if (gamestate == gamestates.ready) {
// Game is ready for player input
// Check for game over
if (moves.length <= 0) {
gameover = true;
}
// Let the AI bot make a move, if enabled
if (aibot) {
animationtime += dt;
if (animationtime > animationtimetotal) {
// Check if there are moves available
findMoves();
if (moves.length > 0) {
// Get a random valid move
var move = moves[Math.floor(Math.random() * moves.length)];
// Simulate a player using the mouse to swap two tiles
mouseSwap(move.column1, move.row1, move.column2, move.row2);
} else {
// No moves left, Game Over. We could start a new game.
// newGame();
}
animationtime = 0;
}
}
} else if (gamestate == gamestates.resolve) {
// Game is busy resolving and animating clusters
animationtime += dt;
if (animationstate == 0) {
// Clusters need to be found and removed
if (animationtime > animationtimetotal) {
// Find clusters
findClusters();
if (clusters.length > 0) {
// Add points to the score
for (var i=0; i<clusters.length; i++) {
// Add extra points for longer clusters
score += 100 * (clusters[i].length - 2);
moi.play();
}
// Clusters found, remove them
removeClusters();
// Tiles need to be shifted
animationstate = 1;
} else {
// No clusters found, animation complete
gamestate = gamestates.ready;
}
animationtime = 0;
}
} else if (animationstate == 1) {
// Tiles need to be shifted
if (animationtime > animationtimetotal) {
// Shift tiles
shiftTiles();
// New clusters need to be found
animationstate = 0;
animationtime = 0;
// Check if there are new clusters
findClusters();
if (clusters.length <= 0) {
// Animation complete
gamestate = gamestates.ready;
}
}
} else if (animationstate == 2) {
// Swapping tiles animation
if (animationtime > animationtimetotal) {
// Swap the tiles
swap(currentmove.column1, currentmove.row1, currentmove.column2, currentmove.row2);
// Check if the swap made a cluster
findClusters();
if (clusters.length > 0) {
// Valid swap, found one or more clusters
// Prepare animation states
animationstate = 0;
animationtime = 0;
gamestate = gamestates.resolve;
} else {
// Invalid swap, Rewind swapping animation
animationstate = 3;
animationtime = 0;
}
// Update moves and clusters
findMoves();
findClusters();
}
} else if (animationstate == 3) {
// Rewind swapping animation
if (animationtime > animationtimetotal) {
// Invalid swap, swap back
swap(currentmove.column1, currentmove.row1, currentmove.column2, currentmove.row2);
// Animation complete
gamestate = gamestates.ready;
}
}
// Update moves and clusters
findMoves();
findClusters();
}
}
function updateFps(dt) {
if (fpstime > 0.25) {
// Calculate fps
fps = Math.round(framecount / fpstime);
// Reset time and framecount
fpstime = 0;
framecount = 0;
}
// Increase time and framecount
fpstime += dt;
framecount++;
}
// Draw text that is centered
function drawCenterText(text, x, y, width) {
var textdim = context.measureText(text);
context.fillText(text, x + (width-textdim.width)/2, y);
}
// Render the game
function render() {
// Draw the frame
drawFrame();
// Draw score
context.fillStyle = "#f00";
context.font = "20px Verdana ";
drawCenterText("Score:", 30, level.y+40, 150);
drawCenterText(score, 60, level.y+90, 90);
// Draw buttons
drawButtons();
// Draw level background
var levelwidth = level.columns * level.tilewidth;
var levelheight = level.rows * level.tileheight;
context.fillStyle = "#000000";
context.fillRect(level.x - 4, level.y - 4, levelwidth + 8, levelheight + 8);
// Render tiles
renderTiles();
// Render clusters
renderClusters();
// Render moves, when there are no clusters
if (showmoves && clusters.length <= 0 && gamestate == gamestates.ready) {
renderMoves();
}
// Game Over overlay
if (gameover) {
context.fillStyle = "rgba(0, 0, 0, 0.8)";
context.fillRect(level.x, level.y, levelwidth, levelheight);
context.fillStyle = "#ffffff";
context.font = "24px Verdana";
drawCenterText("Game Over!", level.x, level.y + levelheight / 2 + 10, levelwidth);
}
}
// Draw a frame with a border
function drawFrame() {
// Draw background and a border
context.fillStyle = "#e8eaec";
context.fillRect(1, 1, canvas.width-2, canvas.height-2);
// Draw title
context.fillStyle = "#000";
context.font = "24px Verdana";
context.fillText("Candy game", canvas.width/2, 70);
// // Display fps
// context.fillStyle = "#ffffff";
// context.font = "12px Verdana";
// context.fillText("Fps: " + fps, canvas.width/2, 60);
}
// Draw buttons
function drawButtons() {
for (var i=0; i<buttons.length; i++) {
// Draw button shape
context.fillStyle = "#222";
context.fillRect(buttons[i].x, buttons[i].y, buttons[i].width, buttons[i].height);
// Draw button text
context.fillStyle = "#ffffff";
context.font = "18px Verdana";
var textdim = context.measureText(buttons[i].text);
context.fillText(buttons[i].text, buttons[i].x + (buttons[i].width-textdim.width)/2, buttons[i].y+30);
}
}
var colores = new Image();
var colores2 = new Image();
var colorfull = [];
// Render tiles
function renderTiles() {
for (var i=0; i<level.columns; i++) {
for (var j=0; j<level.rows; j++) {
// Get the shift of the tile for animation
var shift = level.tiles[i][j].shift;
// Calculate the tile coordinates
var coord = getTileCoordinate(i, j, 0, (animationtime / animationtimetotal) * shift);
// Check if there is a tile present
if (level.tiles[i][j].type >= 0) {
// Get the color of the tile
var col = tilecolors[level.tiles[i][j].type];
drawTile(coord.tilex, coord.tiley, col[0], col[1], col[2]);
// Draw the tile using the color
for (var k=1; k<=7; k++) {
// Draw the tile
colores2.src = "img/apple" + k + ".png";
colorfull.push(colores2);
if (tilecolors[level.tiles[i][j].type] == tilecolors[k-1]) {
context.drawImage(colores2, coord.tilex, coord.tiley , level.tilewidth, level.tileheight);
}
}
}
// Draw the selected tile
const food = new Image();
food.src = "img/apple5.png";
if (level.selectedtile.selected) {
if (level.selectedtile.column == i && level.selectedtile.row == j) {
// Draw a red tile
drawTile(coord.tilex, coord.tiley, 255, 0, 0)
}
}
}
}
let imageArray = new Array();
for (let k = 1; k <= 8; k++) {
imageArray[k] = new Image();
imageArray[k].src = "img/apple" + k + ".png";
}
// Render the swap animation
if (gamestate == gamestates.resolve && (animationstate == 2 || animationstate == 3)) {
// Calculate the x and y shift
var shiftx = currentmove.column2 - currentmove.column1;
var shifty = currentmove.row2 - currentmove.row1;
}
}
// Get the tile coordinate
function getTileCoordinate(column, row, columnoffset, rowoffset) {
var tilex = level.x + (column + columnoffset) * level.tilewidth;
var tiley = level.y + (row + rowoffset) * level.tileheight;
return { tilex: tilex, tiley: tiley};
}
// Draw a tile with a color
function drawTile(x, y) {
context.fillStyle = "#83535341", context.fillRect(x, y, level.tilewidth, level.tileheight);
context.fillRect(x + 2, y + 2, level.tilewidth - 4, level.tileheight - 4);
context.drawImage(colores, x + 2, y + 2, level.tilewidth - 4, level.tileheight - 4);
}
// Render clusters
function renderClusters() {
for (var i=0; i<clusters.length; i++) {
// Calculate the tile coordinates
var coord = getTileCoordinate(clusters[i].column, clusters[i].row, 0, 0);
}
}
// Render moves
function renderMoves() {
for (var i=0; i<moves.length; i++) {
// Calculate coordinates of tile 1 and 2
var coord1 = getTileCoordinate(moves[i].column1, moves[i].row1, 0, 0);
var coord2 = getTileCoordinate(moves[i].column2, moves[i].row2, 0, 0);
// Draw a line from tile 1 to tile 2
context.strokeStyle = "#000";
context.lineWidth = 2.5;
context.beginPath();
context.fillStyle = context.arc(coord1.tilex + level.tilewidth/2, coord1.tiley + level.tileheight/2, level.tilewidth/4, 0, 2 * Math.PI);
context.moveTo(coord1.tilex + level.tilewidth/2, coord1.tiley + level.tileheight/2);
context.lineTo(coord2.tilex + level.tilewidth/2, coord2.tiley + level.tileheight/2);
context.stroke();
}
}
// Start a new game
function newGame() {
// Reset score
score = 0;
// Set the gamestate to ready
gamestate = gamestates.ready;
// Reset game over
gameover = false;
// Create the level
createLevel();
// Find initial clusters and moves
findMoves();
findClusters();
}
// Create a random level
function createLevel() {
var done = false;
// Keep generating levels until it is correct
while (!done) {
// Create a level with random tiles
for (var i=0; i<level.columns; i++) {
for (var j=0; j<level.rows; j++) {
level.tiles[i][j].type = getRandomTile();
}
}
// Resolve the clusters
resolveClusters();
// Check if there are valid moves
findMoves();
// Done when there is a valid move
if (moves.length > 0) {
done = true;
}
}
}
// Get a random tile
function getRandomTile() {
return Math.floor(Math.random() * tilecolors.length);
}
// Remove clusters and insert tiles
function resolveClusters() {
// Check for clusters
findClusters();
// While there are clusters left
while (clusters.length > 0) {
// Remove clusters
removeClusters();
// Shift tiles
shiftTiles();
// Check if there are clusters left
findClusters();
}
}
// Find clusters in the level
function findClusters() {
// Reset clusters
clusters = []
// Find horizontal clusters
for (var j=0; j<level.rows; j++) {
// Start with a single tile, cluster of 1
var matchlength = 1;
for (var i=0; i<level.columns; i++) {
var checkcluster = false;
if (i == level.columns-1) {
// Last tile
checkcluster = true;
} else {
// Check the type of the next tile
if (level.tiles[i][j].type == level.tiles[i+1][j].type &&
level.tiles[i][j].type != -1) {
// Same type as the previous tile, increase matchlength
matchlength += 1;
} else {
// Different type
checkcluster = true;
}
}
// Check if there was a cluster
if (checkcluster) {
if (matchlength >= 3) {
// Found a horizontal cluster
clusters.push({ column: i+1-matchlength, row:j,
length: matchlength, horizontal: true });
}
matchlength = 1;
}
}
}
// Find vertical clusters
for (var i=0; i<level.columns; i++) {
// Start with a single tile, cluster of 1
var matchlength = 1;
for (var j=0; j<level.rows; j++) {
var checkcluster = false;
if (j == level.rows-1) {
// Last tile
checkcluster = true;
} else {
// Check the type of the next tile
if (level.tiles[i][j].type == level.tiles[i][j+1].type &&
level.tiles[i][j].type != -1) {
// Same type as the previous tile, increase matchlength
matchlength += 1;
} else {
// Different type
checkcluster = true;
}
}
// Check if there was a cluster
if (checkcluster) {
if (matchlength >= 3) {
// Found a vertical cluster
clusters.push({ column: i, row:j+1-matchlength,
length: matchlength, horizontal: false });
}
matchlength = 1;
}
}
}
}
// Find available moves
function findMoves() {
// Reset moves
moves = []
// Check horizontal swaps
for (var j=0; j<level.rows; j++) {
for (var i=0; i<level.columns-1; i++) {
// Swap, find clusters and swap back
swap(i, j, i+1, j);
findClusters();
swap(i, j, i+1, j);
// Check if the swap made a cluster
if (clusters.length > 0) {
// Found a move
moves.push({column1: i, row1: j, column2: i+1, row2: j});
}
}
}
// Check vertical swaps
for (var i=0; i<level.columns; i++) {
for (var j=0; j<level.rows-1; j++) {
// Swap, find clusters and swap back
swap(i, j, i, j+1);
findClusters();
swap(i, j, i, j+1);
// Check if the swap made a cluster
if (clusters.length > 0) {
// Found a move
moves.push({column1: i, row1: j, column2: i, row2: j+1});
}
}
}
// Reset clusters
clusters = []
}
// Loop over the cluster tiles and execute a function
function loopClusters(func) {
for (var i=0; i<clusters.length; i++) {
// { column, row, length, horizontal }
var cluster = clusters[i];
var coffset = 0;
var roffset = 0;
for (var j=0; j<cluster.length; j++) {
func(i, cluster.column+coffset, cluster.row+roffset, cluster);
if (cluster.horizontal) {
coffset++;
} else {
roffset++;
}
}
}
}
// Remove the clusters
function removeClusters() {
// Change the type of the tiles to -1, indicating a removed tile
loopClusters(function(index, column, row, cluster) { level.tiles[column][row].type = -1; });
// Calculate how much a tile should be shifted downwards
for (var i=0; i<level.columns; i++) {
var shift = 0;
for (var j=level.rows-1; j>=0; j--) {
// Loop from bottom to top
if (level.tiles[i][j].type == -1) {
// Tile is removed, increase shift
shift++;
level.tiles[i][j].shift = 0;
} else {
// Set the shift
level.tiles[i][j].shift = shift;
}
}
}
}
// Shift tiles and insert new tiles
function shiftTiles() {
// Shift tiles
for (var i=0; i<level.columns; i++) {
for (var j=level.rows-1; j>=0; j--) {
// Loop from bottom to top
if (level.tiles[i][j].type == -1) {
// Insert new random tile
level.tiles[i][j].type = getRandomTile();
} else {
// Swap tile to shift it
var shift = level.tiles[i][j].shift;
if (shift > 0) {
swap(i, j, i, j+shift)
}
}
// Reset shift
level.tiles[i][j].shift = 0;
}
}
}
// Get the tile under the mouse
function getMouseTile(pos) {
// Calculate the index of the tile
var tx = Math.floor((pos.x - level.x) / level.tilewidth);
var ty = Math.floor((pos.y - level.y) / level.tileheight);
// Check if the tile is valid
if (tx >= 0 && tx < level.columns && ty >= 0 && ty < level.rows) {
// Tile is valid
return {
valid: true,
x: tx,
y: ty
};
}
// No valid tile
return {
valid: false,
x: 0,
y: 0
};
}
// Check if two tiles can be swapped
function canSwap(x1, y1, x2, y2) {
// Check if the tile is a direct neighbor of the selected tile
if ((Math.abs(x1 - x2) == 1 && y1 == y2) ||
(Math.abs(y1 - y2) == 1 && x1 == x2)) {
return true;
}
return false;
}
// Swap two tiles in the level
function swap(x1, y1, x2, y2) {
var typeswap = level.tiles[x1][y1].type;
level.tiles[x1][y1].type = level.tiles[x2][y2].type;
level.tiles[x2][y2].type = typeswap;
}
// Swap two tiles as a player action
function mouseSwap(c1, r1, c2, r2) {
// Save the current move
currentmove = {column1: c1, row1: r1, column2: c2, row2: r2};
// Deselect
level.selectedtile.selected = false;
// Start animation
animationstate = 2;
animationtime = 0;
gamestate = gamestates.resolve;
}
// On mouse movement
function onMouseMove(e) {
// Get the mouse position
var pos = getMousePos(canvas, e);
// Check if we are dragging with a tile selected
if (drag && level.selectedtile.selected) {
// Get the tile under the mouse
mt = getMouseTile(pos);
if (mt.valid) {
// Valid tile
// Check if the tiles can be swapped
if (canSwap(mt.x, mt.y, level.selectedtile.column, level.selectedtile.row)){
// Swap the tiles
mouseSwap(mt.x, mt.y, level.selectedtile.column, level.selectedtile.row);
}
}
}
}
// On mouse button click
function onMouseDown(e) {
// Get the mouse position
var pos = getMousePos(canvas, e);
// Start dragging
if (!drag) {
// Get the tile under the mouse
mt = getMouseTile(pos);
if (mt.valid) {
// Valid tile
var swapped = false;
if (level.selectedtile.selected) {
if (mt.x == level.selectedtile.column && mt.y == level.selectedtile.row) {
// Same tile selected, deselect
level.selectedtile.selected = false;
drag = true;
return;
} else if (canSwap(mt.x, mt.y, level.selectedtile.column, level.selectedtile.row)){
// Tiles can be swapped, swap the tiles
mouseSwap(mt.x, mt.y, level.selectedtile.column, level.selectedtile.row);
swapped = true;
}
}
if (!swapped) {
// Set the new selected tile
level.selectedtile.column = mt.x;
level.selectedtile.row = mt.y;
level.selectedtile.selected = true;
}
} else {
// Invalid tile
level.selectedtile.selected = false;
}
// Start dragging
drag = true;
}
// Check if a button was clicked
for (var i=0; i<buttons.length; i++) {
if (pos.x >= buttons[i].x && pos.x < buttons[i].x+buttons[i].width &&
pos.y >= buttons[i].y && pos.y < buttons[i].y+buttons[i].height) {
// Button i was clicked
if (i == 0) {
// New Game
newGame();
} else if (i == 1) {
// Show Moves
showmoves = !showmoves;
buttons[i].text = (showmoves?"Hide":"Show") + " Moves";
} else if (i == 2) {
// AI Bot
aibot = !aibot;
buttons[i].text = (aibot?"Disable":"Enable") + " AI Bot";
}
}
}
}
function onMouseUp(e) {
// Reset dragging
drag = false;
}
function onMouseOut(e) {
// Reset dragging
drag = false;
}
// Get the mouse position
function getMousePos(canvas, e) {
var rect = canvas.getBoundingClientRect();
return {
x: Math.round((e.clientX - rect.left)/(rect.right - rect.left)*canvas.width),
y: Math.round((e.clientY - rect.top)/(rect.bottom - rect.top)*canvas.height)
};
}
// Call init to start the game
init();
};