Daily Creative Coding

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

スペース・トラベル

/**
* space travel
*
* @author aa_debdeb
* @date 2016/05/10
*/

ArrayList<Star> stars;
float speed;

float maxSpeed = 3.0;
float speedStep = 0.01;

void setup(){
  size(640, 480);
  stars = new ArrayList<Star>();
  for(int i = 0; i < 200; i++){
    stars.add(new Star());
  }
  speed = 0;
  background(0);
}

void draw(){
  fill(0, 30);
  noStroke();
  rect(0, 0, width, height);
  translate(width / 2, height / 2);
  noStroke();
  ArrayList<Star> nextStars = new ArrayList<Star>();
  for(Star star: stars){
    star.display();
    if(star.update()){
      nextStars.add(star);
    }
  }
  stars = nextStars;
  float num = random(map(speed, 0, maxSpeed, 0, 5));
  for(int i = 0; i < num; i++){
    stars.add(new Star());  
  }
  
  if(mousePressed){
    speed += speedStep;
  } else {
    speed -= speedStep;  
  }
  speed = constrain(speed, 0, maxSpeed);
}

class Star{
  
  float radious, radian, hue;
  
  Star(){
    radious = random(width / 2);
    radian = random(TWO_PI);
    hue = random(360);
  }
  
  void display(){
    colorMode(HSB, 360, 100, 100);
    fill(hue, 10, 80);
    ellipse(radious * cos(radian), radious * sin(radian), 1, 1);
  }
  
  boolean update(){
    radious += speed;
    float x = radious * cos(radian) + width / 2;
    float y = radious * sin(radian) + height / 2;
    if(0 <= x && x < width && 0 <= y && y < height){
      return true;
    } else {
      return false;
    }
  }
  
}