aboutsummaryrefslogtreecommitdiff
blob: 408470822590aeb4487a05bf156e55b14d2af52a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
ArrayList particles;
int maxParticles = 255;

void setup() {
  size(window.innerWidth, window.innerHeight);
  particles = new ArrayList();
  rectMode(CENTER);
}

void draw() {
  background(21, 32, 46);

  if(particles.size() < maxParticles) {
    x = random(0, width);
    y = random(0, height);
    PVector l = new PVector(x, y);
    Particle particle = new Particle(l);
    particles.add(particle);
  }


  for(int i=0; i<particles.size(); i++) {
    Particle p = (Particle) particles.get(i);
    Particle next;

    if(i>1) {
      next = (Particle) particles.get(i-1);
      stroke(57,219,255,10);
      line(next.location.x, next.location.x, p.location.x, p.location.y)
    }


    // Run the particle
    p.run();

    if(p.isDead()) {
      particles.remove(i);
    }
  }
}

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float   lifespan;
  float   rectSize;

  Particle(PVector l) {
    location = l.get();
    velocity = new PVector(random(0,0.05),random(0.02, 0.8));
    acceleration = new PVector(random(0, 0.01),random(0,0.01));
    lifespan = 255.0;
    rectSize = 5;
  }

  void run() {
    display();
    update();
  }

  void display() {
    noStroke();
    fill(73, 219, 255, lifespan/5);
    rect(location.x, location.y, rectSize, rectSize);
  }

  void update() {
    lifespan -= 2;
    velocity.add(acceleration);
    location.add(velocity);
  }

  boolean isDead() {
    if (lifespan < 0) {
      return true;
    } else {
      return false;
    }
  }
}
Powered by cgit v1.2.3 (git 2.41.0)