[VISIBLE ONLY LOCALLY] Optionally view what the website will look like at another time (in PDT):
Credit: Based on an example by Mehran Sahami
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.
It can be any size. Make it start at a random onscreen location.
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:
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.
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:
A python function that may come in handy is
abs(val) - it returns the absolute value of a number.