2013年3月23日 星期六

[對照]蝦爸的p5範例-3



蝦爸在本案例中運用了ArrayList這原生的Java功能,來儲存各式類別。在本案例中,隨著表演的進行,可以任意敲擊鍵盤,製造有趣好玩的視覺物件。

ArrayList功能可以用來索引儲存在其中的物件,所以當自定用來繪製圖形的類別後,可以用ArrayList儲存它,並依需求呼叫出來。

使用方法,在Processing的算圖視窗中按下:
鍵盤數字鍵1,產生房子
鍵盤數字鍵2,產生雲朵
鍵盤數字鍵3,產生路燈

Example Download

[Processing code] ↓

C_house h1;
C_cloud c1;
C_light l1;
C_star s1;

ArrayList hgroup;
ArrayList cgroup;
ArrayList lgroup;
ArrayList sgroup;

void setup() {
  size(800, 300, OPENGL);
  h1=new C_house();
  c1=new C_cloud();
  l1=new C_light();
  s1=new C_star();

  sgroup=new ArrayList();
  for (int i=0;i<200;i++) {
    sgroup.add(new C_star());
  }

  hgroup=new ArrayList();
  hgroup.add(new C_house());

  cgroup=new ArrayList();
  cgroup.add(new C_cloud());

  lgroup=new ArrayList();
  lgroup.add(new C_light());
}

void draw() {

  background(30, 30, 60);
  for (int i=0;i<hgroup.size();i++) {
    ((C_house)hgroup.get(i)).update();
    ((C_house)hgroup.get(i)).graphic();
  }

  for (int i=0;i<cgroup.size();i++) {
    ((C_cloud)cgroup.get(i)).update();
    ((C_cloud)cgroup.get(i)).graphic();
  }

  for (int i=0;i<lgroup.size();i++) {
    ((C_light)lgroup.get(i)).update();
    ((C_light)lgroup.get(i)).graphic();
  }

  for (int i=0;i<sgroup.size();i++) {
    ((C_star)sgroup.get(i)).update();
    ((C_star)sgroup.get(i)).graphic();
  }

  strokeWeight(2);
  stroke(0);
  fill(20, 20, 30);
  rect(0, height-50, width, 50);

  for (int i=0;i<hgroup.size();i++) {
    if (((C_house)hgroup.get(i)).sx<0) {
      hgroup.remove(i);
    }
  }

  for (int i=0;i<cgroup.size();i++) {
    if (((C_cloud)cgroup.get(i)).px[0]<0) {
      cgroup.remove(i);
    }
  }

  for (int i=0;i<lgroup.size();i++) {
    if (((C_light)lgroup.get(i)).px<0) {
      lgroup.remove(i);
    }
  }
}

void keyPressed() {
  if (key=='1') {
    if (hgroup.size()<50) {
      hgroup.add(new C_house());
      for (int i = 0;;)
      {
        if (i >= hgroup.size()-1)
        {
          break;
        }
        if (((C_house)hgroup.get(i)).dis == ((C_house)hgroup.get(hgroup.size()-1)).dis)
        {
          ((C_house)hgroup.get(hgroup.size()-1)).Change_Dis();
          i = 0;
          continue;
        }
        i++;
      }
    }
  }



  else if (key=='2') {
    cgroup.add(new C_cloud());
  }
  else if (key=='3') {
    lgroup.add(new C_light());
  }
  else {
  }
}

class C_star {
  float px;
  float py;
  float c;
  float size;
  float count;
  int flash;

  C_star() {
    px=random(-1*width, width*1.5);
    py=random(-1*height, height);
    size=random(1, 5);
    flash=(int)random(1, 30);
  }
  void graphic() {
    pushMatrix();
    translate(0, 0, -200);
    fill(255, c);
    noStroke();
    ellipse(px, py, size, size);
    popMatrix();
  }

  void update() {

    count++;
    count=count%flash;
    if (count==0) {
      c=random(0, 255);
    }
  }
}

class C_light {
  float px;
  float c;
  boolean dir;
  float size;
  C_light() {
    dir=random(0, 2)>1;
    px=width;
    size=random(100, 150)/100;
  }

  void graphic() {
    pushMatrix();

    stroke(0);
    fill(255);
    translate(px, height-90, -3);

    if (dir==true) {
      rotateY(radians(180));
    }
    else {
      rotateY(radians(0));
    }
    scale(size);
    rect(0, 0, 3, 40);
    noStroke();
    fill(255, 255, 0, c);
    triangle(-15, -15, -30, 40, 0, 40);
    stroke(0);
    rotateZ(radians(-45));
    fill(255);
    rect(0, 0-20, 3, 20);
    popMatrix();
  }

  void update() {
    px--;
    c=random(50, 250);
  }
}

class C_house {
  float sx;
  float sy;
  float w;
  float h;
  int col;
  int row;
  int winnum;
  float win_w;
  float win_h;
  float c;
  float temp_c;
  float dis;
  C_house() {
    w=random(30, 100);
    h=random(50, 200);
    sx=width;
    sy=height-50-h;
    winnum=((int)random(1, 5))*2;
    win_w=w/4;
    win_h=w/4;
    c=255;
    temp_c=random(50, 100);
    dis=(int)(random(1, 50))*(-6);
    //dis = 0;
  }
  void update() {
    sx--;
    if (c>temp_c) {
      c--;
    }
  }

  void graphic() {

    pushMatrix();
    translate(0, -dis*0.4, dis);
    stroke(50);
    strokeWeight(2);
    fill(c, 255-c,255-c);
    rect(sx, sy, w, h);
    //起始點x,起始點y ,起始點x+寬,起始點y+高
    stroke(0);
    for (int i=0;i<winnum;i++) {
      strokeWeight(1);
      fill(c-50, 255);
      col=floor(i/2);
      row=i%2;
      rect(
      sx+row*(win_w*2)+win_w/2, 
      sy+col*(win_h*1.5)+win_h/2, 
      win_w, 
      win_h);
    }
    popMatrix();
  }

  void Change_Dis()
  {
    dis=(int)(random(1, 50))*(-6);
  }
  void Change_Dis(float Enter_Dis)
  {
    dis = Enter_Dis;
  }
}

class C_cloud {
  float [] px;
  float [] py;
  float [] sx;
  float [] sy;
  int num;
  float dis;
  float c;

  C_cloud() {
    num=(int)random(3, 15);
    px=new float[num];
    py=new float[num];
    sx=new float[num];
    sy=new float[num];
    dis=(random(1, 50))*(-1);
    c=255+(dis*2);
    for (int i=0;i<num;i++) {

      if (i<=0) {
        px[0]=width;
        py[0]=random(10, 60);
      }
      else {
        px[i]=px[i-1]+random(-20, 20);
        py[i]=py[i-1]+random(-5, 5);
      }
      sx[i]=random(10, 40);
      sy[i]=random(10, 20);
    }
  }

  void graphic() {
    pushMatrix();
    translate(0, 0, dis);
    noStroke();
    fill(c);
    for (int i=0;i<num;i++) {
      ellipse(px[i], py[i], sx[i], sy[i]);
    }
    popMatrix();
  }

  void update() {
    for (int i=0;i<num;i++) {
      px[i]=px[i]-1;
    }
  }
}


[Pd code] ↓



同場加映:以Pd撰寫的相同程式,並加上與聲響互動的功能,當發出聲響時,會有對應的視覺造型產生。

Pd Example