2023年10月26日 星期四

chotwo_week07

 

Week07 萬聖節遊戲

    week07_1_halloween_line


▼ 按滑鼠畫線;放開清畫面

void setup(){
  size(400,400);
  background(255);
}
void draw(){
  if(mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); // 畫線
}
void mouseReleased(){
  background(255);  // 放開滑鼠就把畫面清乾淨
}


    week07_2_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);  // 放開滑鼠就把畫面清乾淨
}


    week07_3_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);  // 放開滑鼠就把畫面清乾淨
}

 

    week07_4_halloween_ArrayList_mouseReleased_remove 


放開滑鼠,用for迴圈從資料結構最尾端開始移除

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(190,200, 15,15);
  ellipse(210,200, 15,15);
  //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);
    line(p.x, p.y, p2.x, p2.y); //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);  // 放開滑鼠就把畫面清乾淨
  for(int i=pt.size()-1; i>=0; i--){  //要用倒過來的loop
    pt.remove(i);
  }
}

 

    week07_5_halloween_decide_type1_dx_100_dy_10 

    week07_6_halloween_decide_type2_dx_100_dy_10 


▼ 畫橫線變紅色;畫豎線變藍色


    week07_7_halloween_decide_type3_plus_plus 


▼ 前半部分往右下,線變綠色
計算移動量的方式:rightdown++;


    week07_8_halloween_decide_type3_plus_plus_len


計算移動量的方式:if(滑鼠有被拖曳)rightdown+=一個點到上一個點的長度


    week07_9_halloween_decide_type3_green_part2


▼ 使用boolean函數判斷前半段是否往右下

    week07_A_halloween_decide_type3_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);
  fill(255);
  strokeWeight(2); // 鬼和主角的粗細
  stroke(0); // 鬼和主角不要被畫線影響顏色
  ellipse(ghostX, ghostY, 15,15);
  triangle(180, 190, 183, 175, 190, 185);
  triangle(210, 185, 216, 175, 220, 190);
  ellipse(200,200, 50,30); ellipse(190,200, 15,15); ellipse(210,200, 15,15);   line(195,210, 205, 210);   fill(0);   ellipse(190,200, 5,5); ellipse(210,200, 5,5);   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); //累積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);     // ellipse(p.x, p.y, 10, 10);     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); //ellipse(p.x, p.y, 3, 3);   }   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); // 放開滑鼠就把畫面清乾淨   for(int i=pt.size()-1; i>=0; i--){ //要用倒過來的loop     pt.remove(i);   } }

沒有留言:

張貼留言