2023年10月26日 星期四

LS._. Week07

Halloween:line

  • 下載最新版processing
  • 寫出畫線程式碼
  • 加上背景顏色及加上程式碼讓線可以消失
  • 程式碼
void setup(){
  size(400,400);
  background(255);
}
void draw(){
  if(mousePressed)line(mouseX,mouseY,pmouseX,pmouseY);
}
void mouseReleased(){
  background(255);
}

Halloween:ghost walking

  • 另存程式碼,更改一下,要加入鬼
  • 鬼用圈圈表示,要會走路(往中間靠)
  • 程式碼
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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);
}

Halloween:ArrayList for loop

  • 另存程式碼,更改它,讓鬼沒有殘影
  • 但畫的線不會消失
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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);
}

Halloween:ArrayList mouseReased remove

  • 另存程式碼,更改它,讓線可以消失
  • 要用倒過來的迴圈
  • 加上即更改一些程式碼,畫出直線
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,ghostY=20;
void mouseDragged(){
  pt.add(new PVector(mouseX,mouseY));
}
void draw(){
  background(255);
  ellipse(200,200,15,15);
  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);
  }
  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--){
    pt.remove(i);
  }
}

Halloween:decide type1 dx 100 dy 100

  • 另存程式碼,更改它,讓線畫到一定長度後可以變顏色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.y);
  }
  if(dpx>100&&dpy<40)stroke(255,0,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--){
    pt.remove(i);
  }
}

Halloween:decide type2 dx 40 dy 100

  • 另存程式碼,更改它,讓線跟貓、鬼變黑色
  • 更改程式碼,讓畫出的橫線變紅色
  • 直線變藍色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.y);
  }
  if(dpx>100&&dpy<40)stroke(255,0,0);
  else if(dpx<40 && dpy>100)stroke(0,0,255);
  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--){
    pt.remove(i);
  }
}

Halloween:decide type3 plus plus

  • 另存程式碼,更改它,畫出斜線變綠色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.y);
    if(p2.x-p.x>0 && p2.y-p.y>0)rightdown++;
  }
  if(dpx>100&&dpy<40)stroke(255,0,0);
  else if(dpx<40 && dpy>100)stroke(0,0,255);
  else if(rightdown>30)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--){
    pt.remove(i);
  }
}

Halloween:decide type3 plus plus len

  • 另存程式碼,更改它,使執行後畫線更快變顏色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.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);
  else if(dpx<40 && dpy>100)stroke(0,0,255);
  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--){
    pt.remove(i);
  }
}

Halloween:decide type3 green part2

  • 另存程式碼,更改它,將綠色線改得更好
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector> ();
void setup(){
  size(400,400);
   background(255);
}
float ghostX=400,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;
  float  rightdown=0,rightup=0;
  boolean  green_part1=false,green_ok=false;
 /// if(mousePressed)  line(mouseX,mouseY,pmouseX,pmouseY);
 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);
   dpy+=abs(p2.y-p.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;
 }
     println (rightdown);
 if(dpx>100&&dpy<40)           stroke(255,0,0);
 else if(dpx<40  &&  dpy>100)  stroke(0,0,255);
 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--){
    pt.remove(i);
  }
}

Halloween:decide type3 green part2 rightup

  • 另存程式碼,更改它,讓倒勾勾變深綠色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.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);
  else if(dpx<40 && dpy>100)stroke(0,0,255);
  else if(green_ok)stroke(0,255,0);
  else if(green_part1)stroke(0,128,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--){
    pt.remove(i);
  }
}

Halloween:decide type4 yellow

  • 另存程式碼,更改它,讓倒勾勾變黃色
  • 程式碼
ArrayList<PVector> pt=new ArrayList<PVector>();
void setup(){
  size(400,400);
  background(255);
}
float ghostX=400,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;
  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);
    dpy+=abs(p2.y-p.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);
  else if(dpx<40 && dpy>100)stroke(0,0,255);
  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--){
    pt.remove(i);
  }
}

上傳程式碼

  • 將程式碼上傳至 github.com



沒有留言:

張貼留言