2023年10月26日 星期四

WW-Week07_Halloween_magic_cat_game

 第一節課

萬聖節就是要寫程式,萬聖節的可愛小程式

第一個小程式weeek07_1_halloween_line

畫線,滑鼠按下去可畫線,放開就清除

void setup(){
  size(400,400);
}
void draw(){
  //background(255);//背景可以清畫面但是一條線畫不完
  if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
  //滑鼠按下去可以畫一條線
  
}
void mouseReleased(){///所以改當滑鼠放開時才清背景
  background(255);
}

第二個小程式weeek07_2_halloween_ghost_walking

加鬼的座標,鬼的位置,鬼移動方向,但鬼移動時會有殘影

void setup(){
  size(400,400);
}
float ghostX=40, ghostY=20;
void draw(){
  ellipse(200,200,15,15);///主角
  if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
  ellipse( ghostX, ghostY, 15, 15);//鬼
  float dx = 200-ghostX, dy = 200-ghostY, len= sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){///所以改當滑鼠放開時才清背景
  background(255);
}

第三個小程式weeek07_3_halloween_ArrayList_for_loop

解決殘影問題,但直接加清背景的話又會把上面的筆劃清掉,所以用ArrayList資料結構,畫線的時候像是一堆點點連在一起

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  ellipse(200,200,15,15);
  //if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
  for (int i=0;i<pt.size();i++){
    PVector p=pt.get(i);
    ellipse(p.x,p.y,3,3);
  }
  ellipse( ghostX, ghostY, 15, 15);//鬼
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){///所以改當滑鼠放開時才清背景
  background(255);
}

第四個小程式weeek07_4_halloween_ArrayList_mouseReleased_remove

解決ArrayList留下的筆觸,要畫完就消失,用刪資料結構的方式 : remove(i) + 上倒過來的for迴圈(避免從前面開始刪的話會一直在搬家),再來將相近的兩個點連線

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  ellipse(200,200,15,15);
  //if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
  for (int i=0;i<pt.size()-1;i++){///-1避免超過範圍
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);//把相近的兩個資料結構位置連線
  }
  ellipse( ghostX, ghostY, 15, 15);//鬼
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}
第二節課

第五個小程式weeek07_5_halloween_decide_type1_dx_100_dy_10

要畫出一定的長度的橫線變紅色,直線變藍色

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  ellipse(200,200,15,15);
  ellipse(ghostX,ghostY,15,15);.////偷搬上來
  
  strokeWeight(4);//線變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
  }  
  if(dpx>100 && dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if(dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}

第六個小程式weeek07_6_halloween_decide_type2_dx_40_dy_100

直線畫超過長度變藍色,人物和鬼不受影響


ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  strokeWeight(1);
  stroke(0);ellipse(200,200,15,15);
  stroke(0);ellipse(ghostX,ghostY,15,15);
  
  strokeWeight(4);//變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
  }
  if( dpx>100 &&  dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}

第七個小程式weeek07_7_halloween_decide_type3_dx_dy_plus_plus

挑戰!!試看看V型變色,一定有一段往右下角走(p2.x - p.x>0 && p2.y-p.y>0),若第二階段往右上(x往右,y往上)走那就變色

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  strokeWeight(1);
  stroke(0);ellipse(200,200,15,15);
  stroke(0);ellipse(ghostX,ghostY,15,15);
  
  strokeWeight(4);//變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  int rightdown = 0;///往右下
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
    if(p2.x - p.x>0 && p2.y-p.y>0) rightdown++;
  }
  if( dpx>100 &&  dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else if(rightdown>20) stroke(0,255,0);///右下變綠色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}

第八個小程式weeek07_8_halloween_decide_type3_plus_plus_len

有個問題如果滑鼠動太快的話會沒法湊齊20顆,所以不會變色,小小的改一下

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  strokeWeight(1);
  stroke(0);ellipse(200,200,15,15);
  stroke(0);ellipse(ghostX,ghostY,15,15);
  
  strokeWeight(4);//變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  float rightdown = 0;///往右下
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
    if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
  }
  if( dpx>100 &&  dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else if(rightdown>150) stroke(0,255,0);///右下變綠色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}
第三節課

第九個小程式weeek07_9_halloween_decide_type3_greenpart2

試試看怎麼幫倒勾勾和正勾勾分開
ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  strokeWeight(1);
  stroke(0);ellipse(200,200,15,15);
  stroke(0);ellipse(ghostX,ghostY,15,15);
  
  strokeWeight(4);//變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  float rightdown = 0, rightup =0;///往右下,往右上
  boolean green_part1 = false,green_ok=false;
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
    if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
    if(p2.x - p.x>0 && p2.y-p.y<0) rightup+=sqrt(dpx*dpx+dpy*dpy);
    
    if(green_part1==false && rightdown>150) green_part1 = true;
    else if(green_part1==true && rightup>150) green_ok=true;
  }
  if( dpx>100 &&  dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else if(green_ok) stroke(0,255,0);///右下變綠色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}
第十個小程式weeek07_A_halloween_decide_type3_greenpart2_green_rightup
分出綠勾

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構
void setup(){
  size(400,400);
  background(255);
}
float ghostX=40, ghostY=20;
void mouseDragged(){//按下滑鼠拖曳
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  strokeWeight(1);
  stroke(0);ellipse(200,200,15,15);
  stroke(0);ellipse(ghostX,ghostY,15,15);
  
  strokeWeight(4);//變粗
  float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距
  float rightdown = 0, green_rightup =0;///往右下,往右上
  boolean green_part1 = false,green_ok=false;
  for (int i=0; i<pt.size()-1; i++){///累積移動量
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    dpx += abs(p2.x - p.x);//累積x的移動量
    dpy += abs(p2.y - p.y);//累積y的移動量
    if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
    if(green_part1 && p2.x - p.x>0 && p2.y-p.y<0) green_rightup+=sqrt(dpx*dpx+dpy*dpy);
    
    if(green_part1==false && rightdown>150) green_part1 = true;
    else if(green_part1==true && green_rightup>150) green_ok=true;
  }
  if( dpx>100 &&  dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線
  else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色
  else if(green_ok) stroke(0,255,0);///右下變綠色
  else stroke(0);//不是就黑色 
  for (int i=0; i<pt.size()-1; i++){///畫線
    PVector p=pt.get(i) , p2 = pt.get(i+1);
    line( p.x, p.y, p2.x, p2.y);
  }
  
  float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);
  ghostX += dx/ len/ 3;
  ghostY += dy/ len/ 3;
}
void mouseReleased(){
  for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈
    pt.remove(i);
  }
}

第十一個小程式weeek07_B_halloween_decide_type4_yellow

用正綠勾推出倒勾

ArrayList<PVector> pt = new ArrayList<PVector>();///資料結構 void setup(){   size(400,400);   background(255); } float ghostX=40, ghostY=20; void mouseDragged(){//按下滑鼠拖曳   pt.add(new PVector(mouseX,mouseY)); } void draw(){   background(255);   strokeWeight(1);   stroke(0);ellipse(200,200,15,15);   stroke(0);ellipse(ghostX,ghostY,15,15);      strokeWeight(4);//變粗   float dpx=0, dpy=0;///要看p.x p2.x的差距, p.y p2.y的差距   float rightdown = 0, green_rightup =0;///往右下,往右上   float rightup = 0, yellow_rightdown =0;   boolean green_part1 = false,green_ok=false;   boolean yellow_part1 = false,yellow_ok=false;   for (int i=0; i<pt.size()-1; i++){///累積移動量     PVector p=pt.get(i) , p2 = pt.get(i
+1);     dpx += abs(p2.x - p.x);//累積x的移動量     dpy += abs(p2.y - p.y);//累積y的移動量     if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);     if(green_part1 && p2.x - p.x>0 && p2.y-p.y<0) green_rightup+=sqrt(dpx*dpx+dpy*dpy);     if(p2.x - p.x>0 && p2.y-p.y<0) rightup+=sqrt(dpx*dpx+dpy*dpy);     if(yellow_part1 && p2.x - p.x>0 && p2.y-p.y>0) yellow_rightdown+=sqrt(dpx*dpx+dpy*dpy);          if(green_part1==false && rightdown>150) green_part1 = true;     else if(green_part1==true && green_rightup>150) green_ok=true;     if(yellow_part1==false && rightup>150) yellow_part1 = true;     else if(yellow_part1==true && yellow_rightdown>150) yellow_ok=true;   }   if( dpx>100 && dpy<40) stroke(255,0,0);///超過了長度100type1紅色的橫線   else if( dpx<40 && dpy>100) stroke(0,0,255);///畫直線超過100變藍色   else if(green_ok) stroke(0,255,0);///右下變綠色   else if(yellow_ok)stroke(255,255,0);///黃色   else stroke(0);//不是就黑色   for (int i=0; i<pt.size()-1; i++){///畫線     PVector p=pt.get(i) , p2 = pt.get(i+1);     line( p.x, p.y, p2.x, p2.y);   }      float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy);   ghostX += dx/ len/ 3;   ghostY += dy/ len/ 3; } void mouseReleased(){   for(int i=pt.size()-1;i>=0;i--){///到過來的for迴圈     pt.remove(i);   } }

沒有留言:

張貼留言