// CSC220F19DemoE_ClassArrayTranslate Parson's csc120 recap Avatar1 [] av1 = new Avatar1 [57]; // setup() called once at the start of Run void setup() { size(800, 700); // At a minimum set display window size in setup(). frameRate(60); // Must be explicit in newer Macs after setting size. rectMode(CENTER); ellipseMode(CENTER); for (int i = 0 ; i < av1.length ; i++) { av1[i] = new Avatar1(int(random(0,width)), int(random(0, height))); } } // draw() is called in a loop at the frameRate, defaults to once every 60th sec. void draw() { background(255, 255, 0); // screen color, use Tools -> Color Selector, RGB int i = 0 ; while (i < av1.length) { av1[i].display(); // function call to the display() function to display avatar av1[i].move(); i++ ; } } class Avatar1 { int elx, ely ; // X, Y location int exspeed, eyspeed ; // X, Y speed int ewidth = 75, eheight = 50 ; // w & h of Avatar1 // added in 020 section at 1:30 float scaleFactor, scaleSpeed ; // scale "location" and speed Avatar1(int initialx, int initialy) { // constructor for a class builds an object of that class elx = initialx ; ely = initialy ; exspeed = round(random(1,4)) ; eyspeed = round(random(1,5)) ; ; scaleFactor = random(.1, 2.0); scaleSpeed = .01 ; } void display() { pushMatrix(); translate(elx, ely); scale(scaleFactor); // added in 1:30 class // display my avatar // body stroke(0); fill(255); rect(0, 0, ewidth/2, eheight/2); //head stroke(0, 0, 255); // Sets stroke around shapes, alternatively noStroke() fill(255, 128, 0); // Sets fill within closed shape, alternatively noFill() ellipse(0, -eheight/2, ewidth, eheight); // X, Y, Ewidth, Eheight popMatrix(); } void move() { // move my avatar elx = elx + exspeed ; ely += eyspeed ; if (elx > width+ewidth) { // if horizontal loc is off right side of display exspeed = - exspeed ; // reverse direction } else if (elx < 0-ewidth) { // if horizontal loc is off left side of display exspeed = - exspeed ; } if (ely < 0-eheight || ely > height+eheight) { // if vertical off top or bottom eyspeed = - eyspeed ; } scaleFactor += scaleSpeed ; if (scaleFactor < 0 || scaleFactor > 2) { scaleSpeed = - scaleSpeed ; } } }