Turtle Graphics
Turtle Commands
Commands are how we tell Tracy the Turtle to do things.
Tracy knows several built-in commands.
The forward command makes Tracy move forward a given distance
The backward command makes Tracy move backward a given distance
Negative values can also be used to move Tracy forward or backward
Tracy is positioned in an (x, y) coordinate plane like so:
Each point in Tracy's world can be located by an (x, y) coordinate. For example:
- The center of the world is (0, 0)
- The top middle of the world is (0, 200)
- The bottom left of the world is (-200, -200)
- And so on...
We can use the
setposition
or
goto
commands to send Tracy to a specific location
We can also set only Tracy's x or y location using
setx
or
sety
We have a few command options when we want to turn Tracy to face a new direction.
The
left(degrees)
command will turn Tracy to the left a set number of degrees.
The
right(degrees)
command will turn Tracy to the right a set number of degrees.
Negative values can also be used to turn Tracy right or left
We can use the
setheading
command (or
seth
) to set Tracy to a specific direction
The circle command makes Tracy draw a circle above her with specified radius
Another parameter can be used to draw only certain portions of a circle
An additional parameter can be used to control the number of points in the shape
When the
penup
command is used, Tracy will not leave a trail as she moves
When the
pendown
command is used, Tracy will leave a trail when she moves
Note: Tracy always starts with the pen down!
Controlling the Pen Size
We can use the
pensize
command to set the size of the pen
We can set Tracy's speed using the
speed
command.
Note: Speed options go from 0 to 10.
The
color
command can be used to change the color of Tracy and her trail while the
bgcolor
command is used to change the color of the canvas. Find a list of color acceptable names at
this website.
Hexcodes can also be used to set a larger array of colors. This
hex color picker website can be used to generate hex color codes.
You can also set the color of the background using the
bgcolor
command.
The
begin_fill()
and
end_fill()
commands can be called around a set of movement commands to fill in the shape being drawn.
By default, the cursor will be set to Tracy's shape which is a turtle. We can use the
shape
command to set the cursor to a different shape.
The shape options that can be used are:
- "arrow"
- "turtle"
- "circle"
- "square"
- "triangle"
- "classic"
To clear the screen of any markings, use the
clear
command, which can be used anytime throughout the code.
Loops help us repeat commands which makes our code much shorter.
Make sure everything inside the loop is
indented one level!
Use for-loops when you want to repeat something a fixed number of times.
You can use the
i
variable inside the loop.
i
starts at 0, and goes up to COUNT-1.
You can also control the value of
i
by using extended parameters.
for i in range(STARTING_VALUE, ENDING_VALUE, INCREMENT):
Note: The ending value is not included.
Use while loops when you want to repeat something an unknown number of time or until a condition becomes false.
If there is no point where the condition becomes false, you will create an infinite loop which should always be avoided!
You can also use user input to control a while loop
One condition that we can use is
while True:
which, on its own, would create an infinite loop, which we do not want.
If we pair this condition with a
break
statement somewhere inside the loop, then there is another way to exit the loop and the infitie loop can be avoided.
Note: The
break
statement is
not followed by a set of parentheses.
Writing a function is like teaching Tracy the Turtle a new word.
Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function name. Instead of spaces, use underscores ( _ ):
like_this_for_example
Make sure that all the code inside your function is
indented one level!
We define a function to teach Tracy the instructions for the new word.
We use parameters to alter certain commands in our function.
We call a function to get Tracy to actually carry out the new command.
We can use the
return
keyword to return a value from a function back to the code it was called from.
We can use this returned value right in our commands:
We use variables to store values that can be used to control commands in our code. The variable values can also be altered throughout the code.
Note: For more information on
shortcut operators, check out
this article!
The data we store in variables will most likely fit into one of the following types:
- String (str): A character of group of characters
- Integer (int): A whole number
- Floating Point Number (float): A decimal value
- Boolean (bool): A True or False value
We can turn one type into another (as long as it is possible) using something called
casting.
We can also use the
type
function to determine the type of a variable.
We can tell Tracy how to make decisions using if/else statements.
Make sure that all the code inside your if/else statement is
indented one level!
Use an if statement to tell Tracy to do something only when a condition is true. If the condition is false, Tracy will skip the commands indented underneath.
Use an if/else statement to force Tracy to make a decision between multiple conditions. If the first condition is false, Tracy will skip to the next condition until she finds one that is true. If no conditions are true, the commands inside the 'else' block will be performed.
The
print
function can be used to print text to the console
(the box underneath the canvas).
Different values can be combined inside our print statements. We can use the + operator to combine them which is called
concatenation. We can also use the
format
method to combine values.
Note: All of the text being combined must be of the same type.
To add text to the canvas
(the box where Tracy draws), we use the
write
function. The label for the text is given as a parameter, with optional parameters to change the font attributes or location of the text.
Some fonts that can be used are:
- "Arial"
- "Courier New"
- "Georgia"
- "Times New Roman"
- "Trebuchet MS"
- "Verdana"
- "Futura"
- "Comic Sans MS"
- "Monospace"
- "Cursive"
- "Fantasy"
The alignment attributes that can be used are:
Note: These values discuss where Tracy is located relative to the text. If you set the alignment to "right", Tracy will be on the right side of the text.
We can use methods to alter the way text is written.
Some methods we can use are:
- capitalize: Capitalize first letter of string and make all subsequent letters lowercase
- upper: capitalize every letter in a string
- lower: make every letter in a string lowercae
We also have some string methods that can be used in conditionals based on the values inside a string.
Some conditional string methods are:
- isalpha: returns True if all characters are letters a-z or A-Z
- isdigit: returns True if all characters are numbers 0-9
- isnumeric: returns True if all characters are numbers 0-9
- isupper: returns True if all letters in string are uppercase
- islower: returns True if all letters in string are lowercase
- startswith(value): returns True if the string starts with the given value
- endswith(value): returns True if the string ends with the given value
We use comments to leave notes about the code to the reader. Comments are not actually run by Python, they are just there to help us read the code.
We can make multiline comments with
"""
and single line comments with
#