xxxxxxxxxx
int buttonX;
int buttonY;
int buttonWidth;
int buttonHeight;
int x;
int y;
int w;
int Rad;
int speed;
boolean buttonClicked = false;
void setup () {
size (500,300);
buttonX = width/4+width/2;
buttonY = height/3;
buttonWidth = 100;
buttonHeight = 100;
x = 0;
y = height/2;
w = 30;
Rad = 13;
speed = 4;
}
void draw (){
background (66,188,244); //blue
if (buttonClicked == true) {
background (198,114,255); //purple
} else {
buttonClicked = false;
}
//ball bouncing off button and side
if ( (x > width/4+width/2) || (x < 0) ) {
speed = speed * -1;
}
//hit detection of ball to button
if ( (y+Rad >= buttonY) &&
(x-Rad <= buttonX + buttonWidth) &&
(x+Rad >= buttonX) &&
(y-Rad <= buttonY + buttonHeight) ) {
if (x < buttonX) { //hits left side
speed = -4;
x = buttonX - Rad;
}
buttonClicked = !buttonClicked;
}
fill (66,244,232); //button
stroke (255,255,255);
rect (buttonX,buttonY,buttonWidth,buttonHeight);
fill (255,255,165); //ball
stroke (0,0,0);
ellipse (x,y,w,w);
x = x+speed;
}