Other Turtle Functions
00:00 Other Functions. Undoing changes. No matter how careful you are, there’s always the chance of a mistake, whether it’s a typo or issuing the wrong command in good faith.
00:13
Fortunately, turtle
has an .undo()
function that will step back in the history of the turtle, undoing the last command you ran.
00:20
You can see how far back you can go by querying .undobufferentries()
. Here, there are 9
steps that can be undone.
00:34
And the undo buffer size can be set by changing .setundobuffer()
to the value you want. So if you feel you need more entries to undo, setting this to a larger number will save the day. You’ve already seen how to use .clear()
.
00:51 This will clear the turtle’s current drawing, but note that all variables will still be present and the turtle in question will stay where it was. Also note that if you have more than one turtle onscreen, only the turtle that you clear will be reset.
01:05 Let’s create a second turtle and make it do some drawing.
01:17
Now I’m back on our original turtle, t
. We can clear that using t.clear()
. Notice that v
is unaffected by this. Resetting the environment. It’s possible to go further than the .clear()
command by using the .reset()
command, which will return the turtle in question to the home location of (0,0)
.
01:41
This can be useful if your turtle has gone walkabout and is now off the screen. It’s also possible to show or hide a turtle using .showturtle()
or .hideturtle()
.
01:59
There you can see v
has disappeared, and is now back onscreen. However, v
is going to be hidden for the rest of this video for clarity.
02:13
Leaving a stamp. The .stamp()
function leaves an imprint of the turtle at its current position, which can be useful for marking its progress.
02:33
Note that each time the .stamp()
method was run it returned a number. This is the ID of the stamp, and it can be used to remove it if needed, using the .clearstamp()
method.
02:46
So notice that onscreen at the moment, there is a stamp at the bottom right of the turtle’s path, and we can clear that first stamp, which was ID 41
by using t.clearstamp(41)
.
03:00
Cloning a turtle. It’s possible to clone a turtle from an existing turtle using the .clone()
method. The new turtle will be a clone of the original, down not only to its appearance and current position but even its history.
03:16
This can make setting up a new turtle easy, and also allow you to experiment with one turtle
while preserving another to use in a different way from a given part in your program.
03:32 Now that you’re approaching mastery of the behavior of your turtle, in the next section you’ll take a look at some programming techniques that will allow you to create shorter programs that will be easier to understand, avoid repetition, and allow your code to make decisions.
Become a Member to join the conversation.