[help] how to let the rectangle bounce back?

how to let rectangle that moves around and bounce back when it hits the border of the window?

I’m stuck with the bounce back of the border.

Another homework assignment?
Which part are you stuck with?
A) Detecting the border and thus deciding when to bounce back
B) Subtracting rather than adding to the current position?