Duck [] ducks;
int cwidth = 800;
int cheight = 600;
class Duck {
int x;
int y;
int width;
int height;
color feather_color;
int direction;
int pause;
Duck(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
if (random() < .33) {
this.feather_color = color(255, 255, 0);
} else if (random() < .66) {
this.feather_color = color(120, 0, 0);
} else {
this.feather_color = color(128, 128, 128);
}
if (random() < .5) {
this.direction = 0; // left
} else {
this.direction = 1; // right
}
this.pause = 0;
// 2 left turning right
// 3 right turning left
}
void draw() {
// duck head
fill(this.feather_color);
ellipse(this.x, this.y, this.width, this.width);
// body
rect(this.x - this.width/2, this.y + this.width/2, this.width, this.height - this.width/2);
// duck bill
fill(255, 128, 0); // orange
if (this.direction == 0) { // left
rect(this.x - this.width, this.y - 5, this.width/2, 5);
fill(255);
ellipse(this.x - this.width/3, this.y - this.width/3, 10, 10);
} else if (this.direction == 1) { // right
rect(this.x + this.width/2, this.y - 5, this.width/2, 5);
fill(255);
ellipse(this.x + this.width/3, this.y - this.width/3, 10, 10);
} else {
rect(this.x - 10, this.y - 5, this.width/4, 5);
fill(255);
ellipse(this.x + this.width/3, this.y - this.width/3, 10, 10);
ellipse(this.x - this.width/3, this.y - this.width/3, 10, 10);
}
}
void toggle() {
if (this.direction == 0) {
this.direction = 2;
this.pause = 0;
} else if (this.direction == 1) {
this.direction = 3;
this.pause = 0;
}
}
void waddle() {
// don't move
if (this.x - this.width/2 < 0) {
this.toggle();
this.x = this.width/2;
}
if (this.x + this.width/2 > cwidth) {
this.toggle();
this.x = cwidth - this.width/2;
}
if (this.direction == 0) {
this.x = this.x - this.width/10;
} else if (this.direction == 1){
this.x = this.x + this.width/10;
} else if (this.direction == 2 && this.pause > 20) {
this.direction = 1;
} else if (this.direction == 3 && this.pause > 20) {
this.direction = 0;
}
this.pause = this.pause + 1;
}
}
void setup() {
size(cwidth, cheight);
ducks = new Duck[100];
for (int i = 0; i < ducks.length; i++) {
ducks[i] = new Duck(random(cwidth), random(cheight), random(10, 70), random(10, 70));
}
}
void draw() {
background();
for (int i = 0; i < ducks.length; i++) {
ducks[i].waddle();
ducks[i].draw();
}
}