int n = 500;
float[] xpos = new float[n];
float[] ypos = new float[n];
float[] sizes = new float[n];
float[] alphas = new float[n];
// motion
boolean up, down, left, right, in, out;
void setup() {
size(800, 800);
colorMode(HSB, 360, 100, 100, 255);
// something like this works for any array
for(int i=0; i<n; i++) {
xpos[i] = random(0, width);
ypos[i] = random(0, height);
sizes[i] = random(5, random(5, 30));
alphas[i] = random(50, 100);
}
}
void draw() {
// background(0);
noStroke();
fill(0, 50);
rect(0, 0, width, height);
for(int i=0; i<n; i++) {
fill(0 + i/8, 100, 200, alphas[i]); // alpha => 0 is transparent, 255 is opaque
ellipse(xpos[i], ypos[i], sizes[i], sizes[i]);
if(up) {
ypos[i] += sizes[i]/2;
}
if(down) {
ypos[i] -= sizes[i]/2;
}
if(right) xpos[i] -= sizes[i]/2;
if(left) xpos[i] += sizes[i]/2;
if(in) {
sizes[i] += 0.1;
xpos[i] += (xpos[i] - width/2)/100;
ypos[i] += (ypos[i] - height/2)/100;
}
if(out) {
sizes[i] -= 0.1;
xpos[i] -= (xpos[i] - width/2)/100;
ypos[i] -= (ypos[i] - height/2)/100;
}
// respawn stars that are off screen
if(xpos[i] < 0 || xpos[i] > width || ypos[i] < 0 || ypos[i] > height || sizes[i] < 1) {
xpos[i] = random(width);
ypos[i] = random(height);
sizes[i] = random(5, 20);
}
}
}
void mouseDragged() {
right = left = up = down = false;
if(mouseX > pmouseX) right =true;
if(mouseX < pmouseX) left =true;
if(mouseY > pmouseY) down = true;
if(mouseY < pmouseY) up = true;
}
void mouseReleased() {
right = left = up = down = false;
}
void keyPressed() {
if(key == 'w') {
up = true;
}
if(key == 's') {
down = true;
}
if(key == 'a') {
left = true;
}
if(key == 'd') {
right = true;
}
if(key == 'e') {
in = true;
}
if(key == 'q') {
out = true;
}
}
void keyReleased() {
if(key == 'w') {
up = false;
}
if(key == 's') {
down = false;
}
if(key == 'a') {
left = false;
}
if(key == 'd') {
right = false;
}
if(key == 'e') {
in = false;
}
if(key == 'q') {
out = false;
}
}