-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obstacle.pde
65 lines (57 loc) · 1.18 KB
/
Obstacle.pde
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Obstacle {
private float x, y, dx, dy, w, h;
private float[] xs, ys;
private int frame;
private final int FRAMES = 30;
private final int MIN_W = 100, MAX_W = 125;
private final int MIN_H = 25, MAX_H = 50;
Obstacle(float x, float y, float dx, float dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.w = random(MIN_W, MAX_W);
this.h = random(MIN_H, MAX_H);
this.xs = new float[FRAMES];
this.ys = new float[FRAMES];
for (int i = 0; i < FRAMES; i++) {
xs[i] = x;
ys[i] = y;
}
}
float getX() {
return x;
}
float getY() {
return y;
}
float getWidth() {
return w;
}
float getHeight() {
return h;
}
boolean isGone() {
return y > height + 25;
}
void display() {
frame = frameCount % FRAMES;
xs[frame] = x;
ys[frame] = y;
for (int i = 0; i < FRAMES; i++) {
color newC = color(225, i / 2);
fill(newC);
int index = (frame + 1 + i) % FRAMES;
rect(xs[index], ys[index], w, h);
}
fill(225);
rect(x, y, w, h);
}
void move() {
if (x <= 0 || x + w >= width) {
dx *= -1;
}
x += dx;
y += dy;
}
}