// variables & new types class Bunny { float x, y; // coordinate of bunny float dx, dy; // x and y speed of bunny boolean isLeft; // orientation of bunny int count; // how many times the bunny // banged its head on a wall // built-in function to turn around // here is a "method" of the Bunny class void turnAround(){ if( isLeft ){ isLeft = false; }else{ isLeft = true; } count += 1; dx *= random(0.8, 1.75); if (dx > 25) dx = 25; } // constructor Bunny(){ x = random(50, width-50); y = random(50, height-50); dx = random(0.5, 2.5); dy = 0.0; isLeft = (random(1) < 0.5); count = 0; } } int num; // number of bunnies Bunny [] bunnies; // array to hold all num bunnies PImage leftB, rightB; // initialization void setup() { size(600, 400); rightB = loadImage("right.png"); leftB = loadImage("left.png"); // Create the array that will hold all the bunnies. // This just makes the array it does not make any // actual bunnies (instances of Bunny type) bunnies = new Bunny[20]; // populate the array with actual bunnies for (int i=0; i<20; i+=1) { // creating a random bunny is now easy // with the constructor we defined bunnies[i] = new Bunny(); } } // main program void draw() { background(127); for (int i=0; i<20; i+=1) { // draw the bunny if (bunnies[i].isLeft) image( leftB, bunnies[i].x, bunnies[i].y); else image( rightB, bunnies[i].x, bunnies[i].y); // update bunny location if (bunnies[i].isLeft) { if (bunnies[i].x < 50) { bunnies[i].isLeft = false; // turn around bunnies[i].count += 1; bunnies[i].dx *= random(0.8, 1.75); if (bunnies[i].dx > 25) bunnies[i].dx = 25; bunnies[i].x += bunnies[i].dx; } else { bunnies[i].x -= bunnies[i].dx; } } else { if (bunnies[i].x > width-50) { bunnies[i].isLeft = true; // turn around bunnies[i].count += 1; bunnies[i].dx *= random(0.8, 1.75); if (bunnies[i].dx > 25) bunnies[i].dx = 25; bunnies[i].x -= bunnies[i].dx; } else { bunnies[i].x += bunnies[i].dx; } } } }