You change the direction of the turtle in Python using functions like left()
(lt()
) or right()
(rt()
), specifying the turn angle in degrees.
The Python turtle
module provides straightforward commands to control the turtle's movement and orientation on the drawing canvas. To change which way the turtle is facing, you use turning functions.
Methods for Changing Turtle Direction
The primary ways to change the turtle's direction involve turning it by a specific angle.
Turning Left or Right
Based on the provided reference, the most direct ways to change the turtle's direction are by turning it left or right using degrees.
-
Turn Left: Use the
left()
or its shorter aliaslt()
. This function rotates the turtle counter-clockwise by the specified number of degrees.- Syntax:
turtle.left(angle)
orturtle.lt(angle)
- Example:
turtle.left(90)
would turn the turtle 90 degrees to the left.
- Syntax:
-
Turn Right: Use the
right()
or its shorter aliasrt()
. This function rotates the turtle clockwise by the specified number of degrees.- Syntax:
turtle.right(angle)
orturtle.rt(angle)
- Example:
turtle.right(45)
would turn the turtle 45 degrees to the right.
- Syntax:
These functions only change the turtle's orientation; they do not move the turtle's position on the screen.
Here is a summary of these basic turning commands:
Function | Alias | Description | Argument Type |
---|---|---|---|
turtle.left() |
lt() |
Turns the turtle counter-clockwise. | Degrees (int/float) |
turtle.right() |
rt() |
Turns the turtle clockwise. | Degrees (int/float) |
Moving in an Arc with circle()
While not a simple turn-in-place, the circle()
function also causes the turtle to change direction continuously as it moves.
- Draw a Circle/Arc: The
circle()
function moves the turtle forward along an arc. The direction of the curve depends on the radius and the extent (number of degrees) specified. As per the reference, it curves the turtle to the left.- Syntax:
turtle.circle(radius, extent=None, steps=None)
- Example:
turtle.circle(50, 180)
would make the turtle move along a semi-circle with a radius of 50 units, turning as it goes. This effectively changes its direction over time.
- Syntax:
Setting Absolute Direction (setheading
)
Another common method (not explicitly listed in the provided reference but widely used in the turtle
module) is setheading()
or seth()
. This allows you to set the turtle's orientation to an absolute angle (0 is East, 90 is North, 180 is West, 270 is South).
- Set Heading: Use
turtle.setheading(to_angle)
orturtle.seth(to_angle)
. This function immediately points the turtle in the direction ofto_angle
degrees.- Example:
turtle.setheading(0)
points the turtle towards the positive x-axis (right).turtle.setheading(90)
points it towards the positive y-axis (up).
- Example:
Using left()
or right()
is useful for relative turns (e.g., "turn 90 degrees from wherever I am facing"), while setheading()
is useful for absolute turns (e.g., "point directly North").
Changing the turtle's direction is fundamental to drawing shapes and paths in the turtle
module. By combining these turning functions with movement functions like forward()
(fd()
) or backward()
(bk()
), you can control the turtle's path precisely.