CPTR 124 Fundamentals of Programming
In this lab you will develop a collection of small programs that draw shapes on the screen using Python's Turtle graphics library of functions. Along the way you may use other functions from Python's standard library.
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- What to do
Write three small programs that each use Python's Turtle graphics library. The shapes to draw for the first two programs appear in the figures below. Your third program should draw a reasonably interesting shape of your own design.
The required shapes are
- the five-pointed star:
(The trick is to figure out the angles for the pen's turns.)
- the grid:
(Hint: You can use nested loops to simplify the coding—think about drawing multiple squares, each with its own unique (x, y) starting corner position. The outer loop can control the x component of the corner, and the inner loop can control the y component of the corner. The change in x and change in y would need to be the same as the length of a side in one of the smaller squares so the squares connect to each other with no gaps or overlaps.
Instead of two nested loops, you can use two sequential loops. One loop can draw the horizontal lines, and the other loop is responsible for the vertical lines.)
The third shape is your own design. Experiment and have fun. Try to come up with an interesting pattern.
https://docs.python.org/3/library/turtle.html provides the documentation for Python's Turtle graphics library. There you will find functions that control the pen's movements and visibility. Some functions you may find useful include
setpositionsets the pen to a given (x, y) coordinate
setheadingsets the pen to point in a particular direction
titlesets the text to appear in the window's title bar
penupdisables drawing until the pen is put back down (allows pen movements without drawing)
pendownenables the pen to draw when moved
forwardmoves pen forward a given distance in the current heading
rightturns pen to the right a specified number of degrees
leftturns pen to the left a specified number of degrees
hideturtlemakes the pen shape itself invisible and improves rendering speed
tracerturns off tracing (drawing animation) when its argument is zero—this speeds up rendering
updaterenders all pending drawing actions (necessary sometimes when tracing is disabled)
done(ends the drawing and waits on the user to close the window)
You are not limited to functions from the Turtle graphics module; your programs may use any functions available in the Python standard library.
- the five-pointed star:
- Skeletal Code
You can use the following skeletal code as a starting point for each of your programs:from turtle import * # Make all the Turtle graphics functions available tracer(0) # Do not animate the pen hideturtle() # Do not show the pen # Add your drawing code here . . . update() # Refresh window done() # Wait for the user to close the window
The call of the
hideturtlefunctions speed up the drawing process and makes the pen invisible. The
updatefunction ensures the last bit of drawing appears in the window (since tracing is turned off). The
donefunction keeps the graphics window visible until the user closes it.
- Check out
Your finished programs will be evaluated for correctness and compliance. When approved, you should submit your Python source file to http://eclass.e.southern.edu.