Code:
float rectx,recty,xspeed=7,yspeed=7;
void setup() {
fullScreen();
}
void draw() {
background(0);
rect(rectx,recty,300,150);
fill(0,255,0);
if(mousePressed==true);
rect(recty,rectx,300,150);
if(sideCollision(rectx)) {
xspeed = xspeed * -1;
}
if(topOrBottomCollision(recty)) {
yspeed = yspeed*-1;
}
rectx = rectx + xspeed;
recty = recty + yspeed;
}
boolean sideCollision(float x) {
if(x<0 || x>width-300) {
return true;
} else {
return false;
}
}
boolean topOrBottomCollision(float y) {
if(y<0 || y>height-150) {
return true;
} else {
return false;
}
}