Files
processing4/java/examples/GettingStartedBook/Robots/Robot7_Objects/Robot7_Objects.pde
benfry eb64b2d4fc
2011-01-26 19:22:19 +00:00

54 lines
947 B
Plaintext

// Robot 7: Objects from "Getting Started with Processing"
// by Reas & Fry. O'Reilly / Make 2010
Robot bot1;
Robot bot2;
void setup() {
size(720, 480);
bot1 = new Robot("robot1.svg", 90, 80);
bot2 = new Robot("robot2.svg", 440, 30);
smooth();
}
void draw() {
background(204);
// Update and display first robot
bot1.update();
bot1.display();
// Update and display second robot
bot2.update();
bot2.display();
}
class Robot {
float xpos;
float ypos;
float angle;
PShape botShape;
float yoffset = 0.0;
// Set initial values in constructor
Robot(String svgName, float tempX, float tempY) {
botShape = loadShape(svgName);
xpos = tempX;
ypos = tempY;
angle = random(0, TWO_PI);
}
// Update the fields
void update() {
angle += 0.05;
yoffset = sin(angle) * 20;
}
// Draw the robot to the screen
void display() {
shape(botShape, xpos, ypos + yoffset);
}
}