วันพุธที่ 22 ตุลาคม พ.ศ. 2557

Lab 5 - class Balloon




int n=5;
Balloon[] B = new Balloon[n];

void setup(){
  size(350,500);
  background(255);
  for(int i=0;i<B.length;i++){
    B[i] = new Balloon((width/(n+1))*(i+1),height+((height/n)*(i+1)),20,color(255,0,0));
  }
}

void draw(){
  background(255);
  for(int i=0;i<B.length;i++){
    B[i].display();
    B[i].moveUp(1);
  }
}

void mousePressed(){
  for(int i=0;i<B.length;i++){
    B[i].reset();
  }
}

class Balloon{
  int x,y,r,c;
  int dx,dy,dr,dc;

  Balloon(int x,int y,int r,color c){
    this.x = x;
    this.y = y;
    this.r = r;
    this.c = c;
    this.dx = x;
    this.dy = y;
    this.dr = r;
    this.dc = c;
  }

  void display(){
    int WBalloon = 2*this.r;
    int HBalloon = 3*this.r;
    fill(this.c);
    ellipse(this.x,this.y,WBalloon,HBalloon);
    line(this.x,this.y+(HBalloon/2),this.x,this.y+(1.2*HBalloon));
  }

  void moveUp(int speed){
    this.y = this.y - speed;
  }

  void reset(){
    this.x = dx;
    this.y = dy;
    this.r = dr;
    this.c = dc;
  }
}