-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame2.js
More file actions
70 lines (67 loc) · 1.58 KB
/
game2.js
File metadata and controls
70 lines (67 loc) · 1.58 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
let game = {
ctx:null,
block:[],
rows:4,
cols:8,
sprites:{
background:null,
ball:null,
block:null,
platform:null,
},
init:function()
{
this.ctx = document.getElementById("my-game").getContext("2d")
},
create:function()
{
for(let row = 0; row<this.rows; row++)
{
for(let col =0; col<this.cols; col++ )
{
this.blocks.push({})
}
}
},
preload:function(callback)
{
let loaded = 0;
let reqired = Object.keys(this.sprites).length
let onloadImage = ()=>{
++loaded
if(loaded>=reqired)
{
callback()
}
}
for(let key in this.sprites)
{
this.sprites[key] = new Image()
this.sprites[key].src = 'img/'+key +'.png'
this.sprites[key].addEventListener("load",onloadImage)
}
},
run:function()
{
window.requestAnimationFrame(()=>{
this.render()
})
},
render:function()
{
this.ctx.drawImage(this.sprites.background,0,0)
this.ctx.drawImage(this.sprites.ball,0,0)
this.ctx.drawImage(this.sprites.block,0,0)
this.ctx.drawImage(this.sprites.platform,0,0)
},
start:function()
{
this.init()
this.preload(()=>{
this.run()
})
}
}
window.addEventListener("load",()=>{
game.start()
})