Class Turtle¶
-
cornell.
Turtle
(screen, position=(0, 0), color='red', heading=180, speed=0)¶ Constructor: creates a new turtle to draw on the given screen.
Parameter: - screen (
Window
) – window object that turtle will draw on. - position (2D
tuple
) – initial turtle position (origin is screen center) - color (see
color
) – initial turtle color (default red) - heading (
int
orfloat
) – initial turtle directions (default 180) - speed (
int
0..10) – initial turtle speed (default 0)
- screen (
Attributes¶
-
heading
¶ The heading of this turtle in degrees.
Heading is measured counter clockwise from due east.
invariant: Value must be a
float
-
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 a string with a color name, a 3 element tuple of floats between 0 and 1 (inclusive), or an object in an additive color model (e.g. RGB or HSV).
-
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
-
speed
¶ The animation speed of this turtle.
The speed is an integer from 0 to 10. Speed = 0 means that no animation takes place. The methods forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning. 1 is the slowest speed while 10 is the fastest (non-instantaneous) speed.
invariant: Value must be an
int
in the range 0..10.
-
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
Immutable Attributes¶
These attributes may be read (e.g. used in an expression), but not altered.
-
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
-
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
Methods¶
Drawing Methods¶
-
forward
(distance)¶ Moves the turtle forward by the given amount.
This method draws a line if drawmode is True.
Parameter: distance ( int
orfloat
) – distance to move in pixels
-
backward
(distance)¶ Moves the turtle backward by the given amount.
This method draws a line if drawmode is True.
Parameter: distance ( int
orfloat
) – distance to move in pixels
-
right
(degrees)¶ Turns the turtle to the right by the given amount.
Nothing is drawn when this method is called.
Parameter: degrees ( int
orfloat
) – amount to turn right in degrees
-
left
(degrees)¶ Turns the turtle to the left by the given amount.
Nothing is drawn when this method is called.
Parameter: degrees ( int
orfloat
) – amount to turn left in degrees
Other Methods¶
-
move
(x, y)¶ Moves the turtle to given position without drawing.
This method does not draw, regardless of the drawmode.
Parameter: - x (
int
orfloat
) – new x position for turtle - y (
int
orfloat
) – new y position for turtle
- x (
-
clear
()¶ Deletes the turtle’s drawings from the window.
This method does not move the turtle or alter its attributes.
-
reset
()¶ Deletes the turtle’s drawings from the window.
This method re-centers the turtle and resets all attributes to their defaults.