class Fishery {
float x;
float y;
int[] eyes = { 0, 255 };
int index = int(random(eyes.length));
float yoffset = 0.0;
float angle = random(0, TWO_PI);
float colour = random(80, 255);
float fishScale = random(1, 1.02);
float speed = random(1, 5);
Fishery(float tempX, float tempY) {
x = tempX;
y = tempY;
}
void move() {
if (x > -100) {
x -= speed;
} else {
x = width + 100;
}
angle += 0.15;
yoffset = sin(angle) * 0.5;
y += yoffset;
}
void display() {
scale(fishScale);
fill(colour);
ellipse(x, y, 110, 45);
triangle(x+31, y-19, x+80, y, x+31, y+19);
beginShape();
vertex(x+68, y);
vertex(x+100, y-23);
vertex(x+90, y);
vertex(x+100, y+23);
endShape(CLOSE);
if (colour > 180) {
fill(0);
} else {
fill(colour+100);
}
ellipse(x-35, y+5, 5, 5);
}
}
Fishery[] fish = new Fishery[100]; // Declare objects
void setup() {
size(800, 400);
noStroke();
smooth();
ellipseMode(CENTER);
// Create object and pass in parameters
for (int i = 0; i < fish.length; i++) {
float x = random(width);
float y = random(height);
fish[i] = new Fishery(x, y);
}
}
void draw() {
background(0);
for (int i = 0; i < fish.length; i++) {
fish[i].move();
fish[i].display();
}
}
Response to the final project in the 'Introduction to Processing' text. Demonstrates the use of classes, functions and iteration.