วันอาทิตย์ที่ 21 กันยายน พ.ศ. 2557

Lab 3 - Balloon

 BALLOON UNLIMIT !!!




void setup(){
  size(600,500);
  background(255);
  frameRate(30);
}
int MaxWave = 50;
float[] BalloonX1 = new float[MaxWave];
float[] BalloonY1 = new float[MaxWave];
float[] BalloonR1 = new float[MaxWave];
color[] BalloonC1 = new color[MaxWave];
int CountBalloon1 = -1;
float[] BalloonX2 = new float[MaxWave];
float[] BalloonY2 = new float[MaxWave];
float[] BalloonR2 = new float[MaxWave];
color[] BalloonC2 = new color[MaxWave];
int CountBalloon2 = -1;
int Wave = 1;
int MinR = 15,MaxR = 45;
void draw(){
  background(255);
  //setup balloon
  if((frameCount>=frameRate)&&(Wave==1)){
    frameCount = 0;
    SetupBalloon1(CountBalloon1);
    CountBalloon1 = CountBalloon1 +1;
  }
  if((frameCount>=frameRate)&&(Wave==2)){
    frameCount = 0;
    SetupBalloon2(CountBalloon2);
    CountBalloon2 = CountBalloon2 +1;
  }
  //draw balloon
  for(int i=0;i<CountBalloon1;i++){
    DrawBalloon(BalloonX1[i],BalloonY1[i],BalloonR1[i],BalloonC1[i]);
    BalloonY1[i] = BalloonY1[i]-1;
  }
  for(int i=0;i<CountBalloon2;i++){
    DrawBalloon(BalloonX2[i],BalloonY2[i],BalloonR2[i],BalloonC2[i]);
    BalloonY2[i] = BalloonY2[i]-1;
  }
  //new wave
  if((CountBalloon1>=MaxWave)&&(Wave==1)){
    CountBalloon2=-1;
    Reset2();
    Wave = 2;
  }
  if((CountBalloon2>=MaxWave)&&(Wave==2)){
    CountBalloon1=-1;
    Reset1();
    Wave = 1;
  }
}

void DrawBalloon(float X,float Y,float R,color C){
  fill(C);
  ellipse(X,Y,2.5*R,3*R);
  line(X,Y+(1.5*R),X,Y+(4.5*R));
}

void SetupBalloon1(int Count){
  if(Count>=0){
    BalloonR1[Count] = random(MinR,MaxR);
    BalloonC1[Count] = color(random(0,255),random(0,255),random(0,255));
    BalloonX1[Count] = random(0,width);
    BalloonY1[Count] = height+(2*BalloonR1[Count]);
  }
}

void SetupBalloon2(int Count){
  if(Count>=0){
    BalloonR2[Count] = random(MinR,MaxR);
    BalloonC2[Count] = color(random(0,255),random(0,255),random(0,255));
    BalloonX2[Count] = random(0,width);
    BalloonY2[Count] = height+(2*BalloonR2[Count]);
  }
}
void mouseClicked() {
  background(255);
  Reset1();
  Reset2();
  Wave = 1;
}
void Reset1(){
    for(int i=0;i<MaxWave;i++){
    BalloonR1[i] = 0;
    BalloonC1[i] = 0;
    BalloonX1[i] = 0;
    BalloonY1[i] = height+MaxR;
    }
    CountBalloon1 = -1;

    frameCount = 0;
}
void Reset2(){
    for(int i=0;i<MaxWave;i++){
    BalloonR2[i] = 0;
    BalloonC2[i] = 0;
    BalloonX2[i] = 0;
    BalloonY2[i] = height+MaxR;
    }
    CountBalloon2 = -1;

    frameCount = 0;
}