A Ball In A Box

From CodeStuff
Jump to: navigation, search

This program shows a ball bouncing within the confines of a rectangle.

The ball position is stored in the variables cx and cy. The motion of the ball is stored in the dx and dy. dx represents the horizontal speed of the ball and dy represents the vertical speed.

dx gets added to cx each move cycle moving it sideways and dy gets added to cy to move up or down.

The move cycle also has to make sure the ball does not leave the rectangle. because the rectangle has four sides, it must do four checks looking at each side in turn.

cy is checked to see if it is higher than the top or lower than the bottom. cx is compared with the left and right edges of the rectangle.

Try adding the line


Think about what might happen when you run it, then run the program to see if you guessed right.