// 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; } void takeStep() { if (isLeft) x = x - dx; else x = x + dx; } void drawBunny(){ if (isLeft) image( leftB, x, y); else image( rightB, x, y); } // 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 bunny bunnies[i].drawBunny(); // check if bunny bangs into the wall if (bunnies[i].x < 50 || bunnies[i].x > width-50) { bunnies[i].turnAround(); } // update bunny bunnies[i].takeStep(); } }