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
Below you can ask and answer questions related to this video. If your question is unrelated to the video, please post it at the Processing Forum as I'm unable to help with all of your programs and still have enough free time to eat and sleep :)
comments powered by Disqus