2021年11月22日 星期一

week07

 要開始準備期末作品了

要做掉落的金幣

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

void setup(){

  size(500,600);

}

void draw(){

  background(255);

  ellipse(x,y,50,50);

  y = y + vy;

}

那球就是金幣

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

float x2 = 150,y2 = 50, vx2 = 0, vy2 = 2;

void setup(){

  size(500,600);

}

void draw(){

  background(255);

  ellipse(x,y,50,50);

  ellipse(x2,y2,50,50);

  y = y + vy;

  y2 = y2 + vy2;

}

不同位置與速度的球

接著利用陣列跟迴圈畫球

float []x = {250,150,350,300};

float []y = {0,50,80,100};

float []vx = {0,0,0,0};

float []vy = {3,2,2.5,4};

void setup(){

  size(500,600);

}

void draw(){

  background(255);

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

    ellipse(x[i],y[i],50,50);

    y[i] = y[i] + vy[i];

  }

}


讓球上下彈

float []x = {250,150,350,300};

float []y = {0,50,80,100};

float []vx = {0,0,0,0};

float []vy = {3,2,2.5,4};

void setup(){

  size(500,600);

}

void draw(){

  background(255);

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

    ellipse(x[i],y[i],50,50);

    y[i] = y[i] + vy[i];

    if(y[i] > 600) vy[i] = -vy[i];

    if(y[i] < 0) vy[i] = -vy[i];

  }

}



把球上黃色,再做一個碰到就會停的紅球

float []x = {250,150,350,300};

float []y = {0,50,80,100};

float []vx = {0,0,0,0};

float []vy = {3,2,2.5,4};

void setup(){

  size(500,600);

}

void draw(){

  background(255);

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

    fill(255,255,0);ellipse(x[i],y[i],50,50);

    if(dist(mouseX,mouseY,x[i],y[i])<=50){

      continue;

    }

    y[i] = y[i] + vy[i];

    if(y[i] > 600) vy[i] = -vy[i];

    if(y[i] < 0) vy[i] = -vy[i];

  }

  fill(255,0,0);ellipse(mouseX,mouseY,50,50);

}

做一個隨機生球,碰到消失的程式
class Ball{
  float x,y,vx,vy;
  boolean dead;
  Ball(){
    x = random(500);
    y = random(100,200);
    vy = random(3,4);
    dead = false;
  }
  void draw(){
    if(dead == true) return;
    ellipse(x,y,50,50);
    if(dist(mouseX,mouseY,x,y) < 50){
      dead = true;
    }
    y = y + vy;
    if(y > 500) vy = -vy;
    if(y < 0) vy = -vy;
  }
}
Ball [] balls;
void setup(){
  size(500,500);
  balls = new Ball[20];
  for(int i=0; i<20; i++){
    balls[i] = new Ball();
  }
}
void draw(){
  background(255);
  for(int i=0; i<20; i++){
    balls[i].draw();
  }
}

讓球有更多變化
class Ball{
  float x,y,vx,vy;
  boolean dead;
  Ball(){
    x = random(500);
    y = random(500);
    vy = random(-4,4);//這裡
    vx = random(-4,4);//這裡
    dead = false;
  }
  void draw(){
    if(dead == true) return;
    ellipse(x,y,50,50);
    if(dist(mouseX,mouseY,x,y) < 50){
      dead = true;
    }
    x = x + vx;//這裡
    y = y + vy;
    if(y > 500 || y < 0) vy = -vy;//這裡
    if(x > 500 || x < 0) vx = -vx;//這裡
  }
}
Ball [] balls;
void setup(){
  size(500,500);
  balls = new Ball[20];
  for(int i=0; i<20; i++){
    balls[i] = new Ball();
  }
}
void draw(){
  background(255);
  for(int i=0; i<20; i++){
    balls[i].draw();
  }
}

改造成用滑鼠躲球的遊戲
class Ball{
  float x,y,vx,vy;
  boolean dead;
  Ball(){
    rebron();
  }
  void rebron(){
    x = random(500);
    y = random(500);
    vy = random(-2,2);
    vx = random(-2,2);
    dead = false;
  }
  void draw(){
    if(dead == true) return;
    ellipse(x,y,50,50);
    if(dist(mouseX,mouseY,x,y) < 5){
      dead = true;
      gameOver = true;
    }
    x = x + vx*1;
    y = y + vy*1;
    vy += 0.98;//重力
    if(y > 500 || y < 0) vy = -vy;
    if(x > 500 || x < 0) vx = -vx;
  }
}
Ball [] balls;
void setup(){
  size(500,500);
  balls = new Ball[99];
  for(int i = 0;i < 99;i++){
    balls[i] = new Ball();
  }
}
boolean gameOver = false;
void draw(){
  background(0);
  for(int i = 0;i < 99;i++){
    balls[i].draw();
  }
  if(gameOver) background(255,0,0);
}


沒有留言:

張貼留言