美文网首页
Particle system_useArrayList &am

Particle system_useArrayList &am

作者: 轻荷 | 来源:发表于2015-11-13 22:33 被阅读35次
ANIMATION
import java.awt.Rectangle;

ArrayList particles;
Rectangle blackhole;

void setup(){
  size(600,400);
  pixelDensity(2);
  particles=new ArrayList();
  blackhole=new Rectangle(60,height-100,width-120,30);
}

void draw(){
  background(255);
  //Create a new particle once a circle
  particles.add(new Particle(mouseX,mouseY));
  
  fill(200);
  rect(blackhole.x, blackhole.y,blackhole.width,blackhole.height);
  
  for(int i=0;i<particles.size();i++){
    Particle p=(Particle)particles.get(i);
    p.display();
    if(!blackhole.contains(p.x,p.y)){
      p.move();
    }
  }
  
  if(particles.size()>1000){
    particles.remove(0);
  }
  
  saveFrame("frames/Particle####.jpg");
}

class Particle {
  float x, y, r;
  float c;
  float spx,spy;
  
  Particle(float x_, float y_) {
    x=x_;
    y=y_;
    r=15;
    c=random(10,50);
    spx=random(-1,1);
    spy=random(-4,0);
  }

  void display() {
    fill(255,c*5,c, c);
    noStroke();
    ellipse(x, y, r*2, r*2);
  }

  void move() {
    if (y<height-r) {
      x+=spx;
      y+=spy;
      spy+=0.1;
    }
  }
}

相关文章

网友评论

      本文标题:Particle system_useArrayList &am

      本文链接:https://www.haomeiwen.com/subject/roqkhttx.html