2024年10月21日 星期一

week07

week07

 week07_01_arras01_backgound_fill_stroke_ellipse_rect_line



1.做出模樣


程式碼

///week07_01_arras01_backgound_fill_stroke_ellipse_rect_line

void setup(){

  size(500,400);

}

float x=250,y=200;

void draw(){

  background(215,184,200);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(240,160,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(x,y,55,55);

  

  line(x,y,mouseX,mouseY);

}


week07_02_arras02_atan_sin_cos



1.用tan算出角度,做出凸凸


程式碼

///week07_01_arras02_atan_sin_cos
void setup(){
  size(500,400);
}
float x=250,y=200;
void draw(){
  background(215,184,200);
  float dx=mouseX - x,dy = mouseY - y;
  float a = atan2(dy,dx);
  strokeWeight(20);
  line(x,y,x+cos(a)*40,y+sin(a)*40);

  
  fill(167,167,175);
  stroke(110,110,113);
  strokeWeight(4);
  rect(240,160,20,20);
  
  fill(204,102,156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(x,y,55,55);
  //line(x,y,mouseX,mouseY);
}


week07_03_arras03_atan_angle_rotate_translate



1.函式依照參數宣告


2.旋轉對中心旋轉所以ellipse座標是00


3.rect調整>圖一>圖二

程式碼

///week07_03_arras03_atan_angle_rotate_translate
void setup(){
  size(500,400);
}
float x=250,y=200;
void draw(){
  background(215,184,200);
  float dx=mouseX - x,dy = mouseY - y;
  float a = atan2(dy,dx);
  line(x,y,mouseX,mouseY);
  mytank(x,y,a);
}
void mytank(float x,float y,float a) {
  translate(x,y);
  rotate(a);

  fill(167,167,175);
  stroke(110,110,113);
  strokeWeight(4);
  rect(20,-10,20,20);
  
  fill(204,102,156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(0,0,55,55);
}


week07_04_arras04_background_for_line_line_void_keyPressed_keycode







1.上下左右

2. 下次中心不動


程式碼

///week07_04_arras04_background_for_line_line_void_keyPressed_keycode
void setup(){
  size(500,400);
}
void keyPressed(){
  if(keyCode==RIGHT) x+=1;
  if(keyCode==LEFT) x-=1;
  if(keyCode==UP) y-=1;
  if(keyCode==DOWN) y+=1;
}

float x=250,y=200;
void draw(){
  background(219);
  stroke(210);
  strokeWeight(1);
  for(int i=0;i<30;i++) {
    line(0,i*30,500,i*30);
    line(i*30,0,i*30,500);
  }
  float dx=mouseX - x,dy = mouseY - y;
  float a = atan2(dy,dx);
  line(x,y,mouseX,mouseY);
  mytank(x,y,a);
}
void mytank(float x,float y,float a) {
  translate(x,y);
  rotate(a);
  fill(167,167,175);
  stroke(110,110,113);
  strokeWeight(4);
  rect(20,-10,20,20);
  
  fill(204,102,156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(0,0,55,55);
}



week07_05_arras05_correct_move_translate_mx_my




1.背景移動,本體在螢幕中間


程式碼


///week07_05_arras05_correct_move_translate_mx_my

void setup(){

  size(500,400);

}

void keyPressed(){

  if(keyCode==RIGHT) x+=1;

  if(keyCode==LEFT) x-=1;

  if(keyCode==UP) y-=1;

  if(keyCode==DOWN) y+=1;

}

float x=250,y=200;

void draw(){

  background(219);

  translate(-x,-y);

  translate(width/2,height/2);

  float mx = mouseX+x-width/2,my = mouseY+y-height/2;

  ellipse(mx,my,8,8);

  stroke(210);

  strokeWeight(1);

  for(int i=0;i<30;i++) {

    line(0,i*30,500,i*30);

    line(i*30,0,i*30,500);

  }

  float dx=mx-x,dy = my-y;

  float a = atan2(dy,dx);

  line(x,y,mx,my);

  mytank(x,y,a);

}

void mytank(float x,float y,float a) {

  translate(x,y);

  rotate(a);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(20,-10,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(0,0,55,55);

}


week07_06_arras06_vx_vy_keyPressed_void_keyReleased_void_draw



1.想辦法提高移動速度,keypressed每秒次數加快



程式碼


///week07_06_arras06_vx_vy_keyPressed_void_keyReleased_void_draw

void setup(){

  size(500,400);

}

void keyPressed(){

  if(keyCode==RIGHT) vx= 1;

  if(keyCode==LEFT) vx= -1;

  if(keyCode==UP) vy= -1;

  if(keyCode==DOWN) vy= 1;

}

void keyReleased(){

  if(keyCode==LEFT || keyCode==RIGHT) vx=0;

  if(keyCode==UP || keyCode==DOWN) vy=0;

}

float x=250,y=200,vx=0,vy=0;

void draw(){

  x += vx;

  y += vy;

  background(219);

  translate(-x,-y);

  translate(width/2,height/2);

  float mx = mouseX+x-width/2,my = mouseY+y-height/2;

  ellipse(mx,my,8,8);

  stroke(210);

  strokeWeight(1);

  for(int i=0;i<30;i++) {

    line(0,i*30,500,i*30);

    line(i*30,0,i*30,500);

  }

  float dx=mx-x,dy = my-y;

  float a = atan2(dy,dx);

  line(x,y,mx,my);

  mytank(x,y,a);

}

void mytank(float x,float y,float a) {

  translate(x,y);

  rotate(a);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(20,-10,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(0,0,55,55);

}


week07_07_arras07_angle_bulletX_Y_VX_VY




1.射出子彈


程式碼


///week07_07_arras07_angle_bulletX_Y_VX_VY
void setup(){
  size(500,400);
}
float angle,bulletX=0,bulletY=0,bulletVX=0,bulletVY=0;
void mousePressed(){
  bulletX=x;
  bulletY=y;
  bulletVX = cos(angle);
  bulletVY = sin(angle);
}

void keyPressed(){
  if(keyCode==RIGHT) vx= 1;
  if(keyCode==LEFT) vx= -1;
  if(keyCode==UP) vy= -1;
  if(keyCode==DOWN) vy= 1;
}
void keyReleased(){
  if(keyCode==LEFT || keyCode==RIGHT) vx=0;
  if(keyCode==UP || keyCode==DOWN) vy=0;
}
float x=250,y=200,vx=0,vy=0;
void draw(){
  x += vx;
  y += vy;
  background(219);
  translate(-x,-y);
  translate(width/2,height/2);
  float mx = mouseX+x-width/2,my = mouseY+y-height/2;
  ellipse(mx,my,8,8);
  stroke(210);
  strokeWeight(1);
  for(int i=0;i<30;i++) {
    line(0,i*30,500,i*30);
    line(i*30,0,i*30,500);
  }
  float dx=mx-x,dy = my-y;
  float a = atan2(dy,dx);
  angle = a;
  line(x,y,mx,my);
  pushMatrix();
  mytank(x,y,a);
  popMatrix();
  if(bulletX!=0 && bulletY!=0) {
    bulletX += bulletVX;
    bulletY += bulletVY;
    ellipse(bulletX,bulletY,20,20);
  }

}
void mytank(float x,float y,float a) {
  translate(x,y);
  rotate(a);
  fill(167,167,175);
  stroke(110,110,113);
  strokeWeight(4);
  rect(20,-10,20,20);
  
  fill(204,102,156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(0,0,55,55);
}


week07_08_arras08_array_many_bullet




1.用陣列解決只有一顆子彈的問題

2.只有100個子彈


程式碼


///week07_08_arras08_array_many_bullet

void setup(){

  size(500,400);

}

float angle;

int bulletN = 0;

float [] bulletX=new float[100];

float [] bulletY=new float[100];

float [] bulletVX=new float[100];

float [] bulletVY=new float[100];

void mousePressed(){

  int i = bulletN;

  bulletX[i]=x;

  bulletY[i]=y;

  bulletVX[i] = cos(angle);

  bulletVY[i] = sin(angle);

  bulletN++;

}


void keyPressed(){

  if(keyCode==RIGHT) vx= 1;

  if(keyCode==LEFT) vx= -1;

  if(keyCode==UP) vy= -1;

  if(keyCode==DOWN) vy= 1;

}

void keyReleased(){

  if(keyCode==LEFT || keyCode==RIGHT) vx=0;

  if(keyCode==UP || keyCode==DOWN) vy=0;

}

float x=250,y=200,vx=0,vy=0;

void draw(){

  x += vx;

  y += vy;

  background(219);

  translate(-x,-y);

  translate(width/2,height/2);

  float mx = mouseX+x-width/2,my = mouseY+y-height/2;

  ellipse(mx,my,8,8);

  stroke(210);

  strokeWeight(1);

  for(int i=0;i<30;i++) {

    line(0,i*30,500,i*30);

    line(i*30,0,i*30,500);

  }

  float dx=mx-x,dy = my-y;

  float a = atan2(dy,dx);

  angle = a;

  line(x,y,mx,my);

  pushMatrix();

  mytank(x,y,a);

  popMatrix();

  for(int i=0;i<bulletN;i++) {

    bulletX[i] += bulletVX[i];

    bulletY[i] += bulletVY[i];

    ellipse(bulletX[i],bulletY[i],20,20);

  }

}

void mytank(float x,float y,float a) {

  translate(x,y);

  rotate(a);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(20,-10,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(0,0,55,55);

}

week07_09_arras09_recycle_bullet_move




1.解決子彈上限問題


程式碼


///week07_09_arras09_recycle_bullet_move

void setup(){

  size(500,400);

}

float angle;

int bulletN = 0;

float [] bulletX=new float[100];

float [] bulletY=new float[100];

float [] bulletVX=new float[100];

float [] bulletVY=new float[100];

void mousePressed(){

  int i = bulletN;

  bulletX[i]=x;

  bulletY[i]=y;

  bulletVX[i] = cos(angle);

  bulletVY[i] = sin(angle);

  bulletN++;

  

  if(bulletN==100) {

    for(int k=0;k<50;k++) {

      bulletX[k]=bulletX[k+50];

      bulletY[k]=bulletY[k+50];

      bulletVX[k] = bulletVX[k+50];

      bulletVY[k] = bulletVY[k+50];

    }

    bulletN = 50;

  }

  println(bulletN);

}


void keyPressed(){

  if(keyCode==RIGHT) vx= 1;

  if(keyCode==LEFT) vx= -1;

  if(keyCode==UP) vy= -1;

  if(keyCode==DOWN) vy= 1;

}

void keyReleased(){

  if(keyCode==LEFT || keyCode==RIGHT) vx=0;

  if(keyCode==UP || keyCode==DOWN) vy=0;

}

float x=250,y=200,vx=0,vy=0;

void draw(){

  x += vx;

  y += vy;

  background(219);

  translate(-x,-y);

  translate(width/2,height/2);

  float mx = mouseX+x-width/2,my = mouseY+y-height/2;

  ellipse(mx,my,8,8);

  stroke(210);

  strokeWeight(1);

  for(int i=0;i<30;i++) {

    line(0,i*30,500,i*30);

    line(i*30,0,i*30,500);

  }

  float dx=mx-x,dy = my-y;

  float a = atan2(dy,dx);

  angle = a;

  line(x,y,mx,my);

  pushMatrix();

  mytank(x,y,a);

  popMatrix();

  for(int i=0;i<bulletN;i++) {

    bulletX[i] += bulletVX[i];

    bulletY[i] += bulletVY[i];

    ellipse(bulletX[i],bulletY[i],20,20);

  }

}

void mytank(float x,float y,float a) {

  translate(x,y);

  rotate(a);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(20,-10,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(0,0,55,55);

}



week07_10_arras010_bullet_for_height_die




1.做出子彈的壽命


程式碼


///week07_10_arras010_bullet_for_height_die

void setup(){

  size(500,400);

}

float angle;

int bulletN = 0;

float [] bulletX=new float[100];

float [] bulletY=new float[100];

float [] bulletVX=new float[100];

float [] bulletVY=new float[100];

float [] bulletT=new float[100];


void mousePressed(){

  int i = bulletN;

  bulletX[i]=x;

  bulletY[i]=y;

  bulletVX[i] = cos(angle);

  bulletVY[i] = sin(angle);

  bulletT[i] = 600;

  bulletN++;

  

  if(bulletN==100) {

    for(int k=0;k<50;k++) {

      bulletX[k]=bulletX[k+50];

      bulletY[k]=bulletY[k+50];

      bulletVX[k] = bulletVX[k+50];

      bulletVY[k] = bulletVY[k+50];

    }

    bulletN = 50;

  }

  println(bulletN);

}


void keyPressed(){

  if(keyCode==RIGHT) vx= 1;

  if(keyCode==LEFT) vx= -1;

  if(keyCode==UP) vy= -1;

  if(keyCode==DOWN) vy= 1;

}

void keyReleased(){

  if(keyCode==LEFT || keyCode==RIGHT) vx=0;

  if(keyCode==UP || keyCode==DOWN) vy=0;

}

float x=250,y=200,vx=0,vy=0;

void draw(){

  x += vx;

  y += vy;

  background(219);

  translate(-x,-y);

  translate(width/2,height/2);

  float mx = mouseX+x-width/2,my = mouseY+y-height/2;

  ellipse(mx,my,8,8);

  stroke(210);

  strokeWeight(1);

  for(int i=0;i<30;i++) {

    line(0,i*30,500,i*30);

    line(i*30,0,i*30,500);

  }

  float dx=mx-x,dy = my-y;

  float a = atan2(dy,dx);

  angle = a;

  line(x,y,mx,my);

  pushMatrix();

  mytank(x,y,a);

  popMatrix();

  for(int i=0;i<bulletN;i++) {

    bulletX[i] += bulletVX[i];

    bulletY[i] += bulletVY[i];

    ellipse(bulletX[i],bulletY[i],20,20);

    bulletT[i] --;

    if(bulletT[i]==0) {

      for(int k=i+1;k<bulletN;k++) {

        bulletX[k-1] = bulletX[k];

        bulletY[k-1] = bulletY[k];

        bulletVX[k-1] = bulletVX[k];

        bulletVY[k-1] = bulletVY[k];

        bulletT[k-1] = bulletT[k];

      }

      bulletN --;

    }

  }

  println(bulletN);

}

void mytank(float x,float y,float a) {

  translate(x,y);

  rotate(a);

  fill(167,167,175);

  stroke(110,110,113);

  strokeWeight(4);

  rect(20,-10,20,20);

  

  fill(204,102,156);

  stroke(125,84,105);

  strokeWeight(3);

  ellipse(0,0,55,55);

}



沒有留言:

張貼留言