2020年3月26日 星期四

彩色小球掉落

今天老師教的是有關於彩色小球的掉落以及讓小球可以一直發射不會當機!





int []x=new int [1000];//將小球設定為1000顆,滑鼠鍵可在按1000下內都可以執行此程式
int []y=new int [1000];
color[]c=new color [1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
  background(255);
  for(int i=0;i<n;i++){
    fill(c[i]);
    circle(x[i],y[i],30);
    y[i]+=2;
  }
}
void mousePressed(){
  x[n]=mouseX;
  y[n]=mouseY;
  c[n]=color(random(255),random(255),random(255));
  n++;

}


而此圖是讓彩色圈圈往上飛,且小球大球可藉由滑鼠按的時間來變更大小。



int []x=new int [1000];
int []y=new int [1000];
int []r=new int [1000];
color[]c=new color [1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
  background(255);
  for(int i=0;i<n;i++){
    fill(c[i]);
    circle(x[i],y[i],r[i]);
    y[i]--;
  }
  if( mousePressed )r[n-1]++;
}
void mousePressed(){
  x[n]=mouseX;
  y[n]=mouseY;
  r[n]=10;
  c[n]=color(random(255),random(255),random(255));
  n++;

}

此為另一個程式方法的寫法




class Bubble{
  int x;
  int y;
  int r;
  color c;
}
Bubble []b=new Bubble[1000];
int n=0;
void setup(){
  size(400,400);
}
void draw(){
  background(255);
  for(int i=0;i<n;i++)
  {
    fill(b[i].c);
    circle(b[i].x,b[i].y,b[i].r);
    b[i].y--;
  }
  if( mousePressed ) b[n-1].r++;
}
void mousePressed(){
  b[n]=new Bubble();
  b[n].x=mouseX;
  b[n].y=mouseY;
  b[n].r=10;
  b[n].c=color(random(255),random(255),random(255));

 n++;

}


接下來老師教的是定位座標上色




size(300,300);
int [] [] table={{0,0,0,0,0},
                 {1,1,1,0,1},
                 {0,0,1,1,0},
                 {0,0,1,1,0}};
for(int i=0;i<4;i++){
  for(int j=0;j<5;j++){
    if(table[i][j]==1)fill(255,0,0);
    else fill(128);
    rect(j*50,i*50,50,50);
  }
}

利用簡易座標法將座標標示出來,短短幾行程式碼即可呈現出。

今天老師教的程式遊戲是可以做出很多不同顏色的球,我覺得第二段的比較有趣,因為可以讓球像吹氣球一樣變大。總而言之,今天的程式碼也不難,不過多了一些基礎的程式打法,跟我們一開始學的c語言不太一樣,所以一開始有點難接受。

沒有留言:

張貼留言