Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Created February 19, 2014 21:22
Show Gist options
  • Save ArcticLight/9101880 to your computer and use it in GitHub Desktop.
Save ArcticLight/9101880 to your computer and use it in GitHub Desktop.
Because Why Not Particle Effects?
abstract class Shape {
PVector p,v,g;
float r,rv;
int radius;
color fc, sc;
public Shape() {
this.fc = color((int)random(0,256),(int)random(0,256),(int)random(0,256),(int)random(0,256));
this.sc = color((int)random(0,256),(int)random(0,256),(int)random(0,256),(int)random(100,256));
//this.p = new PVector(width/2 + random(-width/8,width/8), height/2 + random(-height/8,height/8));
this.p = new PVector(random(0,width), -30);
this.v = new PVector(random(-1,1),random(-2,0));
this.g = new PVector(0,.06);
this.r = random(0,360);
this.radius = (int)random(30,50);
this.rv = random(-.1,.1);
}
abstract void draw();
void tick() {
v.add(g);
p.add(v);
r += rv;
}
boolean dead() {
return (p.x < 0 - radius || p.x > width + radius || p.y > height + radius);
}
}
class Triangle extends Shape {
public Triangle() {
super();
//for some reason triangles are always a bit too big.
this.radius /= 2;
}
void draw() {
pushMatrix();
translate(p.x,p.y);
rotate(r);
stroke(sc);
fill(fc);
triangle(-radius,-radius,0,radius/2*sqrt(3),radius,-radius);
popMatrix();
}
}
class Rectangle extends Shape {
void draw() {
pushMatrix();
translate(p.x,p.y);
rotate(r);
stroke(sc);
fill(fc);
rect(0,0,radius,radius);
popMatrix();
}
}
class Circle extends Shape {
public Circle() {
super();
}
void draw() {
stroke(sc);
fill(fc);
ellipse(p.x,p.y,radius,radius);
}
}
ArrayList<Shape> shapes;
ArrayList<Shape> deadShapes;
int maxShapes = 1000;
int br = 0;
int i = 0;
void setup(){
ellipseMode(CENTER);
rectMode(CENTER);
size(400,400);
shapes = new ArrayList<Shape>(maxShapes);
deadShapes = new ArrayList<Shape>(maxShapes/4);
}
void draw() {
i++;
if(i < 1500) {
if(i > 200 && i < 455) {
br = i - 200;
} else if (mousePressed && i < 200) {
i = 200;
} else if (i > 1000) {
if(br > 0) br--;
}
background(255-br);
fill(br,br,br,(i < 800)? 255 : br);
textAlign(CENTER,CENTER);
text(i < 250? "Click Me." : i < 900 ? "Hold the mouse down." : "Maybe try letting go.",width/2,height/2);
} else {
if(i/800 % 3 == 1) {
background(255);
} else if(i / 800 % 3 == 2) {
background(abs(255*sin(i/256.)));
}
}
if(i > 255) {
deadShapes.clear();
for(Shape s : shapes) {
if(mousePressed) s.tick();
if(s.dead()) {
deadShapes.add(s);
} else {
s.draw();
}
}
for(Shape s : deadShapes) {
shapes.remove(s);
}
if(shapes.size() < maxShapes && mousePressed) {
shapes.add(randShape());
}
}
}
Shape randShape() {
float r = random(0,1);
if(r > 0.66) {
return new Triangle();
} else if(r > 0.33) {
return new Rectangle();
} else {
return new Circle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment