Skip to content

Instantly share code, notes, and snippets.

@nguyenj
Last active December 20, 2015 17:29
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 nguyenj/6168707 to your computer and use it in GitHub Desktop.
Save nguyenj/6168707 to your computer and use it in GitHub Desktop.
Just click on the canvas to create new balls.
Ball[] balls = new Ball[1];
void setup() {
size(200, 200);
balls[0] = new Ball(50, 100, 16, 0.1);
}
void draw() {
background(0);
for ( int i = 0; i < balls.length; i++ ) {
balls[i].display();
balls[i].move();
}
}
void mousePressed() {
Ball b = new Ball(mouseX, mouseY, 16, 0.1);
balls = (Ball[]) append(balls, b);
}
class Ball {
float x;
float y;
float speed;
float w;
float gravity;
Ball( float temp_x, float temp_y, float temp_w, float temp_gravity ) {
x = temp_x;
y = temp_y;
w = temp_w;
speed = 0;
gravity = temp_gravity;
}
void display() {
fill(255);
noStroke();
ellipseMode(CENTER);
ellipse(x,y,w,w);
}
void move() {
y = y + speed;
speed = speed + gravity;
if ( y > height ) {
speed = speed * -0.95;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment