topical media & game development

talk show tell print

graphic-processing-learning-08-example-8-3-example-8-3.pde / pde



  // Learning Processing
  // Daniel Shiffman
  // http://www.learningprocessing.com
  
  // Example 8-3
  Zoog zoog;
  
  void setup() {
    size(200,200);
    smooth();
    zoog = new Zoog(100,125,60,60,16);
  }
  
  void draw() {
    background(255);
    // mouseX position determines speed factor
    float factor = constrain(mouseX/10,0,5);
    zoog.jiggle(factor);
    zoog.display();
  }
  
  class Zoog {
    // Zoog's variables
    float x,y,w,h,eyeSize;
    
    // Zoog constructor
    Zoog(float tempX, float tempY, float tempW, float tempH, float tempEyeSize) {
      x = tempX;
      y = tempY;
      w = tempW;
      h = tempH;
      eyeSize = tempEyeSize;
    }
    
    // Move Zoog
    void jiggle(float speed) {
      // Change the location of Zoog randomly
      x = x + random(-1,1)*speed;
      y = y + random(-1,1)*speed;
      // Constrain Zoog to window
      x = constrain(x,0,width);
      y = constrain(y,0,height);
    }
    
    // Display Zoog
    void display() {
      // Set ellipses and rects to CENTER mode
      ellipseMode(CENTER);
      rectMode(CENTER);
      // Draw Zoog's arms with a for loop
      for (float i = y - h/3; i < y + h/2; i += 10) {
        stroke(0);
        line(x-w/4,i,x + w/4,i);
      }
      // Draw Zoog's body
      stroke(0);
      fill(175);
      rect(x,y,w/6,h);
      // Draw Zoog's head
      stroke(0);
      fill(255);
      ellipse(x,y-h,w,h);
      // Draw Zoog's eyes
      fill(0);
      ellipse(x-w/3,y-h,eyeSize,eyeSize*2);
      ellipse(x + w/3,y - h,eyeSize,eyeSize*2);
      // Draw Zoog's legs
      stroke(0);
      line(x - w/12,y + h/2,x - w/4,y + h/2 + 10);
      line(x + w/12,y + h/2,x + w/4,y + h/2 + 10);
    }
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.