Today's lab will focus on the python's mathematics library and the book's graphics library.

Bouncing Ball

Included with the textbook is a simple graphics library. To use it, place the file to the directory where you saved your python files. There is a useful cheat sheet of the graphics functions.

Here is the first program (inspired from

#Idea from:

from math import *
from graphics import *
from time import *

def goodSine():
    veloc = .5  #horizontal velocity (pixels per second)
    amp = 50    #sine wave amplitude (pixels)
    freq = .01  #oscillations per second

    #Set up a graphics window:
    win = GraphWin("Good Sine Waves",400,200)
    win.setCoords(0.0, -100.0, 200.0, 100.0)

    #Draw a line for the x-axis:
    p1 = Point(0,0)
    p2 = Point(200,0)
    xAxis = Line(p1,p2)

    #Draw a ball that follows a sine wave
    for time in range(1000):
        x = time*veloc
        y = amp*sin(freq*time*2*pi)
        ball = Circle(Point(x,y),2)
        sleep(0.1)  #Needed so that animation runs slowly enough to be seen


Save this file to your USB or MyDocuments folder (along with and run the program. Let's go through what each part does:

To make the ball appear to bounce, we need for it to stay above the line representing the x-axis. Another way of saying that is its y-coordinate should always be positive. A simple way to do that is to take the absolute value of the expression for the y variable:

        y = abs(amp*sin(freq*time*2*pi))
Try running your program to see the bouncing ball!

If you finish early, you may work on the programming problems.