2020年3月26日 星期四

week04

程式設計二


瘋狂程設

1. 瘋狂程設-第04週練習模式-基礎題:分開整數的每個數字

2. 瘋狂程設-第04週練習模式-進階題:函數判斷質數 
3. 瘋狂程設-第04週練習模式-進階題:擲骰統計 

processing

1. 點擊一次滑鼠可以出現一顆球,且每次出現的球會有不一樣的顏色

程式:
int []x=new int[1000];
int []y=new int[1000];
color []c=new color[10000];
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++;
}


2. 點擊一次滑鼠可以出現一顆球,且每次出現的球會有不一樣的顏色並隨著滑鼠點擊的時間慢慢變大

程式:
int []x=new int[1000];
int []y=new int[1000];
int []r=new int[1000];
color []c=new color[10000];
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++;
}

3. (作業二)物件導向的寫法

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

4. 陣列練習

程式:
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);
  }
}

5. 被滑鼠點擊的方塊可以變紅色

程式:
int [][]table=new int[6][6];
void setup(){
  size(300,300);
}
void draw(){
  background(128);
  for(int i=0;i<6;i++){
    for(int j=0;j<6;j++){
      if(table[i][j]==1) fill(255,0,0);
      else fill(128);
      rect(j*50,i*50,50,50);
    }
  }
}
void mousePressed(){
  int x=(mouseX)/50;
  int y=(mouseY)/50;
  table[y][x]=1;
}

沒有留言:

張貼留言