class Game {
constructor(ctx, sprites) {
this.ctx = ctx;
this.sprites = sprites
}
init() {
this.ctx = document.getElementById('canvas').getContext("2d");
};
preload() {
this.sprites = {
ball: null,
background: null,
platform: null
};
let loaded = 0;
let required = Object.keys(this.sprites).length;
for (let key in this.sprites) {
this.sprites[key] = new Image();
this.sprites[key].src =
img/${key}.png
;
this.sprites[key].addEventListener('load', () => {
loaded++;
console.log(loaded === required);
if(loaded >= required) {
this.run();
}
})
}
}
render() {
this.ctx.drawImage(this.sprites.background, 0, 0)
this.ctx.drawImage(this.sprites.ball, 0, 0)
this.ctx.drawImage(this.sprites.platform, 0, 0)
}
run() {
this.loop();
}
loop() {
this.render();
requestAnimationFrame(loop);
}
start() {
this.init();
this.preload();
}
};