2021年11月22日 星期一

岩壁悲歌 Week07

 2021/11/22

-------------------------------------------------

(step01)▼▼▼▼▼▼▼▼▼▼▼



一顆50*50的球從(250,0)的位置往下掉,每次調3










一次掉2顆球球














利用for迴圈+陣列,一次掉4顆








在y要大於600時回彈,並且在y要小於0時再回彈

(step01)▲▲▲▲▲▲▲▲▲▲▲

(step02)▼▼▼▼▼▼▼▼▼▼▼








先在mouse的x,y建立一個紅色的球,當紅色與黃色的球

距離小於等於50,可以使黃色的球停止








新增boolean [] dead={false,false,false,false};

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

      dead[i]=true;

if(dead[i]==true)  continue;







利用上面程式碼了解boolean原理

(step02)▲▲▲▲▲▲▲▲▲▲▲

(step03)▼▼▼▼▼▼▼▼▼▼▼









讓球變多








球會依照y軸上下回彈,滑鼠只要一碰到就會消失


(step03)▲▲▲▲▲▲▲▲▲▲▲

(step04)▼▼▼▼▼▼▼▼▼▼▼













球會在500*500的大小間來回彈
在y = y+vy;之後加上vy += 0.98;會讓y有重力加速度









class Ball{

  float x, y, vx, vy;

  boolean dead;

  Ball(){

    reborn();

  }

  void reborn(){

    x = random(500);

    y = random(500);

    vy = random(-2,2);

    vx = random(-2,2);

    dead = false;//沒有死掉

  }

  void draw(){

    if(dead==true) return;

    fill(255,0,0); ellipse(x,y, 5,5);

    if( dist(mouseX,mouseY,x,y)<5 ){

      dead=true;

      gameOver=true;

    }

    x = x + vx*1;//速度*時間=距離

    y = y + vy*1;

    if(y>500 || y<0) reborn();

    if(x>500 || x<0) reborn();

  }

}

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);

}


沒有留言:

張貼留言