14.1. Turtle Functions and Procedures¶
Learning Objectives:
Introduce more turtle functions and procedures.
Show examples using conditionals with turtles.
Use the modulo operator to detect odd and even
Show how to generate random numbers in Python
Use conditionals to avoid a collision
Turtles can do more than go forward, turn left, and turn right. The table below lists turtle functions and procedures.
Name |
Parameters |
Description |
---|---|---|
backward |
amount |
Moves the turle backward by the specified amount |
color |
colorname |
Sets the color for drawing. Use ‘red’, ‘black’, etc |
forward |
amount |
Moves the turtle forward by the specified amount |
goto |
x,y |
Moves the turtle to position x,y |
left |
angle |
Turns the turtle counter clockwise by the specified angle |
pendown |
None |
Puts down the turtles tail so that it draws when it moves |
penup |
None |
Picks up the turtles tail so that it doesn’t draw when it moves |
pensize |
width |
Sets the width of the pen for drawing |
right |
angle |
Turns the turtle clockwise by the specified angle |
setheading |
angle |
Turns the turtle to face the given heading. East is 0, north is 90, west is 180, and south is 270. |
shape |
shapename |
Should be ‘arrow’, ‘classic’, ‘turtle’, or ‘circle’ |
stamp |
None |
Leaves an impression of a turtle shape at the current location |
Turtle |
None |
Creates and returns a new turtle object |
xcor |
None |
Returns the x value of the turtle’s position |
ycor |
None |
Returns the y values of the turtle’s position |
- shape
- Use shape to set the shape used for the turtle. It doesn't affect the line that is drawn.
- xcor
- Use this function to get the x value of the turtle's position
- pensize
- This changes the width of the line that the turtle draws as it moves.
- color
- Use color to change the line color and the turtle color.
What procedure would you use to change the size of the line the turtle leaves when it moves?
- stamp
- Use stamp to leave a copy of turtle shape at the current position.
- xcor
- Use xcor to get the x value of the turtle's position.
- penup
- Use penup to pick up the pen (stop drawing as you move).
- goto
- Use goto to move to the given x and y position.
What procedure would you use move the turtle to a given x and y position?
Once you are comfortable with the basics of turtle graphics you can read about even more options on the Python Docs Website.