Class Turtle

class tkturtle.Turtle(screen, position=(0, 0), color='red', heading=180, speed=0)

Instances represent a graphics 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.

Constructor: Creates a new turtle to draw on the given screen.

param screen:window object that turtle will draw on.

Precondition: object of type Window.

param position:initial turtle position (origin is screen center)

Precondition: 2D tuple of floats or ints.

param color:initial turtle color (default red)

Precondition: 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).

param heading:initial turtle directions (default 180)

Precondition: a float or int

param speed:initial turtle speed (default 0)

Precondition: a int between 0 and 10, inclusive

The argument screen is not optional.

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.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).

Turtle.drawmode

Indicates 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

Turtle.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 integer value in the range 0..10.

Turtle.visible

Indicates 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.

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

Methods

Drawing Methods

Turtle.forward(distance)

Moves the turtle forward by the given amount.

param distance:distance to move in pixels

Precondition: a float or int

This method draws a line if drawmode is True.

Turtle.backward(distance)

Moves the turtle backward by the given amount.

param distance:distance to move in pixels

Precondition: a float or int

This method draws a line if drawmode is True.

Turtle.right(degrees)

Turns the turtle to the right by the given amount.

param degrees:amount to turn right in degrees

Precondition: a float or int

Nothing is drawn when this method is called.

Turtle.left(degrees)

Turns the turtle to the left by the given amount.

param degrees:amount to turn left in degrees

Precondition: a float or int

Nothing is drawn when this method is called.

Other Methods

Turtle.move(x, y)

Moves the turtle to given position without drawing.

param x:new x position for turtle

Precondition: a float or int

param y:new y position for turtle

Precondition: a float or int

This method does not draw, regardless of the drawmode.

Turtle.clear()

Deletes the turtle’s drawings from the window.

This method does not move the turtle or alter its attributes.

Turtle.reset()

Deletes the turtle’s drawings from the window.

This method re-centers the turtle and resets all attributes to their default values.