Today's lab will focus on the python's mathematics library and the turtle graphics library. An excellent overview of turtle graphics can be found at the How to think like a Computer Scientist.
We will use the turtle graphics (built-in to python) to write a simulation of a bouncing ball.
Here is the first program (inspired from motionscript.com):
#Idea from: http://www.motionscript.com/mastering-expressions/simulation-basics-1.html from turtle import * from math import * def main(): veloc = .5 #horizontal velocity (pixels per second) amp = 75 #sine wave amplitude (pixels) freq = .01 #oscillations per second #Set up a graphics window and turtle: win = Screen() ball = Turtle() ball.speed(10) win.setworldcoordinates(0.0, -100.0, 500.0, 100.0) #Draw a line for the x-axis: ball.up() goto(0,0) ball.down() ball.forward(500) ball.up() ball.backward(500) ball.down() #Draw a ball that follows a sine wave for time in range(500): #For each time step, move the turtle to (time, sin(time)) # (with some scaling to make it fill screen) ball.goto(time,int(amp*sin(freq*time*2*pi))) win.exitonclick() #Close the window when clicked main()Save this file to your USB or MyDocuments folder and run the program. Let's go through what each part does:
from turtle import * from math import *The import statement asks python to include the functions from a given module, or collection of functions. These two import statements ask for the functions from the turtle and math modules, both of which are built-in to Python.
veloc = .5 #horizontal velocity (pixels per second) amp = 75 #sine wave amplitude (pixels) freq = .01 #oscillations per secondThese three variables control the speed, height, and bounce of the ball. Try changing them and see what happens to the animation. Your goal is to have the ball touch the ceiling every time.
#Set up a graphics window and turtle: win = Screen() ball = Turtle() ball.speed(10) win.setworldcoordinates(0.0, -100.0, 500.0, 100.0)These lines set up the graphics window. We are using a turtle to represent the bouncing ball (and is stored in the variable called ball). To make the animation go faster, we have adjusted the speed of the turtle, ball to be maximum (i.e. 10). In the turtle module, there is a function for changing the coordinates of a graphics window. We did so here to make the math calculating the height of the bouncing ball easier. Our new coodinates have the smallest x-value as 0, the smallest y-value as -100, the largest x-value is 500, and the largest y-value is 100.
#Draw a line for the x-axis: ball.up() goto(0,0) ball.down() ball.forward(500) ball.up() ball.backward(500) ball.down()The up() command lifts the drawing pen up. This is very useful if you want to start drawing from a certain point (and don't want extra lines to show up as you reach the point). Similarly, down() puts down the pen for drawing. The goto(x,y) moves the turtle to the coordinates (x,y). The commands forward() and backwards() move the turtle forward and backwards respectively. The input parameter to these commands specifies how far the the turtle should move.
#Draw a ball that follows a sine wave for time in range(500): #For each time step, move the turtle to (time, sin(time)) # (with some scaling to make it fill screen) ball.goto(time,int(amp*sin(freq*time*2*pi)))This loop repeats the indented statements 500 times. First time through the variable time is 0; the next time through it is 1; and it keeps going until time is 499. Each repetition of the loop moves the turtle to a new point. The x-coordinate of the point is the variable time, so, first it is 0, then 1, and keeps going until it reaches 499. The y-coordinate is the sine of the x-coordinate (with a bunch of scaling that can be adjusted to make it fit the screen or increase oscillations).
win.exitonclick() #Close the window when clickedWe add this line at the end of our program to close the window when the user clicks.
main()This is a call (or invocation) of the function main() that we just wrote. It is not necessary to have in your file, but if you do, you do not have to type main() in the Python shell to run your function. Instead, it will be called when you use the F5 key.
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-coordinate:
abs(amp*sin(freq*time*2*pi))Try adding this to your goto() statement, that is:
ball.goto(time,abs(int(amp*sin(freq*time*2*pi))))Try running your program to see the bouncing ball! Two more challenges to think about (ask your instructor for hints if you do not see how to do these, since questions about them will appear on quizzes and exams):
If you finish early, you may work on the programming problems.