Skip to content

Instantly share code, notes, and snippets.

@slambert
Created September 29, 2015 18:12
Show Gist options
  • Save slambert/92f2d31e8acc72cc2f10 to your computer and use it in GitHub Desktop.
Save slambert/92f2d31e8acc72cc2f10 to your computer and use it in GitHub Desktop.
This Processing sketch is an example of one way to use functions.
/*
Robot with Red Eyes with functions
Steve Lambert
Originally written sometime in 2012
* * *
This Processing Sketch is an example of one way to use functions.
*/
// Notice I have declared no global variables - I don't need them!
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
// Here's what the parameters are: (int robotWidth, int xPos, int yPos)
//drawRobot(250,mouseX,mouseY);
drawRobot(80,mouseX-250,mouseY);
drawRobot(120,mouseX,mouseY);
drawRobot(60,mouseX+160,mouseY);
drawRobot(20,mouseX+80,mouseY-30); // super tiny
}
// Thus begins the robot function:
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
// Note that all the sizes are relative multiples of robotWidth, this is
// so they'll adjust to various sizes.
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment