CSci 157 - Lab 10
The purpose of this lab is to develop your mastery of using loops, animations, and running from the command line.
Warmup:- In the Python shell, if you import the time module, you can
see a description of what functions it provides by entering
help(time)
- What is returned when you call
time.time()
in the shell? Wait a few seconds and call it again. How much has the returned value changed?
The HotRods
- Download the file raceway.py
to your CS157 folder. Run it to ensure it works as expected.
- Improve the program in two ways:
- In
draw_car
make each racer look as cool as you can make it within the limits imposed by the graphics module You could improve the look of the side view, or change it to a top view as in the right screenshot above. You can simply modify the shapes already used, or if you like you can add other graphic shapes to the car or a driver image (You can also (optionally) download and use my "driver" images: racer1.jpg, racer2.jpg, or find your own)
- Note: if you add another shape to draw a car, you'll also
need to add its variable to the end of the list returned
by the function
draw_car
- In
setup_racetrack
, rather than a single gray strip, you can use a loop to lay down a series of white rectangular segments (or very thick lines) as lane markers
- In
- Switch driver and observer.
Now add a way to compute and display the time and distance (in pixels) taken by each car to run the race. Create and display a "Jumbotron-style" scoreboard (finish times on a dark background) centered near the top of the window showing the time/distance each racer obtained, using the color of each racer. Some notes on how this might be done:
- since the cars start at x-coordinate 0, the distance is then the (leftmost) x-coordinate of the car body when it stops
- the
time
module includes a functiontime()
that returns the number of seconds since Jan 1, 1970 (also known as the "Epoch") - if you call
time()
twice and save the returned values in two different variables, their difference is the elapsed time in seconds; figure out the best places to call the function to time the movement of the cars from start to finish line
- See the Tutorial
for using formatted strings to specify the number of digits (both
before and after the decimal point) that are
displayed.
- Demonstrate your work to the instructor.