# I want to make a rectangle that will spawn when mouseclicked and bounce back and collide with another rectangle

**URL:** <https://discourse.processing.org/t/i-want-to-make-a-rectangle-that-will-spawn-when-mouseclicked-and-bounce-back-and-collide-with-another-rectangle/25743>\
**Category:** Coding Questions\
**Created:** [November 27, 2020, 1:02am UTC](https://discourse.processing.org/t/i-want-to-make-a-rectangle-that-will-spawn-when-mouseclicked-and-bounce-back-and-collide-with-another-rectangle/25743 "2020-11-27T01:02:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![0w3n928](https://avatars.discourse-cdn.com/v4/letter/0/b77776/32.png) [@0w3n928](https://discourse.processing.org/u/0w3n928)\
**Post date:** [November 27, 2020, 1:02am UTC](https://discourse.processing.org/t/i-want-to-make-a-rectangle-that-will-spawn-when-mouseclicked-and-bounce-back-and-collide-with-another-rectangle/25743/1 "2020-11-27T01:02:43Z")

</div>

I’m trying to make another rectangle spawn when mouse is clicked once and it will collide to my first bouncing rectangle and I can add however many i want but my code doesn’t even need me to click and it doesn’t really work.

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;  
}  
}
