-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpipe.js
49 lines (43 loc) · 1.03 KB
/
pipe.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
class Pipe {
constructor() {
// How big is the empty space
let spacing = 125;
// Where is th center of the empty space
let centery = random(spacing, height - spacing);
// Top and bottom of pipe
this.top = centery - spacing / 2;
this.bottom = height - (centery + spacing / 2);
// Starts at the edge
this.x = width;
// Width of pipe
this.w = 80;
// How fast
this.speed = 6;
}
// Did this pipe hit a bird?
hits(bird) {
if ((bird.y - bird.r) < this.top || (bird.y + bird.r) > (height - this.bottom)) {
if (bird.x > this.x && bird.x < this.x + this.w) {
return true;
}
}
return false;
}
// Draw the pipe
show() {
image(pipeBodySprite, this.x, 0, this.w, this.top);
image(pipePeakSprite, this.x, height - this.bottom, this.w, this.bottom);
}
// Update the pipe
update() {
this.x -= this.speed;
}
// Has it moved offscreen?
offscreen() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
}