topical media & game development

talk show tell print

graphic-processing-learning-23-example-23-4-example-23-4.pde / pde



  // Learning Processing
  // Daniel Shiffman
  // http://www.learningprocessing.com
  
  // Example 23-4: Super fancy ArrayList and rectangle particle system
  
  // Declaring a global variable of type ArrayList
  ArrayList particles;
  
  // A "Rectangle" will suck up particles
  Rectangle blackhole;
  
  void setup() {
    size(200,200);
    blackhole = new Rectangle(50,150,100,25);
    particles = new ArrayList();
    smooth();
  }
  
  void draw() {
    background(255);
    
    // Displaying the Rectangle
    stroke(0);
    fill(175);
    rect(blackhole.x, blackhole.y, blackhole.width,blackhole.height);
    
    // Add a new particle at mouse location
    particles.add(new Particle(mouseX,mouseY));
    
    // Loop through all Particles
    for (int i = particles.size() - 1; i >= 0; i-- ) {
      Particle p = (Particle) particles.get(i);
      p.run();
      p.gravity();
      p.display();
      
      // If the Rectangle contains the location of the Particle, stop the Particle from moving.
      if (blackhole.contains(p.x,p.y)) { 
        p.stop();
      }
      
      // If the particle is no longer needed, it is deleted from the list
      if (p.finished()) {
        particles.remove(i);
      }
    }
  }
  


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