w3 Turtle Graphics and Python Modules
w3 Turtle Graphics and Python Modules
• Does the program run too fast? No problem, add the following line after
creating the graphics window:
win.delay(100) # delay each action that tito performs by 100 ms
13-Aug-19 RMIT University 6
More turtle
• An object can have various methods — things it can do
— and it can also have attributes (sometimes also
called properties). For example:
win.exitonclick()
13-Aug-19 RMIT University 18
Iteration simplifies our turtle program 2
• Actually, it’s more important that we found a repeated
pattern rather than saving a few lines of code
• We could use another kind of list to do the same thing
import turtle
win.exitonclick()
13-Aug-19 RMIT University 19
Iteration simplifies our turtle program 3
• Now, let’s add one more line of code to make the
square more beautiful
import turtle
win.exitonclick()
13-Aug-19 RMIT University 20
The range function
• Generating a list of integers is a very common thing to
do, especially when using for loop
• And this?
tito.up() # ask tito to pick her tail up, i.e. no draw after that
tito.forward(100) # this moves tito, but no line is drawn
tito.down() # ask tito to put her tail down
...
tito.shape("turtle")
...
win = turtle.Screen()
win.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")
tess.up()
for size in range(5, 60, 2): # start with size = 5 and grow by 2
tess.stamp() # leave an impression on the canvas
tess.forward(size) # move tess along
tess.right(24) # and turn her
win.exitonclick()
import math
print(math.pi)
print(math.e)
print(math.sqrt(2.0))
print(math.sin(math.radians(90))) # sin of 90 degrees
import random
• Examples