Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 5-10: Zoog and conditionals
float x = 100;
float y = 100;
float w = 60;
float h = 60;
float eyeSize = 16;

// Zoog has variables for speed in the horizontal and vertical direction.
float xspeed = 3;
float yspeed = 1;


void setup() {
  size(200,200);  
  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(255);  
  ellipseMode(CENTER);
  rectMode(CENTER); 

  // Draw Zoog's body
  stroke(0);
  fill(150);
  rect(x,y,w/6,h*2);

  // Draw Zoog's head
  fill(255);
  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 legs
  stroke(0);
  line(x-w/12,y+h,x-w/4,y+h+10);
  line(x+w/12,y+h,x+w/4,y+h+10);
}
  • http://www.facebook.com/profile.php?id=744985901 Readydot WeAre

    I build this code but my Zoog keeps leaving the draw-area although I constrained him. What am I doing wrong?

    // DEVY MASKOT v1.2 by READYdot

    float colorR;
    float colorG;
    float colorB;
    float x = 0;
    float y = 0;
    float gravity = 0.1;
    float gravityVar = 1;
    float xspeed = 10;
    float yspeed = 8;

    void setup() {
      size(400, 400);
      background(255);
      ellipseMode(CENTER);
      rectMode(CENTER);
      frameRate(30);
      smooth();
    }

    void draw() {
      colorR = random(255);
      colorG = random(255);
      colorB = random(255);
      //background(255);

      if (x = width || y = height) {
        gravity += random(-gravityVar, gravityVar);
      }
      x += (xspeed += gravity);
      y += (yspeed += gravity);

      if (x>=width || x =height || y <= 0) {
        yspeed *= -1;
      }
      constrain (x, 10, width-10);
      constrain (y, 10, width-10);

      // Ohren
      fill(255, 176, 3);
      ellipse(x-52, y, 40, 50);
      ellipse(x+48, y, 40, 50);
      // Kopf
      stroke(1);
      fill(colorR, colorG, colorB);
      ellipse(x, y, 120, 100);
      // Augen
      strokeWeight(3);
      noFill();
      bezier(x-30, y, x-30, y-20, x-10, y-20, x-10, y);
      bezier(x+20, y, x+20, y-20, x+40, y-20, x+40, y);
      // Mund
      // MundUnten
      strokeWeight(2);
      fill(255);
      bezier(x-30, y+20, x-20, y+40, x+30, y+40, x+40, y+20);
      // MundOben
      line(x-30, y+20, x+40, y+20);
      // Zahnlinien
      line(x+5, y+20, x+5, y+34);
      line(x-12, y+20, x-12, y+32);
      line(x+22, y+20, x+22, y+32);
      //Antennen
      noFill();
      bezier(x-20, y-48, x-20, y-48, x-20, y-70, x-20, y-70);
      bezier(x+20, y-48, x+20, y-48, x+20, y-70, x+20, y-70);
    }

  • Anonymous

    you need to say:

    x = constrain (x, 10, width-10);
    y = constrain (y, 10, width-10);