Chapter 4 Python Turtle Graphics

Sections 4.1 - 4.3

\(\boxdot\) The turtle module - what is a module?
  • Our first turtle program, as shown in the text. NOTE: the turtle module is NOT supported (can not be used) by pythontutor.com

    import turtle                # allows us to use the turtles library
    import sys                   # imports the system module
    sys.setExecutionLimit(20000) # set execution time limit in thousands of a second
    wn = turtle.Screen()         # creates a graphics window
    alex = turtle.Turtle()       # create a turtle named alex
    alex.forward(150)            # tell alex to move forward by 150 units
    alex.left(90)                # turn by 90 degrees
    alex.forward(75)             # complete the second side of a rectangle
    wn.exitonclick()             # closes the window with a mouse click within the window
  • Activity 1: CAP, then modify the code above so that alex will draw a square with side-length of 100 units.

  • Some more turtle methods:

    • up() - lift the turtle’s tail up so nothing will be drawn when the turtle moves, no parameter

    • down() - put the turtle’s tail down so a line will be drawn when the turtle moves, no parameter

    • goto(x_loc, y_loc) - move the turtle to this location on the canvas

  • Activity 2: Write a program so that alex will draw a “plus sign” with each line member having a length of 200 units.

  • Activity 3: Write a program so that alex will draw an “x” with each line member having a length of 200 units. The four angles between the lines should be equal. Include a turtle stamp at the ends of each cross member using the stamp method.

\(\boxdot\) Further Activities
  • Activity 4: The for loop, lists and the turtle module are used in this exercise.

    • Construct a for loop that graphs a square of side length determined by the variable s_l

    • Create a turtle graph of a sequence of sqares with decreasing side lengths. The sequence of side lengths is 150, 125, 100, 75, and 50.

    • Next, use the following colors list to color-fill each square differently.

      colors = [‘orange’, blue’, ‘green’, ‘yellow’, ‘red’]

    • Alter your code so that the squares are centered on the middle of the window.

    • Alter your code so that the squares are draw with each axis rotated to the right by 10 degrees. The final figure should look like

  • Activity 5: Accomplish the following tasks in order to create a turtles program the draws a set of ticked axes and the graph of a function in the graph window.
    • Use the turtle method wn.setworldcoordinates(x_min, y_min, x_max, y_max) to dictate the coordinate locations within the window. Use the following values : y_min = x_min = -10.0, y_max = x_max = 10.0.
    • Draw the \(x\) and \(y\) axes on the graph.
    • Next, have your turtle draw tick marks on each of the axes. Use an interval of 1.0. Each tick length should be 0.2. Do this by defining a list object of tick locations. Then use a for loop to draw a tick at each location.
    • The range function can be used as an alternative to using a list object. Make changes to your program to accommodate this feature.
    • Draw tick marks with a spacing of 0.5 using the range function. Recall that the range steps only in integer lengths. However, you can use the tick spacing to determine how many single integer steps are required to draw all tick marks on a given axis.
    • Create a point plot of the function \(y(x) = \frac{1}{10}x^2 - 5\) using the dot() method. Use blue dots.
    • Generalize your program so that the y_min and y_max can be different from x_min and x_max, and the tick spacing can vary from one axis to the other. For example, let x_min = 0, x_max = 10, y_min = -5, y_max = 10, and use ticks spacings of 0.5 and 1.0, respectively.
\(\boxdot\) Other Turtle Methods
    import turtle  
    wn = turtle.Screen()
    t = turtle.Turtle()
    wn.tracer(0)       # drawing is done without showing what's happening
    .  
    .  
    .  
      
      
    wn.update()        # final and full results are shown 
    wn.exitonclick()