Week 10
1.畫出四個圓
程式碼:
size (300,500);
ellipse(75,200+75,150,150);
ellipse(75+150,200+75,150,150);
ellipse(75,200+75+150,150,150);
程式碼:
size (300,500);
ellipse(50,200+50,100,100);
ellipse(50+100,200+50,100,100);
ellipse(50+100+100,200+50,100,100);
ellipse(50,200+50+100,100,100);
ellipse(50+100,200+50+100,100,100);
ellipse(50+100+100,200+50+100,100,100);
ellipse(50,200+50+100+100,100,100);
ellipse(50+100,200+50+100+100,100,100);
化簡程式碼:
size (300,500);
for(int y=0;y<3;y++){
for(int x=0;x<3;x++) {
ellipse(50+x*100,200+50+y*100,100,100);
}
}
3.直接畫出5X5
程式碼:
size (300,500);
int n=5;
int R=300/n,w=R/2; ///R指的是直徑,w指的是半徑
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
ellipse(w+x*R,200+w+y*R,R,R) ;
}
4.從小變大
程式碼:
void setup(){
size (300,500);
}
int n=5;
int R=300/n,w=R/2,RR=0;
int R=300/n,w=R/2,RR=0;
void draw(){
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
ellipse(w+x*R,200+w+y*R,RR,RR);
}
}
if(RR<R)RR++; ///如果碰到邊停止
5.球變大變多
程式碼:
void setup(){
size (300,500);
}
int n=3,RR=0;
void draw(){
background(0); ///黑色
int R=300/n;
int w=R/2;
int w=R/2;
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
ellipse(w+x*R,200+w+y*R,RR,RR);
}
}
if(RR<R)RR+=R/30; ///慢慢變大,直到遇到牆
else{
n++;
RR=0; ///當變大時,遇到牆時,加一顆圓然後重新開始
6.滑鼠指到的地方變紅
程式碼:
void setup(){size (300,500);
}
int ansX=1,ansY=2; ///例外的
int n=3,RR=0;
void draw(){
background(0);
int R=300/n,w=R/2;
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
if( x==ansX && y==ansY ) fill(128); ///答案要有點不同,灰色
else fill(255); ///白色 (在這題黃色底可以不用寫)
if( dist(mouseX,mouseY,w+x*R,200+w+y*R)<w) fill(255,0,0); ///現在鼠標選中誰
ellipse(w+x*R,200+w+y*R,RR,RR);///從小變到大
} ///因為寬度是300,有n個球去分,每個球分到直徑300/n,半徑300/n/2
}
if(RR<R)RR+=R/30; ///如果還沒超過範圍,++從小變到大
} ///為了讓速度一致,改成RR+=R/10變得很快
程式解讀:
if( dist(mouseX,mouseY,w+x*R,200+w+y*R)<w) fill(255,0,0);從這句的分析來說, dist指的是距離,如果滑鼠指到的地方小於圓心的半徑,就填滿紅色!!! ((黃色的 部分為圓心
7.按一下不同的顏色會換地方
程式碼:
void setup(){
size (300,500);}
int ansX=1,ansY=2; ///答案
int n=3,RR=0;
void draw(){
background(0);
int R=300/n,w=R/2;
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
if(x==ansX && y==ansY)fill(240);
else fill(255);
if(dist(mouseX,mouseY,w+x*R,200+w+y*R)<w){ ///現在的鼠標選中
if(mousePressed && x==ansX && y==ansY){ ///他剛好也就是答案
ansX=int(random(n));
ansY=int(random(n));
}
}
ellipse(w+x*R,200+w+y*R,RR,RR); ///從小變到大
}
}
if(RR<R)RR+=R/30;
}
程式解讀:
random的意思是亂數,而int(random(n))是宣告一個亂數,前面
if(mousePressed && x==ansX && y==ansY) 這一行說的是,如果按下去剛好是答 案,就隨便跳到一個n(圈)
8.完成品
程式碼:
void setup(){size (300,500);
colorMode(HSB,256); ///改色彩系統 後面指的是最大的顏色
}
int ansX=1,ansY=2; ///不一樣的答案
int n=3,RR=0;
int H=0; ///Hue色調
void draw(){
background(0);
int R=300/n,w=R/2;
for(int y=0;y<n;y++){
for(int x=0;x<n;x++){
if(x==ansX && y==ansY) fill(H-10,243,234);
else fill(H,255,255);
if( dist(mouseX,mouseY,w+x*R,200+w+y*R )< w){
if(mousePressed && x==ansX && y==ansY){
ansX=int(random(n));
ansY=int(random(n));
H+=20; ///改色彩系統
if(H>256)H=0; ///重複循環
}
}
ellipse(w+x*R,200+w+y*R,RR,RR);
}
}
if(RR<R)RR+=R/30;
}
程式解讀:
HSB指的是(色調,濃度,亮度),所以面淺橘色的底都是,只要改H質就好,後面 可以隨便設置。









沒有留言:
張貼留言