// Anthony E. Capiral © 2012 // CST Intro to Programming // Project 6 - Objects, Constructors, and Methods // "Dancing Dogs" // Rework of Project 1 and 2, "Mandogbird", using classes/objects. // Midterm takehome, "Day Life vs. Night Life", also included the use of objects. // init infoText strings String title = "CST 112 Project 6"; String projectName = "Dancing Dogs"; String author = "Anthony E. Capiral"; String date = "© 12.17.12"; // set program size int stageWidth = 700; int stageHeight = 500; // set dog (creature) boundaries, kennel int kennelTop = stageHeight/2; int kennelBot = stageHeight-25; int kennelLeft = stageWidth - (stageWidth-25); int kennelRight = stageWidth-25; // determine the center of the kennel int kennelCenterX = stageWidth/2; int kennelCenterY = stageHeight - (stageHeight/4); // init dog color color brownColor = color(130, 65, 0); color tanColor = color(130, 85, 0); color beigeColor = color(130, 45, 0); color whiteColor = color(225); // off-white color greyColor = color(125); boolean bump = false; // set up array of dogs, Kennel int amountOfDogs = 5; // total number of dog objects // create a number of dogs in an array called kennel Dog kennel[] = new Dog[amountOfDogs]; // initialize! void setup() { smooth(); size(stageWidth, stageHeight); setupDogs(); } // create dog constructors void setupDogs() { // initial dog velocities int dogDX = 4; int dogDY = 3; // create the dogs, located near the center of the stage(kennel boundaries), // moving randomly with velocities in X and Y directions, with a string (name) kennel[0] = new Dog(brownColor, kennelCenterX, kennelCenterY, dogDX, dogDY, " Spot "); kennel[1] = new Dog(tanColor, kennelCenterX - 50, kennelCenterY - 50, dogDX, dogDY, " Spike "); kennel[2] = new Dog(beigeColor, kennelCenterX + 50, kennelCenterY - 50, dogDX, dogDY, "Scruffy"); kennel[3] = new Dog(whiteColor, kennelCenterX - 25, kennelCenterY + 25, dogDX, dogDY, "Snoopy"); kennel[4] = new Dog(greyColor, kennelCenterX + 25, kennelCenterY - 25, dogDX, dogDY, " Fido "); } // end setupDogs() void draw() { createBg(); infoText(); // display text about project dogs(); } // create scene background void createBg() { noStroke(); // kennel rectMode(CORNERS); fill(50); // grey rect(0, 0, width, height); // sky fill(75, 75, 255); // light blue rect(0, 0, width, height/2); } // end createBg() // descriptive text void infoText() { fill(255); text(title, width-400, height-485); fill(0, 150, 255); text(projectName, width-390, height-475); fill(255); text(author, width-675, height-50); text(date, width-665, height-40); } // end infoText() // main method to call drawing, moving and bumping of dogs void dogs() { drawDogs(); moveDogs(); bumpDogs(); } // draw(create) the dog objects void drawDogs() { for (int i = 0; i < amountOfDogs; i++) { kennel[i].create(); } } // move the dog objects, make them "run" around the kennel void moveDogs() { for (int i = 0; i < amountOfDogs; i++) { kennel[i].run(); } } // traverse the kennel array void bumpDogs() { for (int i = 0; i < amountOfDogs; i++) { for (int j = i+1; j < amountOfDogs; j++ ) { // exchange velocities if they hit // see bump() method below kennel[i].bump(kennel[j]); } } } // check to see it dogs hit each other // original code without using "this" void bumpDogsOld() { float dogTempDX, dogTempDY; // for (int i = 0; i < amountOfDogs; i++) { for (int j = i+1; j < amountOfDogs; j++) { if ( (dist(kennel[i].dogPosX, kennel[i].dogPosY, kennel[j].dogPosX, kennel[j].dogPosY) < 30) ) { //text("Two dogs has collided!", kennelCenterX-60, kennelCenterY-200); // swap X velocities dogTempDX = kennel[i].dogDX; kennel[i].dogDX = kennel[j].dogDX; kennel[j].dogDX = dogTempDX; // swap Y velocites dogTempDY = kennel[i].dogDY; kennel[i].dogDY = kennel[j].dogDY; kennel[j].dogDY = dogTempDY; } } } } // end bumpDogsOld() // OBJECTS! // define the Dog class public class Dog { // local dog variables color dogColor; float dogPosX, dogPosY; float dogDX, dogDY; String dogName; // Dog constructor Dog(color c, float dPX, float dPY, float dDX, float dDY, String name) { dogColor = c; // color of the dog dogPosX = dPX; // dog starting X position in kennel dogPosY = dPY; // dog starting Y pos // randomize dog velocities dogDX = random(-dDX, dDX); // moving left or right dogDY = random(-dDY, dDY); // up or down dogName = name; // name of the dog } // method to create(draw) and name the dog void create() { //// drawing ELLIPSES (circles) to simplify dogs \\\\ noStroke(); ellipseMode(CENTER); fill(dogColor); // shades of brown ellipse(dogPosX, dogPosY, 30, 30); fill(255); // show dogs name in white above the circle text(dogName, dogPosX-18, dogPosY-30); } // move the dog, make it run void run() { // contstrain a dog run in the kennel (left and right boundaries) if (dogPosX <= kennelLeft) { dogDX = abs(dogDX); } if (dogPosX >= kennelRight) { dogDX = -dogDX; } dogPosX = dogPosX + dogDX; // prevent dog from running too far up or down if ( (dogPosY <= kennelTop) || (dogPosY >= kennelBot) ) { dogDY = -dogDY; } dogPosY = dogPosY + dogDY; } // end run() void bump(Dog otherDog) { // method takes in a Dog object!! // test to see if "THIS" dog hits OTHER dog // swap velocities if so // temporary velocities float dogTempDX, dogTempDY; if ( (dist(this.dogPosX, this.dogPosY, otherDog.dogPosX, otherDog.dogPosY) < 30) ) { //text("Two dogs have collided!", kennelCenterX-60, kennelCenterY-200); // swap X velocities dogTempDX = this.dogDX; this.dogDX = otherDog.dogDX; otherDog.dogDX = dogTempDX; // swap Y velocities dogTempDY = this.dogDY; this.dogDY = otherDog.dogDY; otherDog.dogDY = dogTempDY; // create circle where collision occurred // using colors of the two dogs dogColorSwap(this.dogColor, this.dogPosX, this.dogPosY, otherDog.dogColor, otherDog.dogPosX, otherDog.dogPosY); } } } // end Dog class definition void dogColorSwap(color dColorOne, float dPosX, float dPosY, color dColorTwo, float oPosX, float oPosY) { // create circle in the middle where collision occurred // using colors of the two dogs involved // outline color of the first dog, inside color of the second strokeWeight(5); stroke(dColorOne); fill(dColorTwo); ellipse( (dPosX + oPosX)/2, (dPosY + oPosY)/2 , 60, 60 ); } // EOF