有点类似烟花的效果
鼠标点击的地方出现彩色的粒子
Particle.pde
public class Particle {
//
PVector location;
PVector acceleration;
PVector velocity;
float lifespan = 255;
float vr = 6f;
int r,g,b;
public Particle(PVector l) {
location = l;
acceleration = new PVector(0, random(0.1,0.5));
velocity = new PVector(random(-vr, vr), random(-vr, vr));
r = (int)random(0,255);
g = (int)random(0,255);
b = (int)random(0,255);
}
public Particle() {
location = new PVector(width/2,height/2);
acceleration = new PVector(0, random(0.1,0.5));
velocity = new PVector(random(-5, 5), random(-5, 5));
}
// draw the particle
void display() {
noStroke();
fill(r, g, 255, lifespan);
float size = random(5,15);
ellipse(location.x, location.y, size, size);
}
// update the position
void update() {
location.add(velocity);
velocity.add(acceleration);
// the lifespan is diminishing
}
// Check to see if it's dead
boolean isDead() {
if (lifespan < 0f) {
return true;
} else {
return false;
}
}
// make the point run
void run() {
display();
update();
lifespan-= 10;
}
}
ParticleTest.pde
// 练习:模拟单个粒子的运动 //<>// //<>// //<>//
import java.util.Iterator;
Iterator<Particle> it;
int total = 100;
ArrayList<Particle> particles = new ArrayList<Particle>();
void setup() {
size(800, 800);
}
void draw() {
background(0);
it = particles.iterator();
while(it.hasNext()) {
Particle p = it.next();
p.run();
if (p.isDead()) {
it.remove();
}
}
}
void mouseClicked() {
for (int i = 0; i < total; i++) {
PVector location = new PVector(mouseX,mouseY);
particles.add(new Particle(location));
}
}