topical media & game development

talk show tell print

graphic-processing-learning-10-example-10-10-example-10-10.pde / pde



  // Learning Processing
  // Daniel Shiffman
  // http://www.learningprocessing.com
  
  // Example 10-10: The raindrop catching game
  
  Catcher catcher;    // One catcher object
  Timer timer;        // One timer object
  Drop[] drops;       // An array of drop objects
  int totalDrops = 0; // totalDrops
  
  void setup() {
    size(400,400);
    smooth();
    catcher = new Catcher(32); // Create the catcher with a radius of 32
    drops = new Drop[1000];    // Create 1000 spots in the array
    timer = new Timer(300);   // Create a timer that goes off every 2 seconds
    timer.start();             // Starting the timer
  }
  
  void draw() {
    background(255);
    
    // Set catcher location
    catcher.setLocation(mouseX,mouseY); 
    // Display the catcher
    catcher.display(); 
    
    // Check the timer
    if (timer.isFinished()) {
      // Deal with raindrops
      // Initialize one drop
      drops[totalDrops] = new Drop();
      // Increment totalDrops
      totalDrops ++ ;
      // If we hit the end of the array
      if (totalDrops >= drops.length) {
        totalDrops = 0; // Start over
      }
      timer.start();
    }
    
    // Move and display all drops
    for (int i = 0; i < totalDrops; i++ ) {
      drops[i].move();
      drops[i].display();
      if (catcher.intersect(drops[i])) {
        drops[i].caught();
      }
    }
  }
  


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