Turtle¶
import introcs.turtle
A graphics turtle is a pen that is controlled by direction and movement. The Turtle
is a cursor that that you control by moving it left, right, forward, or backward.
As it moves, it draws a line of the same color as the Turtle
.
Each turtle is attached to a Window
upon creation, and this window cannot
be change. If the window is closed or deleted, the turtle can no longer be used.
Because of the overhead, this class is not included in the top level of the introcs
module. It must be imported separately as part of the introcs.turtle
module.
Constructor¶
- class introcs.turtle.Turtle(screen, position=(0, 0), color='red', heading=0, speed=10)¶
An instance represents a graphics turtle.
The turtle is attached to a window on creation, and this window cannot be changed. If the window is closed or deleted, the turtle can no longer be used. Any attempt to call a graphics method after the window is disposed will result in an error.
- Parameters
screen (
Window
) – window object that turtle will draw on.position (2D
tuple
) – initial turtle position (origin is screen center)color (
RGB
,HSV
orstr
) – initial turtle color (default red)heading (
int
orfloat
) – initial turtle directions (default 0)speed (
int
0..10) – initial turtle speed (default 10)
Mutable Attributes¶
- Turtle.heading¶
The heading of this turtle in degrees.
Heading is measured counter clockwise from due east.
Invariant: Value must be a
float
- Turtle.speed¶
The animation speed of this turtle.
The speed is an integer from 0 to 10. Speeds from 1 to 10 enforce increasingly faster animation of line drawing and cursor updates. Value 1 is the slowest speed while 10 is the fastest speed. Roughly, speed 1 draws 1 pixel per step, while speed 10 draws an entire line in a single step.
Speed 0 is special. Speed 0 means that no animation takes place at all. The drawing commands will be remembered, but not shown on the screen. To display the drawing, you must call the method
flush()
. When that method is called, all of the drawing commands will be displayed instantly. This is useful for fast drawing.If the speed is currently 0, changing the speed will immediately flush any existing drawing commands.
Invariant: Value must be an
int
in the range 0..10.
- Turtle.color¶
The color of this turtle.
All subsequent draw commands (forward/back) draw using this color. If the color changes, it only affects future draw commands, not past ones.
Invariant: Value must be either an additive color model (e.g. RGB or HSV) or string representing a color name or a web color (e.g.
'#f3CC02'
).
- Turtle.stroke¶
The stroke width of this turtle.
By default, the turtle draws lines that are one pixel wide. Changing this value will increase (or decrease, if your implementation supports sub-pixel graphics) the stroke width.
Invariant: Value must be either a positive
float
- Turtle.dash¶
The dash pattern of this turtle.
A dash pattern is a tuple of integers that specifes the dash in pixels. Only odd values of the pattern are drawn. For example, if the pattern is (10,10), the turtle will draw 10 pixels, and then stop drawing for 10 pixels. After 20 pixels that patterns repeat. Similarly (10,5,5,10) will draw for 10 pixels, stop for 5 pixels, draw for 10 pixels and the stop for 5 pixels before repeating.
If this value is
None
, the line will be solid.Invariant: Value must be
None
or a non-empty tuple of positive integers.
- Turtle.visible¶
Whether the turtle’s icon is visible.
Drawing commands will still work while the turtle icon is hidden. There will just be no indication of the turtle’s current location on the screen.
Invariant: Value must be a
bool
- Turtle.drawmode¶
Whether the turtle is in draw mode.
All drawing calls are active if an only if this mode is True
Invariant: Value must be a
bool
Immutable Attributes¶
These attributes may be read (e.g. used in an expression), but not altered.
- Turtle.x¶
The x-coordinate of this turtle.
To change the x coordinate, use one of the drawing methods.
This attribute may not be (directly) altered
- Turtle.y¶
The y-coordinate of this turtle.
To change the x coordinate, use one of the drawing methods.
This attribute may not be (directly) altered
Drawing Methods¶
All of these methods modify the underlying turtle object.
- Turtle.forward(distance)¶
Moves the turtle forward by the given amount.
This method draws a line if
drawmode
is True.- Parameters
distance (
int
orfloat
) – distance to move in pixels
- Turtle.backward(distance)¶
Moves the turtle backward by the given amount.
This method draws a line if
drawmode
is True.- Parameters
distance (
int
orfloat
) – distance to move in pixels
- Turtle.right(degrees)¶
Turns the turtle to the right by the given amount.
Nothing is drawn when this method is called.
- Parameters
degrees (
int
orfloat
) – amount to turn right in degrees
- Turtle.left(degrees)¶
Turns the turtle to the left by the given amount.
Nothing is drawn when this method is called.
- Parameters
degrees (
int
orfloat
) – amount to turn left in degrees
Update Methods¶
All of these methods are used to update the status of the associated Window
- Turtle.flush()¶
Forces a redraw of the associated
Window
.This is the same as calling
flush()
on the associated window. It is necessary to update the graphics when the turtle speed is 0.
- Turtle.clear()¶
Deletes the turtle’s drawings from the
Window
.This method does not move the turtle or alter its attributes. It is different from the window’s
clear()
method in that no other turtles are affected and the turtle is not removed.
- Turtle.reset()¶
Deletes the turtle’s drawings from the
Window
.This method re-centers the turtle and resets all attributes to their defaults. This method is different from the window’s
clear()
method in that no other turtles are affected and the turtle is not removed.