Python Turtle Functions
The Python turtle module has functions and methods for graphics program development. Turtle Functions relate to animation control, moving, drawing, etc.
Turtle Module Functions
In addition to the turtle module functions we presented on the Turtle Graphics Basic page, we will use the following additional turtle module functions (methods) for turtle graphics programming and game development.
You can learn all the details of the methods available in the turtle module by typing help(‘turtle’)
from the Python prompt.
Move and Draw Methods
We learned the Move and Draw functions in the Turtle Graphics Basic chapter. The following are a few additional Move and Draw methods.
Heading Control:
FUNCTION | DESCRIPTION |
---|---|
turtle.setheading(to_angle) | Set the orientation of the turtle to to_angle. Example: >>> turtle.setheading(90) |
Here are some common directions in degrees:
TO_ANGLE (DEGREES) | DIRECTION |
---|---|
0 | East |
90 | North |
180 | West |
270 | South |
Turtle.stamp() Function:
In some animation applications, you would like to show where the turtle was in the previous frames to display the track that the turtle took.
You have the option of leaving a stamp of your turtle on the screen (an imprint of the turtle) by using turtle.stamp() function.
FUNCTION | DESCRIPTION |
---|---|
turtle.stamp() | Stamp() method: A turtle can “stamp” its footprint onto the canvas, and this will remain after the turtle has moved somewhere else. Stamping works even when the pen is up. |
Window Control Methods
FUNCTION | DESCRIPTION |
---|---|
turtle. bgcolor (*args) | args – a color string or a tuple of three numbers. Example: screen.bgcolor(“orange”) screen.bgcolor(127, 127, 145) |
turtle .screensize (canvwidth=None, canvheight=None, bg=None) | canvwidth – positive integer, new width of canvas in pixels canvheight – positive integer, new height of canvas in pixels bg – colorstring or color-tuple, new background color |
Methods for Screen
FUNCTION | DESCRIPTION |
---|---|
exitonclick() | Exits the program |
Screen.setup(width=number, height=number, startx=number, starty=number) | Set the size and position of the main window. width – the size in pixels height – the height in pixels startx – starting position in pixels from the left edge of the screen starty – starting position in pixels from the top edge of the screen Example: The following sets window size to 200×200 pixels. turtle.setup (width=200, height=200, startx=0, starty=0) |
Screen.title(titlestring) | You can give a title to your screen using the title() function. Set title of turtle window to titlestring. titlestring – a string that is shown in the title bar of the turtle graphics window. >>> screen.title ("Turtle RACE") |
User Screen Events Methods
FUNCTION | DESCRIPTION |
---|---|
turtle.onkey(fun, key) | Bind fun to key-release event of key. If fun is None, event bindings are removed. fun is a function written by the user. fun = a function with no arguments or None . Key = a string: key (e.g. “a”) or key-symbol (e.g. “space” or “left-arrow”). |
turtle.listen() | Listen to and collect keyboard events. |
Example:
# Define function to turn turtle left by 45 degrees.
def turnLeft():
turtle.left(45)
# Set up Keyboard bindings
# Set focus on TurtleScreen to collect key-events.
turtle.listen()
# Define Left-arrow key as the key to bind to function "turnLeft"
# Bind function "turnLeft" to key press event of "left-arrow' key.
turtle.onkey(turnLeft, "Left")
Animation Control Turtle Functions
FUNCTION | DESCRIPTION |
---|---|
turtle.delay(delay) | Parameters: delay = a positive number Set the drawing delay in milliseconds. This is approximately the time interval between two consecutive canvas updates. If delay is longer, the animation is slower. |
turtle.tracer(n,delay) | Turns turtle animation on/off and set delay for update drawings. Parameters: n = non-negative integer. Each n-th regular screen update is performed. delay = non-negative integer, sets delay value. |
turtle.update() | Perform a Turtle Screen update. Used when tracer is turned off. Screen.update() tells Python to refresh the window with the new drawing. This method is used whenever you want the screen to be updated with the latest version of your drawing. |
Note: The screen.tracer(0)
turns animation off. In animation applications, we need to ensure that a frame is shown only after it has been completed. This can be done by first telling the screen to not show anything automatically using the screen.tracer(0)
function. Then, tell Python to show the new frame only after the new frame has been completed using the screen.update()
function.
ontimer() Turtle Function
FUNCTION | DESCRIPTION |
---|---|
window.ontimer(fun, time) | The Screen object function, ontimer allows a function to be executed some specified time (in milliseconds) later. fun = Function to be executed. time = fun will be executed time milliseconds later. Example: The following code would call a function ‘my_fun’ 200 milliseconds later. window.ontimer(my_fun, 200) |
turtle.speed() Turtle Functions:
The turtle module provides a function to control the speed at which the turtle moves on the screen. You can speed up or slow down the turtle’s animation speed using the turtle.speed() function. This function controls how quickly the turtle turns and moves forward.
FUNCTION | DESCRIPTION |
---|---|
turtle.speed() | Speed settings can be set between 1 (slowest) to 10 (fastest). If you set the speed to 0, it has a special meaning. It turns off animation and go as fast as possible. |
Turtle State Methods – Visibility Control Turtle Functions:
FUNCTION | DESCRIPTION |
---|---|
turtle.showturtle() turtle.st() | Make the turtle visible. |
turtle.hideturtle() turtle.ht() | Hide the turtle. |
Write() Function:
You can write text on the screen using the write() function as per details below.
FUNCTION | DESCRIPTION |
---|---|
turtle.write() | Parameters: arg = object to be written align = one of the strings “left”, “center” or right” font = (fontname, fontsize, fonttype) Example: >>> turtle.write("TURTLE RACE", font = ("calibri", 36, "bold")) |
Below is a link for the turtle module’s methods:
<a href=”https://runestone.academy/ns/books/published/fopp/PythonTurtle/SummaryOfTurtleMethods.html“>
Copyright © 2019 – 2024 softwareprogramming4kids.com
All Rights Reserved