// 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 } 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) { // create a bunny bunnies[i] = new Bunny(); // set random coordinates for bunny bunnies[i].x = random(50, width-50); bunnies[i].y = random(50, height-50); // set random speed for bunnies bunnies[i].dx = random(0.5, 2.5); bunnies[i].dy = 0.0; // set random direction bunnies[i].isLeft = (random(1) < 0.5); } } // 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; } } } }