Learn creative coding writing simple programs

15. Ball bouncing at the window borders

This example is a bit more advanced. In the past, when our animated circle reached the border it jumped to the opposite border. Now we discover a way to make our circle bounce at the borders!
The trick is to have a variable that says how much to move our circle on each frame. This variable can hold a positive number or a negative number. If the number is positive (above 0), our circle can move to the right or down. If it's negative (below 0, like -1), our circle can move to the left or up.
If we flip the sign of the variable when touching the borders of the screen, it will look like the circle is bouncing at the borders.


Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```