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.
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
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.for
loop to draw a tick at each location.range
function can be used as an alternative to using a list object. Make changes to your program to accommodate this feature.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.dot()
method. Use blue dots. 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()