Skip to content

Instantly share code, notes, and snippets.

@todocono
Last active March 17, 2020 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todocono/a337cc756f67d654582244e86dcc7532 to your computer and use it in GitHub Desktop.
Save todocono/a337cc756f67d654582244e86dcc7532 to your computer and use it in GitHub Desktop.
/* IxLab 2020S - NYU Shanghai
* recorded to introduce concepts
* Ideas for exercises to practice:
* Add a third ball bouncing
* Make the size different for dif. balls
* Make the color different for dif. balls
*/
float[] x; //position of the ball 1
float[] y; //position of the ball 2
float spdX1, spdY1; //speed of the ball 1
float spdX2, spdY2; //speed of the ball 2
float spdX3, spdY3; //speed of the ball 2
color c1, c2, c3; //colors of the balls
int d = 20;
void setup() {
size(400, 400);
x = new float[3];
y = new float[3];
for (int i=0; i<2; i++) {
x[i] = random(width);
// y[i] = random(height);
}
y[0] = random(height);
y[1] = random(height);
y[2] = random(height);
noStroke();
spdX1 = random(-10, 10);
spdY1 = random(-10, 10);
spdX2 = random(-10, 10);
spdY2 = random(-10, 10);
spdX3 = random(-10, 10);
spdY3 = random(-10, 10);
}
void draw() {
background(125);
x[0] = x[0] + spdX1;
x[1] = x[1] + spdX2;
x[2] = x[2] + spdX3;
y[0] = y[0] + spdY1;
y[1] = y[1] + spdY2;
y[2] = y[2] + spdY3;
if ((x[0] > width-d/2) || (x[0] < d/2)) { // x is too big
spdX1 *= -1; // spdX = -spdX;
}
if ((x[1] > width-d/2) || (x[1] < d/2)) { // x is too big
spdX2 *= -1; // spdX = -spdX;
}
if ((x[2] > width-d/2) || (x[2] < d/2)) { // x is too big
spdX3 *= -1; // spdX = -spdX;
}
if ((y[0] > height-d/2) || (y[0] < d/2)) { // x is too big
spdY1 *= -1;
}
if ((y[1] > height-d/2) || (y[1] < d/2)) { // x is too big
spdY2 *= -1;
}
if ((y[2] > height-d/2) || (y[2] < d/2)) { // x is too big
spdY3 *= -1;
}
fill(c1);
ellipse (x[0], y[0], d, d);
fill(c2);
ellipse (x[1], y[1], d, d);
fill(c3);
ellipse (x[2], y[2], d, d);
}
void mousePressed() {
c1 = color(random(0, 255), random(0, 255), random(0, 255));
c2 = color(random(0, 255), random(0, 255), random(0, 255));
}
void keyPressed() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment