input:
ParticleSystem ps; //粒子系统,添加彩色和鼠标移动功能
void setup(){
size(960,540);
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw(){
background(255);
ps.addParticle(mouseX,mouseY); //鼠标调整粒子系统
ps.run();
}
input(Particle粒子类):
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan; //时间周期或生命周期,即延迟时间
color particleColor = color(random(255), random(255), random(255), 95);
Particle(float x,float y){
location = new PVector(x,y);
velocity = new PVector(random(-1,1),random(-2,0));
acceleration = new PVector(0,0.05);
lifespan = 400.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
stroke(particleColor, lifespan);
fill(particleColor, lifespan);
ellipse(location.x,location.y,8,8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
input(ParticleSystem粒子系统类):
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector location) {
origin = location.copy();
particles = new ArrayList<Particle>();
}
void addParticle(float x,float y) {
particles.add(new Particle(x,y));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
output:
欢快的彩虹花火
参考视频链接:【欢快的彩虹花火】 https://www.bilibili.com/video/BV1sm4y147pC/?share_source=copy_web&vd_source=81f6714046d21d930e6f52a77c0d8811