-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
49 lines (42 loc) · 1.12 KB
/
game.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
46
47
48
49
import { Birb } from './birb.js';
import InputHandler from './input.js';
import Pipe from './pipe.js';
import { GameState } from './util.js';
export class Game {
constructor(canvas, ctx) {
this.canvas = canvas;
this.ctx = ctx;
this.birb = new Birb(this);
this.state = GameState.TITLE;
this.input = new InputHandler();
this.pipes = [];
}
init() {
this.canvas.style.backgroundColor = 'rgb(61, 201, 244)';
document.body.removeChild(startbtn);
this.draw();
// wait 3 seconds
return new Promise((resolve) => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
update() {
this.birb.update(this.input.keys);
// score
// Pipes
if (this.pipes.length === 0) Pipe.generatePipe(this);
for (let i = 0; i < this.pipes.length; i++) {
if (this.pipes[i].x < 0) this.pipes.splice(this.pipes[i], 1);
//else if (this.pipes[i].x < this.canvas.width / 2) Pipe.generatePipe(this);
else this.pipes[i].update();
}
}
draw() {
for (let i = 0; i < this.pipes.length; i++) {
this.pipes[i].draw();
}
this.birb.draw();
}
}