Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

Surfacing Bubbles

/**
* Surfacing Bubbles
*
* @author aa_debdeb
* @date 2015/09/04
*/


float BG_GRA_STEP_SIZE = 5.0;
float MAX_BUBBLE_RADIOUS = 20.0;
float MAX_BUBBLE_X_SPEED = 3.0;
float BUBBLE_Y_SPEED = 5.0;
float NEW_BUBBLE_RATE = 0.7;

ArrayList<Bubble> bubbles;

void setup(){
  size(300, 700);
  smooth();
  frameRate(40);
  
  bubbles = new ArrayList<Bubble>();
  for(int i = 0; i < 10; i++){
    bubbles.add(new Bubble());
  }
}

void draw(){
  drawBackground();
  for(Bubble bubble: bubbles){
    bubble.draw();
  }
  for(Bubble bubble: bubbles){
    bubble.update();
  }
  
  ArrayList<Integer> removalBubbleIdxes = new ArrayList<Integer>();
  for(int i = 0; i < bubbles.size(); i++){
    if(bubbles.get(i).isReachedSurface()){
      removalBubbleIdxes.add(i);
    }
  }
  for(int i: removalBubbleIdxes){
    bubbles.remove(i);
  }
    
  if(random(1) < NEW_BUBBLE_RATE){
    bubbles.add(new Bubble());
  }
}

void drawBackground(){
  background(255);
  float h = 0;
  while(h < height){
    noStroke();
    fill(0, 0, 200, 255 * ((float)h / height));
    rect(0, h, width, BG_GRA_STEP_SIZE);
    h += BG_GRA_STEP_SIZE;
  }
}

class Bubble{
  
  float x;
  float y;
  float radious;
  
  Bubble(){
    radious = random(MAX_BUBBLE_RADIOUS);
    x = width / 2.0;
    y = height + radious;
  }
  
  void draw(){
    stroke(255);
    strokeWeight(1);
    fill(255, 40);
    arc(x, y, radious * 2.0, radious * 2.0, 0, 2.0 * PI);
  }
  
  void update(){
    x += random(MAX_BUBBLE_X_SPEED * 2.0) - MAX_BUBBLE_X_SPEED;
    y -= BUBBLE_Y_SPEED;
  }
  
  boolean isReachedSurface(){
     if(y <= 0){
       return true;
     } else {
       return false;
     }
  }
  
  
}