-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFish.java
47 lines (44 loc) · 1.15 KB
/
Fish.java
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
/*
* Laura Chevalier
* Intro to Computer Science with Professor Versoza
*/
import processing.core.PApplet;
public class Fish extends FishTank {
//draws fish
float vx = r.nextInt(2) + 1;
public Fish(PApplet p) {
super(p);
this._p = p;
}
@Override
public void render() {
//the fish draws itself
_p.stroke(0);
_p.fill(red, green, blue);
//Fish body
_p.ellipse(this.x, this.y, size * 2, size);
//Fish tail
//changes sides based on direction of swimming
if (vx > 0) {
_p.triangle(this.x - 3 * size / 2, this.y + size / 2, this.x - size,
this.y, this.x - 3 * size / 2, this.y - size / 2);
} else if (vx < 0) {
_p.triangle(this.x + size * 3 / 2, this.y + size / 2, this.x + size,
this.y, this.x + size * 3 / 2, this.y - size / 2);
}
//fish eye (changes sides based on direction of swimming)
_p.fill(0);
if (vx < 0) {
_p.ellipse(this.x - size / 2, this.y - size / 5, 4, 4);
} else {
_p.ellipse(this.x + size / 2, this.y - size / 5, 4, 4);
}
}
public void move() {
//fish moves horizontally, changes direction when it reaches edge
this.x += vx;
if (this.x >= _p.width || this.x <= 0) {
vx *= -1;
}
}
}