Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created October 8, 2013 07:59
Show Gist options
  • Select an option

  • Save yoggy/6881185 to your computer and use it in GitHub Desktop.

Select an option

Save yoggy/6881185 to your computer and use it in GitHub Desktop.
class Ball {
int x, y, dx, dy, r;
Ball() {
x = (int)random(0, width);
y = (int)random(0, height);
dx = (int)random(-10, 10);
dy = (int)random(-10, 10);
r = 10;
}
void update() {
int nx = x + dx;
int ny = y + dy;
if (nx < 0 || width <= nx) dx = -dx;
if (ny < 0 || width <= ny) dy = -dy;
x = x + dx;
y = y + dy;
}
void draw() {
fill(#ffffff);
stroke(#ffffff);
ellipse(x, y, r, r);
}
}
ArrayList<Ball> list;
void setup() {
size(400, 400);
list = new ArrayList<Ball>();
for (int i = 0; i < 200; ++i) {
list.add(new Ball());
}
}
void draw() {
background(#000044);
for (Ball b : list) {
b.update();
b.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment