Robot as a function

Exercise 7-10

and the code…

void setup() {
size(600, 300);
smooth();
background(240);
rectMode(CENTER);
// done with setup
}

void draw() {

background(240); // this kepps the whole thing from redrawing itself
drawRobot(80,mouseX-200,mouseY);
drawRobot(120,mouseX,mouseY);
drawRobot(60,mouseX+160,mouseY);

}

void drawRobot(int robotWidth, int xPos, int yPos) {
// robotWidth is the width of the robot body
// xPos and yPos are the x and y coordinates of the center of the robot

// body and arms
fill(200); // make it grey
rect(xPos,yPos,robotWidth,robotWidth*.6); //body is 50x30
ellipse(xPos-robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);
ellipse(xPos+robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);

// legs
float legStart = xPos-robotWidth*.3;
for (int i=0; i<2; i++) { rect(legStart,yPos+robotWidth*.4,robotWidth*.2,robotWidth*.2); legStart = legStart+robotWidth*.6; } // head rect(xPos,yPos-robotWidth*.45,robotWidth*.8,robotWidth*.3); // head is 40x15 // mouth fill(255); rect(xPos,yPos-robotWidth*.37,robotWidth*.4,robotWidth*.06); // eyes if (mousePressed == true) { fill(255,0,0); // make the eyes red } else { fill(255); // make the eyes white } ellipse(xPos-robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1); ellipse(xPos+robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1); }