Created
September 28, 2011 17:49
-
-
Save raheelahmad/1248645 to your computer and use it in GitHub Desktop.
Drawing MovingBalls
This file contains hidden or 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 MovingBall { | |
float x, y, w; // variables for drawing the ellipse | |
boolean movingForward; | |
boolean movingUp; | |
void drawItself() { | |
ellipse(x, y, w, w); | |
} | |
void update() { | |
if (movingForward) | |
x += 1; | |
else | |
x -= 1; | |
if (movingUp) | |
y -= 1; | |
else | |
y += 1; | |
} | |
void check() { | |
if (y <= 0) | |
movingUp = false; | |
if (y >= height) | |
movingUp = true; | |
if (x >= width) | |
movingForward = false; | |
if (x <= 0) | |
movingForward = true; | |
} | |
} | |
MovingBall[] balls; // declaration | |
int numBalls; | |
void setup() { | |
size(400, 400); | |
smooth(); | |
// setting up the space for 3 moving balls | |
numBalls = 70; | |
balls = new MovingBall[numBalls]; | |
// setting up the startup values for the balls in the array | |
for (int i = 0; i < numBalls; i++) { | |
balls[i] = new MovingBall(); | |
balls[i].x = random(0, width); | |
balls[i].y = random(0, height); | |
balls[i].w = random(5, 25); | |
balls[i].movingForward = true; | |
balls[i].movingUp = true; | |
} | |
fill(127); | |
noStroke(); | |
} | |
void draw() { | |
background(255); | |
fill(0); | |
for (int i = 0; i < numBalls; i++) { | |
balls[i].drawItself(); | |
balls[i].update(); | |
balls[i].check(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment