概要
Processingで雪を降らせる方法をメモ.
奥行きを出すため、サイズと速さが連動しています(スピードが遅い雪は小さい).
コード
class Snowflake { //Snowflake Class
float x; //x axis
float y; //y axis
float s; //size
color c; //color
float dy; //speed
Snowflake() { //Function to create snowflakes
x = random(width); //initial x point is random
y = random(height);
dy = random(0, 3); // Falling speed is random between 3 and 10
s = 5 * dy / 4.0; //Size is small if initial speed is slow
c = #ffffff; //white snow
}
void drop() { //create snow and move
y += dy;
if (y > height) y = 0; //if snow gets the bottom of the screen, return to top
noStroke(); //no outline
fill(c);
ellipse(x, y, s, s);
}
}
Snowflake[] sf = new Snowflake[5000]; //prepare the area for 1000 snow object
void setup(){
size (600,600);
noCursor();
for (int i = 0; i < 2000; i++) { //雪⽚オブジェクトを1000 個作る
sf[i] = new Snowflake();
}
}
void draw() {
background(12,15,32);
for(int i = 0; i < 2000; i++) {
sf[i].drop();
}
}