A simplified version to make things easier. What we are going to do is make the ball and array of balls.
this first example is where we are starting from.
float ballx = 0;
float bally = 200;
float speedx = 2.7;
float speedy = 1.2;
void setup(){
size(600, 400);
}
void draw (){
background (50);
smooth();
ballx = ballx + speedx;
bally = bally + speedy;
if ((ballx > width) || (ballx < 0)) {
speedx = speedx * -1;
}
if ((bally > height) || (bally < 0)) {
speedy = speedy * -1;
}
ellipse (ballx, bally, 10, 10);
}
Here's the code with 200 balls.
int ballnumber = 200;
float [] ballx = new float [ballnumber];
float [] bally = new float[ballnumber];
float [] speedx = new float[ballnumber];
float [] speedy = new float[ballnumber];
void setup(){
size(600, 400);
for (int i=0; i< ballnumber; i++){
ballx [i] = width/2;
bally [i] = height/2;
speedx [i] = random (-3, 3);
speedy [i] = random (-3, 3);
}
}
void draw (){
background (50);
smooth();
for (int i=0; i< ballnumber; i++){
ballx [i] = ballx [i] + speedx [i];
bally [i] = bally [i] + speedy [i];
if ((ballx [i] > width) || (ballx [i] < 0)) {
speedx [i] = speedx [i] * -1;
}
if ((bally [i] > height) || (bally [i] < 0)) {
speedy [i] = speedy [i] * -1;
}
ellipse (ballx [i], bally [i], 10, 10);
}
}