/************************************************************ /* Author: Dr. Parson /* Due Date: 4/6/2018 /* Course: CSC120 Intro. to Creative Graphical Coding /* Professor Name: Dr. Parson /* Assignment: 3. /* Sketch name: avatarClass2018 /* Purpose: Go from creating & using a function to /* creating & using a class. *********************************************************/ // Modeled after photo http://www.gradprofiles.com/images/kutzlib.gif // These variables can hold integers, i.e., whole numbers with no fractional parts. int backgroundColor = 0 ; // Bounces from 1 to 254 to 1 using backgroundColorSpeed. // backgroundColor flickers if I let it go < 0 or > 255, hence I am using range 1..254. int backgroundColorSpeed = 1 ; // Starts in a positve direction. // These new global variables and array will house Avatar objects. Avatar avatar1, avatar2, avatar3 ; Avatar [] crowd = new Avatar[2]; // array that can hold 2 Avatars void setup() { size(750, 500); background(backgroundColor); // THE FOLLOWING LINES CONSTRUCT AVATAR OBJECTS // AND STORE THEM IN THEIR VARIABLES AND ARRAY. // Build a mobile avatar at the original location // with a speed and a leg speed of 1. avatar1 = new Avatar(0, height/2, 1.0, 0, 1, 1); // Build a stationary avatar at assn2 location with high leg speed. avatar2 = new Avatar(width/4, height/8, 0.5, 0, 0, 4); // Build a stationary avatar at assn2 location with high leg speed. avatar3 = new Avatar(width/4+width/2, height/8, 0.5, 0, 0, 4); // To see what the parameters are, go look at the constructor definition // down inside class Avatar. crowd[0] = new Avatar(width/4, height/8, 0.5, 0, 0, 4); crowd[1] = new Avatar(width/4+width/2, height/8, 0.5, 0, 0, 4); // I am replacing avatar2 and avatar3 with crowd[0] and crowd[1]. } /* for (int clonex = width/4 ; clonex < width ; clonex += width/2) { // start clonex at width/4; adding width/2 comes back into the // loop only a second time. display(clonex, height/8, 0.5, 16*legX); } */ void draw() { // Make the background cyclic through light & dark, no jumps. backgroundColor = backgroundColor + backgroundColorSpeed ; if (backgroundColor < 1 || backgroundColor > 254) { // It went out of range backgroundColorSpeed = - backgroundColorSpeed ; // reverse direction } background(backgroundColor); stroke(0); // PAINT BACKGROUND SCENERY. rectMode(CENTER); // Semi-transparent green grass uses the background's light-to-dark. fill(0, 100, 0, 100); rect(width/2, height*3/4, width, height/2); // Semi-transparent blue sky uses the background's light-to-dark. fill(0, 0, 100, 100); rect(width/2, height/4, width, height/2); // Create the building (library). fill(108, 57, 15); rect(width/2, height/4, width/2, height/3); rect(width/2, height/2-35, width, height/3); fill(100); rect(width/2, height/2, width*2/3, height/5); // Create poles leading into library, make one foreground later. strokeWeight(4); stroke(200); ellipseMode(CENTER); fill(255); line(width/3, height/3, width/3, height*2/3); line(width*2/3, height/3, width*2/3, height*2/3); arc(width/3, height/3, 40, 40, PI, 2*PI, PIE); arc(width*2/3, height/3, 40, 40, PI, 2*PI, PIE); // Make the next poll be the same distance further. int newx = (width/3 - width/4) + width/3 ; // Do the same for its height -- shrink by same amount. int newy = (height/3 - height/4) + height/3 ; line(newx, newy, newx, height-newy); line(width-newx, newy, width-newx, height-newy); arc(newx, newy, 30, 30, PI, 2*PI, PIE); arc(width-newx, newy, 30, 30, PI, 2*PI, PIE); // DRAW & MOVE THE PRIMARY AVATAR. // THE OTHERS DISPLAY & MOVE AFTER THE LAMPPOSTS. avatar1.display(); avatar1.move(); // Create last pole leading into library. strokeWeight(6); stroke(200); ellipseMode(CENTER); fill(255); line(width/4, height/4, width/4, height*3/4); line(width*3/4, height/4, width*3/4, height*3/4); arc(width/4, height/4, 50, 50, PI, 2*PI, PIE); arc(width*3/4, height/4, 50, 50, PI, 2*PI, PIE); // fill(0, 100, 0) ; // dark green for some foreground trees fill(255 - abs(backgroundColor - 255), abs(backgroundColor - 255), 0); noStroke(); triangle(0, 0, 100, 180, 0, 180); triangle(0, 135, 100, 315, 0, 315); triangle(0, 270, 100, 450, 0, 450); fill(85, 73, 25); rectMode(CORNER); rect(0, 450, 20, height-450); // Put another tree on the right. // fill(0, 100, 0) ; // dark green for some foreground trees fill(255 - abs(backgroundColor - 255), abs(backgroundColor - 255), 0); noStroke(); triangle(width, 0, width-100, 180, width, 180); triangle(width, 135, width-100, 315, width, 315); triangle(width, 270, width-100, 450, width, 450); fill(85, 73, 25); rectMode(CORNER); rect(width-20, 450, width, height-450); // The little avatars display after the lampposts. /* REPLACE WITH THE ARRAY crowd. avatar2.display(); avatar3.display(); avatar2.move(); avatar3.move(); */ // DISPLAY ALL AVATAR OBJECTS IN ARRAY crowd. for (int i = 0 ; i < crowd.length ; i++) { crowd[i].display(); crowd[i].move(); } /* DEMO A LOOP THAT DOES NOT INVOLVE THE AVATAR: fill(0,75,0); // dark green bushes noStroke(); int bushx = 50 ; while (bushx < width-50) { ellipse(bushx, height - 30, 30, 20); bushx += 50 ; } */ } // class Avatar models the state of each Avatar object in // data variables inside the class. It uses constructor // Avatar() to put intial values into these variables. // It uses functions display() and move() to cause // an Avatar to display and move. class Avatar { // function display will now be in class Avatar int avx; int avy; float avscale; int avwiggle ; int avxspeed ; int wiggleAmountspeed ; Avatar(int avx_, int avy_, float avscale_, int avwiggle_, int avxspeed_, int wiggleAmountspeed_) { avx = avx_ ; avy = avy_; avscale = avscale_; avwiggle = avwiggle_ ; avxspeed = avxspeed_; wiggleAmountspeed = wiggleAmountspeed_ ; } void display() { // avy is this avatar's x location; avy is its y location; // float avscale is its scaling factor; avwiggle wiggles body parts. // STEPS 2 & 3: Move the avatar push, translate, scale, and pop code here, // but keep the avatar move code up in draw(). // USE GEOMETRIC TRANSFORMS TO POSITION THE AVATAR // All avatar-specific coordinates are relative to the translated center of the avatar. pushMatrix(); // After popmatrix() we can draw the foregorund scenery. translate(avx, avy); // This is where to center the avatar. scale(avscale); noStroke(); fill(240, 150, 150); ellipse(0, 0, 50, 40); // head quad(-5, 0, 5, 0, 10, 0+40, -10, 0+40); // neck fill(0); // professor gown ellipse(0, 0+60, 40, 80); // torso stroke(0); // stick arms & legs strokeWeight(8); line(0, 0+60, -20+avwiggle, 0+120); // left leg line(0, 0+60, 20-avwiggle, 0+120); // right leg strokeWeight(5); line(0, 0+60, -40, 0+20-2*avwiggle); // left arm line(0, 0+60, 40, 0+20-2*avwiggle); // right arm strokeWeight(2); fill(0, 50, 255); ellipse(-10, 0-5, 10, 10); // avatar's right side of glasses ellipse(10, 0-5, 10, 10); // avatar's right side of glasses line(-5, 0-5, 5, 0-5); // glasses connector line(-15, 0-5, -22, 0-8); // left earpiece line(15, 0-5, 22, 0-8); // right earpiece fill(0); ellipse(0, 0+1, 5, 5); // nose arc(0, 0+10, 20, 10, 0, PI); // mouth quad(-30, 0-15, 30, 0-15, 15, 0-30, -35, 0-30); popMatrix(); // Move popMatrix from draw() to bottom of display(). } void move() { avx = avx + avxspeed ; if (avx < -10 || avx > width+10) { // It went out of range avxspeed = - avxspeed ; // reverse direction } avwiggle = avwiggle + wiggleAmountspeed ; if (avwiggle < -10 || avwiggle > 10) { // It went out of range wiggleAmountspeed = - wiggleAmountspeed ; // reverse direction } } }