2024年10月21日 星期一

Week07 發射與滑行

本周內容: 旋轉移動(arras.io),雙軸控制(LOLM,荒野)



07-1(繪製)

//week_07_01_arras01_draw
void setup() {
  //background(220, 220, 220);
  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(215, 102, 156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(x, y, 55, 55);
  
  line(x,y,mouseX,mouseY);
}

07-2(atan)

//week_07_02_arras02_draw_atan_angle
void setup() {
  //background(220, 220, 220);
  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(215, 102, 156);
  stroke(125,84,105);
  strokeWeight(3);
  ellipse(x, y, 55, 55);
  
  line(x,y,mouseX,mouseY);
}

07-3(tower flow)

//week_07_03_arras03_atan_rotate_myTank
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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}


07-4(移動與網格)

//week_07_04_arras04_backgroung_line_line_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(215, 184, 200);
  background(219);
  stroke(196);
  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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}



07-5(中心視角移動)

//week_07_05_arras05_correct_move_translate_mxmy
void setup() {
  size(500, 400);
}
void keyPressed(){
  if(keyCode==RIGHT) x += 3; 
  if(keyCode==LEFT) x -= 3; 
  if(keyCode==UP) y -= 3; 
  if(keyCode==DOWN) y += 3; 
}
float x = 250, y = 200;
void draw() {
  //background(215, 184, 200);
  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(196);
  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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}

07-6(斜向移動)

//week_07_06_arras06_vxvy_void_keyPressed_keyReleased
void setup() {
  size(500, 400);
}
void keyPressed(){
  if(keyCode==RIGHT) vx =spd;
  if(keyCode==LEFT)  vx =-spd;
  if(keyCode==UP)  vy=-spd;
  if(keyCode==DOWN)  vy=spd;
}
float spd = 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(215, 184, 200);
  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(196);
  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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}


07-7(發射(彈容一顆))

//week_07_07_arras07_bullet
void setup() {
  size(500, 400);
}
void keyPressed(){
  if(keyCode==RIGHT) vx =spd;
  if(keyCode==LEFT)  vx =-spd;
  if(keyCode==UP)  vy=-spd;
  if(keyCode==DOWN)  vy=spd;
}
float spd = 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(215, 184, 200);
  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(196);
  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);
  angle = a;
  pushMatrix();
  myTank(x, y, a);
  popMatrix();
  if(bulletX!=0 && bulletY!=0){
    bulletX += bulletVX;
    bulletY += bulletVY;
    ellipse(bulletX,bulletY,20,20);
    }
}
float angle,bulletX = 0,bulletY = 0,bulletVX = 0,bulletVY = 0;
void mousePressed(){
    bulletX += x;
    bulletY = y;
    bulletVX = cos(angle);
    bulletVY = sin(angle);
}
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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}


07-8(發射 array100)

//week_07_08_arras08_bullets_array
void setup() {
  size(500, 400);
}
void keyPressed() {
  if (keyCode==RIGHT) vx =spd;
  if (keyCode==LEFT)  vx =-spd;
  if (keyCode==UP)  vy=-spd;
  if (keyCode==DOWN)  vy=spd;
}
float spd = 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(215, 184, 200);
  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(196);
  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);
  angle = a;
  pushMatrix();
  myTank(x, y, a);
  popMatrix();
  for (int i=0; i<100; i++) {
    bulletX[i] += bulletVX[i];
    bulletY[i] += bulletVY[i];
    ellipse(bulletX[i], bulletY[i], 20, 20);
  }
}
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 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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}

07-9(發射 回收array100)

//week_07_09_arras09_bullets_array_debug
void setup() {
  size(500, 400);
}
void keyPressed() {
  if (keyCode==RIGHT) vx =spd;
  if (keyCode==LEFT)  vx =-spd;
  if (keyCode==UP)  vy=-spd;
  if (keyCode==DOWN)  vy=spd;
}
float spd = 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(215, 184, 200);
  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(196);
  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);
  angle = a;
  pushMatrix();
  myTank(x, y, a);
  popMatrix();
  for (int i=0; i<100; i++) {
    bulletX[i] += bulletVX[i];
    bulletY[i] += bulletVY[i];
    ellipse(bulletX[i], bulletY[i], 20, 20);
  }
}
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 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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}



07-10(發射 回收timer)

//week_07_10_arras10_bullets_timmer_debug
void setup() {
  size(500, 400);
}
void keyPressed(){
  if(keyCode==RIGHT) vx =spd;
  if(keyCode==LEFT)  vx =-spd;
  if(keyCode==UP)  vy=-spd;
  if(keyCode==DOWN)  vy=spd;
}
float spd = 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(215, 184, 200);
  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(196);
  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);
  angle = a;
  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);
}
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];
int [] bulletT = new int[100];
void mousePressed(){
  int i = bulletN;
    bulletX[i] = x;
    bulletY[i] = y;
    bulletVX[i] = cos(angle);
    bulletVY[i] = sin(angle);
    bulletT[i] = 600;
    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(215, 102, 156);
  stroke(125, 84, 105);
  strokeWeight(3);
  ellipse(0, 0, 55, 55);
}





沒有留言:

張貼留言