-
Notifications
You must be signed in to change notification settings - Fork 0
/
obstacles.js
39 lines (35 loc) · 978 Bytes
/
obstacles.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
const obstacles = [];
class Obstacle {
constructor() {
this.top = (Math.random() * canvas.height / 3) + 20;
this.bottom = (Math.random() * canvas.height / 3) + 20;
this.x = canvas.width;
this.width = 20;
this.color = 'hsla(' + hue + ',100%,50%,1)';
this.counted = false;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, 0, this.width, this.top)
ctx.fillRect(this.x, canvas.height - this.bottom, this.width, this.bottom);
}
update() {
this.x -= gameSpeed;
if (!this.counted && this.x < bird.x) {
score++;
this.counted = true;
}
this.draw();
}
}
function handleObstacles() {
if (frame % 50 === 0) {
obstacles.unshift(new Obstacle);
}
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].update();
}
if (obstacles.length > 20) {
obstacles.pop(obstacles[0]);
}
}