LoginSignup
11
9

More than 5 years have passed since last update.

心臓の鼓動っぽいモーション

Last updated at Posted at 2015-03-24

こちらのコードはJavaになりますが、
JS仕様にしてjsdo.itで公開しました。
http://jsdo.it/hp0me_/uBj3

ゲームプログラミングの息抜きにProcessingというのも乙なもの(?)です。
Javaに慣れてない方は、練習にもなります。

実行イメージ (gif動画)

gifじゃ伝わりにくいですが・・・
sketch16.gif

コードのイメージ

フレームが0から20くらいまであるとします。
そのうち17から20フレまではスピードを急激に速くするとこんな感じになります。

コード

sketch_150324a.pde
//血の粒の数
int cellNumber = 350;

//通常スピードと速いスピード
int speed = 1;
int speedFast = 5;

//通常フレームとどこから速くなるか
int frameMax = 20;
int frameFast = 17;

//粒の大きさにバリエーションをつける
int minRad = 10;
int maxRad = 90;

BloodCell[] _cellArr = {};

void setup(){
  size(800,800);
  background(255);
  smooth();
  generateCells();
}

void draw(){
  background(255);
  for(int i=0; i<_cellArr.length; i++){
    BloodCell cell = _cellArr[i];
    cell.update();
  }
}

void generateCells(){
  //粒を生成
  for(int i=0; i<cellNumber; i++){
    BloodCell cell = new BloodCell();
    cell.genarate();
    _cellArr = (BloodCell[])append(_cellArr,cell);
  }
}

//血の粒をクラス化
class BloodCell {
  float x,y,vx,vy,rad;
  int counter;

  BloodCell(){
    x = random(width);
    y = random(height);
    rad = random(minRad,maxRad);
    vx = random(-speed,speed);
    vy = random(-speed,speed);
  }

  void genarate(){
    noStroke();
    fill(203,51,51,120);
    ellipse(x,y,rad*2,rad*2);
  }

  void update()
  {
    //遅い移動
    if(counter < frameFast){
      vx = random(-speed,speed);
      vy = random(-speed,speed);
    //速い移動
    }else{
      vx = random(-speedFast,speedFast);
      vy = random(-speedFast,speedFast);
    }
    counter++;
    if(counter == frameMax)counter = 0;

    x += vx;
    y += vy;

    //フレーム描画
    genarate();
  }

}

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9