Download file
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 5-10: Zoog and conditionals
float x = 250;
float y = 250;
float w = 150;
float h = 150;
float eyeSize = 30;
// Zoog has variables for speed in the horizontal and vertical direction.
float xspeed = 5;
float yspeed = 0.5;
void setup() {
size(700,700);
smooth();
}
void draw() {
// Change the location of Zoog by speed
x = x + xspeed;
y = y + yspeed;
// An IF statement with a logical OR determines if Zoog has reached either the right or left edge of the screen.
// When this is true, we multiply speed by Ð1, reversing ZoogÕs direction!
// Identical logic is applied to the y direction as well.
if ((x > width) || (x < 0)) {
xspeed = xspeed * -1;
}
if ((y > height) || (y < 0)) {
yspeed = yspeed * -1;
}
background(0);
ellipseMode(CENTER);
rectMode(CENTER);
// Draw Zoog's body
stroke(3,205,252);
fill(2,10,90);
rect(x,y,w/5,h*2);
// Draw Zoog's head
fill(3,204,252);
ellipse(x,y-h/2,w,h);
// Draw Zoog's eyes
fill(0);
ellipse(x-w/3+1,y-h/2,eyeSize,eyeSize*2);
ellipse(x+w/3-1,y-h/2,eyeSize,eyeSize*2);
//Draw Zoog's mouth
fill(0);
ellipse(x-w/3+40,y-h/4.5,eyeSize,eyeSize);
//Draw Zoog's arms
stroke(3,204,252);
line(x-w/12,y+h/5,x-w/1,y+h-50);
line(x-w/-12,y+h/5,x-w/-1,y+h-50);
// Draw Zoog's legs
stroke(3,205,252);
line(x-w/12,y+h,x-w/4,y+h+10);
line(x+w/12,y+h,x+w/4,y+h+10);
}