topical media & game development

talk show tell print

graphic-processing-learning-22-example-22-2-example-22-2.pde / pde



  // Learning Processing
  // Daniel Shiffman
  // http://www.learningprocessing.com
  
  // Example 22-2: Polymorphism
  
  // One array of Shapes
  Shape[] shapes = new Shape[30];
  
  void setup() {
    size(200,200);
    smooth();
    
    for (int i = 0; i < shapes.length; i++ ) {
      int r = int(random(2));
      // Randomly put either circles or squares in our array
      if (r == 0) {
        shapes[i] = new Circle(100,100,10,color(random(255),100));
      } else {
        shapes[i] = new Square(100,100,10);
      }
    }
  }
  
  void draw() {
    background(255);
    // Jiggle and display all shapes
    for (int i = 0; i < shapes.length; i++ ) {
      shapes[i].jiggle();
      shapes[i].display();
    }
  }
  


(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.