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.

  1. Teams

    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.

  2. 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:

      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

    • setposition sets the pen to a given (x, y) coordinate
    • setheading sets the pen to point in a particular direction
    • title sets the text to appear in the window's title bar
    • penup disables drawing until the pen is put back down (allows pen movements without drawing)
    • pendown enables the pen to draw when moved
    • forward moves pen forward a given distance in the current heading
    • right turns pen to the right a specified number of degrees
    • left turns pen to the left a specified number of degrees
    • hideturtle makes the pen shape itself invisible and improves rendering speed
    • tracer turns off tracing (drawing animation) when its argument is zero—this speeds up rendering
    • update renders 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.

  3. 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 tracer and hideturtle functions speed up the drawing process and makes the pen invisible. The update function ensures the last bit of drawing appears in the window (since tracing is turned off). The done function keeps the graphics window visible until the user closes it.

  4. 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.