// Dropping particles // Click the mouse to add obstacles // Daniel Shiffman // www.shiffman.net // Nov 2006 // This code is not particularly well commented, sorry. . .! ArrayList particles; ArrayList obstacles; void setup() { size(320,240); frameRate(60); particles = new ArrayList(); smooth(); obstacles = new ArrayList(); obstacles.add(new Obstacle(width/2,height/2,50,5)); } void draw() { particles.add(new Particle(width/3,0)); // Add a new particle each time particles.add(new Particle(2*width/3,0)); // Add a new particle each time background(100); // Iterate through our arraylist and get each particle for (int i = particles.size()-1; i >= 0; i--) { // When something comes out of the arraylist via "get" we have to // remind ourselves what type it is. In this case, it's an instance of "Particle" Particle p = (Particle) particles.get(i); p.run(); p.gravity(); p.render(); for (int k = 0; k < obstacles.size(); k++) { Obstacle o = (Obstacle) obstacles.get(k); p.hitObstacle(o); } if (p.finished()) { particles.remove(i); } } for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.display(); } } void mousePressed() { obstacles.add(new Obstacle(mouseX,mouseY,50,5)); }