forked from AmrSaber/ComputerGraphics-OpenGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFly.pde
48 lines (36 loc) · 1.05 KB
/
Fly.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
class Fly{
private float x, y;
private final float yMin, yMax = height - 25;
private float dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
private float dy[] = {1, -1, 0, 1, -1, 0, 1, -1};
private int direction = (int) random(dx.length);
private final int counterLimit = 75;
private int counter = (int) random(counterLimit);
private float speed = random(0.5, 1);
private float size = 2;
public Fly(float _yMin){
this.yMin = _yMin;
this.x = random(width);
this.y = random(yMin, yMax);
}
public void display(){
pushMatrix();
translate(x, y, 6);
noStroke();
fill(color(255, 215, 0));
ellipse(0, 0, size, size);
popMatrix();
update();
}
private void update(){
++counter;
if (counter == counterLimit || (x <= 0 || x >= width) || (y <= yMin || y >= yMax)) {
counter = 0;
direction = (int) random(dx.length);
}
this.x += dx[direction] * speed;
this.y += dy[direction] * speed;
x = constrain(x, 0, width);
y = constrain(y, yMin, yMax);
}
}