xxxxxxxxxx
//Balls
//Amount of balls
int balls = 55;
//Variables
float [] ballsX = new float [55]; //Position X
float [] ballsY = new float [55]; //Position Y
float [] ballsSY = new float [55]; //Speed Y
float [] ballsSX = new float [55]; //Speed x
float ballsW;
//Button
float bX, bY, bW;
//Ball bouncing off button
boolean ballbounce = false;
void setup (){
size (150 , 150);
//Button position
bX = 50;
bY = 50;
bW = 50;
//Ball size
ballsW = 5;
//Balls positions
for (int x1 = 0 ; x1 < ballsX.length; x1++) {
ballsX[x1] = random(height);
ballsSX[x1] = 1;
}
for (int y1 = 0; y1 < ballsY.length; y1++) {
ballsY[y1] = random(height);
ballsSY[y1] = 1;
}
}
void draw (){
//Background change when balls bounce on button
if (ballbounce) {
background (255 , 191 , 254); //Bright astel pink
} else {
background (245 , 191 , 254); //Bright pastel purple
}
//Button
fill (0);
noStroke ();
rect (bX , bY , bW , bW);
//Balls
for (int i = 0; i < ballsY.length; i++) { //Loop
stroke(0,((int)random(255)),0); //Random colours in green spectrum
fill(0,((int)random(255)),0); //Random colours in green spectrum
ellipse (ballsX[i], ballsY[i], ballsW, ballsW);
ballsY[i] = ballsY[i] + ballsSY[i]; //Movement in Y
ballsX[i] = ballsX[i] + ballsSX[i]; //Movement in X
//Balls touching button
if (( ballsY[i] > bY-ballsW) && ( ballsY[i] < bY+bW+ballsW) && (ballsX[i] > bX-ballsW) && (ballsX[i] < bX+bW+ballsW)) {
ballbounce = !ballbounce;
//Balls bouncing in different directions
if ((ballsY[i] < bY) || (ballsY[i] > bY+bW)) {
ballsSY[i] = ballsSY[i] * -1;
} else if ((ballsX[i] < bX) || (ballsX[i] > bX+bW)) {
ballsSX[i] = ballsSX[i] * -1;
}
}
//Balls bouncing on walls
if (( ballsY[i] > height) || ( ballsY[i] < 0)) {
ballsSY[i] = ballsSY[i] * -1;
}
if ((ballsX[i] > width) || (ballsX[i] < 0)) {
ballsSX[i] = ballsSX[i] * -1;
}
}
}