assignment 4

Ball[] ballCollection = new Ball[20];

void setup(){
  size(500,500);
  smooth();
 for( int i=0; i< ballCollection.length;i++){ 
 ballCollection[i] = new Ball(random(0,width),random(0,200));
 }
}

void draw(){
  background(0);
 
 for( int i=0; i< ballCollection.length;i++){
 ballCollection[i].run();
 }
}

class Ball{
  float x=0;
  float y=0;
  float xSpeed=4;
  float ySpeed=0.5;
 
 
  Ball(float _x, float _y){
    x = _x;
    y = _y;
  }
 
  void run(){
    display();
    move();
    bounce();
    gravity(); 
  }
 
  void gravity(){
    ySpeed += 0.2 ;
  }
 
 
  void bounce(){
    if( x> width){
      xSpeed = xSpeed *(-1);
    }
    if( x<0){
      xSpeed = xSpeed *(-1);
    }
    if( y>height){
      ySpeed = ySpeed *(-1);
    }
    if( y<0){
      ySpeed = ySpeed *(-1);
    }
  }
 
   void move(){
     x += xSpeed;
     y += ySpeed;
   }
 
  void display(){
    ellipse(x,y,20,20);
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s