-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipe.js
49 lines (39 loc) · 1.11 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() {
// Spacing between the top and bottom pipes
const pipeGap = 75;
// Center of the pipe
const centerOfPipe = random(height - pipeDownImg.height - pipeGap / 2, pipeTopImg.height + pipeGap / 2);
// Top pipe's bottom y coordinate
this.top = centerOfPipe - (pipeGap / 2);
// Bottom pipe's top y coordinate
this.bottom = centerOfPipe + (pipeGap / 2);
// x position of left edge of the pipe
this.x = width;
// Width of the pipe
this.width = pipeDownImg.width;
// Speed of the pipe
this.speed = 6;
}
checkCollision(bird) {
if (bird.x + bird.width / 2 >= this.x && bird.x - bird.width / 2 <= this.x + this.width) {
if (bird.y - bird.height / 2 <= this.top || bird.y + bird.height / 2 >= this.bottom) {
return true;
}
}
return false;
}
show() {
image(pipeTopImg, this.x, -Math.abs(pipeTopImg.height - this.top), this.width, pipeTopImg.height);
image(pipeDownImg, this.x, this.bottom, this.width, pipeDownImg.height);
}
update() {
this.x -= this.speed;
}
checkOffScreen() {
if (this.x < -this.width) {
return true;
}
return false;
}
}