-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
45 lines (41 loc) · 1.23 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
function _main(canvasDom, width, height) {
let canvas = document.querySelector(canvasDom)
let context = canvas.getContext('2d')
let images = {
house: 'images/house.png',
block: 'images/block.png',
bull: 'images/bull.png',
currentblock: 'images/currentblock.png',
down: 'images/down.png',
left: 'images/left.png',
right: 'images/right.png',
up: 'images/up.png',
wall: 'images/wall.png',
boxedBull: 'images/boxedBull.png',
trap: 'images/trap.png',
dead: 'images/dead.png',
}
let game = new Game(canvas)
initImages(images, game)
}
function initImages(images, game) {
//pre-load all of the image
let loads = []
let imgNames = Object.keys(images)
for (let i = 0; i < imgNames.length; i++){
let name = imgNames[i]
let path = images[name]
let img = new Image()
img.src = path
img.onload = function (){
images[name] = img
loads.push(1)
if (loads.length == imgNames.length){
//Images loaded sucessfully
game.images = images
//Game intialize
game.init()
}
}
}
}