Created
March 17, 2020 05:36
-
-
Save todocono/79bf9ddf0ba81fdd77bd5d947dcb30e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* IxLab 2020S - NYU Shanghai | |
* recorded to introduce concepts | |
* Ideas for exercises to practice: | |
* make the balls have different size | |
* make the balls appear if you press a key | |
* make them vanish if certain time elapsed | |
*/ | |
float[] x; //positions X | |
float[] y; //positions Y | |
float[][] spd; //speeds: first element is speedX, second speedY | |
color[] c; //colors of the balls | |
int d = 20; | |
int qty = 50; //how many balls | |
int t = 255; | |
void setup() { | |
size(500, 500); | |
x = new float[qty]; | |
y = new float[qty]; | |
spd = new float[qty][2]; | |
c = new color[qty]; | |
for (int i=0; i<qty; i++) { | |
x[i] = random(width); | |
y[i] = random(height); | |
spd[i][0] = random(-5, 5); | |
spd[i][1] = random(-5, 5); | |
} | |
noStroke(); | |
} | |
void draw() { | |
background(125); | |
for (int i=0; i<qty; i++) { | |
x[i] = x[i] + spd[i][0]; | |
y[i] = y[i] + spd[i][1]; | |
if ((x[i] > width-d/2) || (x[i] < d/2)) { // x is too big | |
spd[i][0] *= -1; | |
} | |
if ((y[i] > height-d/2) || (y[i] < d/2)) { // x is too big | |
spd[i][1] *= -1; | |
} | |
fill(c[i]); | |
ellipse (x[i], y[i], d, d); | |
} | |
} | |
void mousePressed() { | |
for (int i=0; i<qty; i++) { | |
c[i] = color(random(0, 255), random(0, 255), random(0, 255), t); //the 4th parameter sets transparency | |
} | |
} | |
void keyPressed() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment