xxxxxxxxxx
float[] elX = new float[200];//total 200 balls
float[] elY = new float[200];//total 200 balls
float[] elSpeedX = new float[200]; //for different speed X
float[] elSpeedY = new float[200]; //for different speed Y
int elWidth = 20; //diameter
int rectX = 200;
int rectY = 230;
int rectW = 100;
int rectH = 70;
int radius = elWidth/2;
boolean clickButton = false;
void setup() {
size (500, 500);
for (int i = 0; i < 200; i++) {
elX[i] = 220 + i; //make the balls start at postion (220, 130)
elY[i] = 130 + i;
elSpeedX[i] = random(-8, 8); // give balls different/random speed to separate them
elSpeedY[i] = random(-8, 8);
// frameRate (4);
}
}
void draw() {
if(clickButton){ // click Button to change color
background (0);
fill(78, 239, 67);
fill(random(255), random(0), random(255), random(255));
for (int i = 0; i < 200; i++) {
elX[i] += elSpeedX[i];
elY[i] += elSpeedY[i];
rect (rectX, rectY, rectW, rectH);
}
for (int i = 0; i < 200; i++) { //all the balls are bouncing back and forth inside the background
if ((elX[i] > width)||(elX[i] < 10)) {
elSpeedX[i] *= -1;
}
if ((elY[i] > height) || (elY[i] <= 0)) {
elSpeedY[i] *= -1;
}
if (( elX[i]+ radius > rectX ) && ( elY[i]+ radius> rectY) && //bouncing off the rectangle
( elX[i]- radius < rectX + rectW) && (elY[i]- radius <rectY+rectH)) {
elSpeedX[i] *= -1;
elSpeedY[i] *= -1;
}
ellipse (elX[i], elY[i], elWidth, elWidth);
}
}else{
background (255);
fill (0);
rect (rectX, rectY, rectW, rectH);
String [] Inspirational = new String [1];
Inspirational[0] = "Click the Black Box To Start";
textSize (30);
text (Inspirational [int(random(Inspirational.length))], 80, height/3);
}
}
void mousePressed() {
if ((mouseX > rectX) && (mouseY > rectY) && (mouseX < rectX + rectW) && (mouseY < rectY + rectH)) { //give an area for the button are
if (clickButton == true) {
clickButton = false;
} else {
clickButton = true;
}
}
}