Last active
December 20, 2015 17:29
-
-
Save nguyenj/6168707 to your computer and use it in GitHub Desktop.
Just click on the canvas to create new balls.
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
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); | |
} |
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
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