topical media & game development

talk show tell print

graphic-processing-site-examples-Topics-Advanced-Data-ArrayListClass-Ball.pde / pde



  // Simple bouncing ball class
  
  class Ball {
    
    float x;
    float y;
    float speed;
    float gravity;
    float w;
    float life = 255;
    
    Ball(float tempX, float tempY, float tempW) {
      x = tempX;
      y = tempY;
      w = tempW;
      speed = 0;
      gravity = 0.1;
    }
    
      void move() {
      // Add gravity to speed
      speed = speed + gravity;
      // Add speed to y location
      y = y + speed;
      // If square reaches the bottom
      // Reverse speed
      if (y > height) {
        // Dampening
        speed = speed * -0.8;
        y = height;
      }
    }
    
    boolean finished() {
      // Balls fade out
      life--;
      if (life < 0) {
        return true;
      } else {
        return false;
      }
    }
    
    void display() {
      // Display the circle
      fill(0,life);
      //stroke(0,life);
      ellipse(x,y,w,w);
    }
  }  
  


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