// it falls! class Flake { float x, y; float vx; int t; float depth; Flake(float xpos, float ypos, int type) { x = xpos; y = ypos; t = type; vx = 0; depth = random(5); } } class Snow { //PGraphics bg; PImage[] types; int w, h; Flake[] flakes; int newest; Snow(int wi, int hi) { w = wi; h = hi; types = new PImage[2]; types[0] = managedRequestImage("snow_1.gif"); types[1] = managedRequestImage("snow_2.gif"); //bg = createGraphics(w, h, P2D); flakes = new Flake[30]; newest = 0; } void Draw(float xpos, float ypos, int s) { int rand = int(random(w*s)); if (flakes[newest] == null) { if (rand % 50 == 0) { int type = 0; if (random(1) > .8) type = 1; flakes[newest] = new Flake(rand, 0, type); newest = (newest + 1) % flakes.length; } } //image(bg, xpos, ypos, bg.width*s, bg.height*s); for (int i = 0; i < flakes.length; i++) { rand = int(random(w)); if (flakes[i] != null) { if (random(1) > .5) { flakes[i].y += rand % 2; flakes[i].vx += (rand % 3) - 1; if (flakes[i].vx > 5) flakes[i].vx = 5; if (flakes[i].vx < -5) flakes[i].vx = -5; flakes[i].x += flakes[i].vx * .2; if (flakes[i].x < 3*s) flakes[i].x = 3*s; if (flakes[i].x > (w-3)*s) flakes[i].x = (w-3)*s; } PImage type = types[flakes[i].t]; image(type, xpos + flakes[i].x, ypos + flakes[i].y, type.width*s, type.height*s); if (flakes[i].y > (h - 10 + flakes[i].depth)*s) { //bg.beginDraw(); //bg.image(type, flakes[i].x/s, flakes[i].y/s); //bg.endDraw(); flakes[i] = null; } } } } }