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.