Step 1: Warm-up - Working with turtles
examples.py and read it over. Read the turtle documentation for each function and see what it does. Notice that there are several names for some of the functions, such as
Optional read: A quirk of the turtle library, which makes it unusual compared to other libraries you may work with, is that
import turtle imports a whole, single
turtle object. Think of it like learning a language in school: a library is usually like a unit on, say, going to a restaurant, or going to an airport. In that unit, you might learn about different objects that relate to the activity, different actions you can do with those objects, and the words for talking about those objects and actions. Similarly, most libraries consist of objects and actions you can do, or actions specific to objects (sometimes called methods.) To actually use those objects, you usually have to create a variable for them first. This could look like:
import airportThings # all the things!
my_plane = airportThings.Plane() # make just one plane
my_plane.fly() # or whatever you want to do with the plane...
your_plane = airportThings.Plane() # this makes a whole other plane!
turtle is a little different. Instead of talking about an airport, tickets, security, luggage, flights, and terminals, it just gives you an airplane.
import turtle creates one
turtle object called
turtle, which can do all the things described in the documentation. You can
clone it to get more. Like this:
import turtle as my_turtle # this is just one turtle
your_turtle = my_turtle.clone()
There's no other way to create a new
turtle, separate from the original one. A lot of times, you can import a single object from a larger library, like
from airportThings import Plane, but you still have to create a specific instance, like
my_plane = Plane() similar to the plane example from before.
Anyway. See what you can do with the turtle. Test this code by running
python examples.py in the terminal. Read the comments to help you think about what the code is doing, and read the documentation if you're confused. Can you:
Draw a square?
Draw a circle?
Change the line and fill color?
Speed it up?
Make a cloned turtle?
Step 2: Activity - Fast Functions
You may have noticed that making turtles do certain things takes a lot of repetition. Hopefully, you used some loops to simplify things (like making a circle).
What if you wanted to make a square, and then another square half the size? You'd have to write all that code twice. What if we had some kind of structure that could reuse code without rewriting it, and only change a few details if we wanted to?
This is exactly what functions can do. They pack multiple lines of code under a single name and take 0 or more parameters, which are like options for how the function will do things. Open
turtle_functions.py and read through it to see how functions are used. Then, see if you can complete the function headers to create interesting patterns. Test it by typing
python turtle_functions.py in the terminal.
Step 3: Turtle Armies
Have you ever held a bunch of markers together in a fist to draw the same thing in multiple colors? That's what we're going to do with turtles. We'll create our own
TurtleArmy object or class to do this. An object or class is just a bunch of information about something in one package, along with methods, which are functions for what it can do. Open
turtle_army.py and read through it to see how classes are created. Then, see how you can use the
TurtleArmy to move a bunch of pens at once. Test it by typing
python turtle_army.py in the terminal.
To use the
TurtleArmy in other code you write, you can put
from turtle_army import TurtleArmy at the top of the file. To create an army, type something like
little_army = TurtleArmy(3) with however many turtles you want. Then, you can use
little_army in most places you would use a regular
Step 4: Make it your own
drawing_turtle.py. It contains a version of the
DrawingTurtle, which can draw a vector file as it moves. Test it by typing
python turtle_army.py in the terminal.
Try using your previously made functions to draw a cool vector image with this turtle. To use the
DrawingTurtle in other code you write, you can put
from drawing_turtle import DrawingTurtle at the top of the file. To create a drawer, type something like this:
d = drawSvg.Drawing(200, 200, origin = 'center') # create a vector canvas
t = DrawingTurtle(d)
Then, you can use
t in most places you would use a regular
turtle. Try creating a cool vector design, then laser cut it into something.