Class Turtle

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

Instance is a graphics turtle.

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: Create 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.

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

param speed:initial turtle speed (default 0)

Precondition: a int between 0 and 10, inclusive

The argument screen is not optional.

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

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

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.

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.

x

The x-coordinate of this turtle.

To change the x coordinate, use of the drawing methods.

This attribute may not be (directly) altered

y

The y-coordinate of this turtle.

To change the x coordinate, use of the drawing methods.

This attribute may not be (directly) altered

Methods

Drawing Methods

forward(distance)

Move the turtle forward.

param distance:distance to move in pixels

Precondition: a float

Draws a line if drawmode is True.

backward(distance)

Move the turtle backward.

param distance:distance to move in pixels

Precondition: a float

Draws a line if drawmode is True.

right(degrees)

Turn the turtle to the right.

param degrees:amount to turn right in degrees

Precondition: a float

Nothing is drawn.

left(degrees)

Turn the turtle to the left.

param degrees:amount to turn left in degrees

Precondition: a float

Nothing is drawn.

Other Methods

move(x, y)

Moves the turtle to given position without drawing.

param x:new x position for turtle

Precondition: a float

param y:new y position for turtle

Precondition: a float

This method does not draw, regardless of the drawmode.

clear()

Delete the turtle’s drawings from the window, but do not move the turtle or alter its attributes.

reset()

Delete the turtle’s drawings from the window, re-center the turtle and reset all attributes to their default values.

Table Of Contents

Previous topic

Class Window

Next topic

Class Pen

This Page