File: bouncing_ball.py

Write a graphics program that creates a ball and makes it bounce around the screen. Ignore gravity and have a collision with the right or left wall reflect the x direction of the ball, and have a collision with the top or bottom wall change the y direction of the ball.

The figure below shows a ball moving through a bounce with the bottom wall.

Milestone 1: Create a Ball

It can be any size. Make it start at a random onscreen location.

Milestone 2: Make it move

Loop forever and in the loop update the ball and pause. You can pause by adding import time to the top of your program and doing the following:

time.sleep(seconds)

If you don't pause, the computer will animate faster than the human eye can see the movement!

There is also a function to move a graphical shape on the canvas:

canvas.move(object, dx, dy)

dx is the number of pixels the object will move in the x direction.
dy is the number of pixels the object will move in the y direction.

Milestone 3: Bounce

Now, before you update the position of the ball, you should check if the ball has collided with the wall and if so you should update the direction it is moving.

Reflections should follow the rule "the angle of incidence equals the angle of reflection". You can implement this by flipping the x direction if you hit a left or right wall and flipping the y direction if you hit a bottom or top wall.

How do you figure out the position of the ball? You can use the functions:

canvas.get_left_x(obj)
canvas.get_top_y(obj)

A python function that may come in handy is abs(val) - it returns the absolute value of a number.