Class Pen

cornell.Pen(screen, position=(0, 0), color='red', speed=0)

Constructor: creates a new pen to draw on the given screen.

The color will be assigned to both the pencolor and the fillcolor.

Parameter:
  • screen (Window) – window object that pen will draw on.
  • position (2D tuple) – initial pen position (origin is screen center)
  • color (see color) – initial pen color (default red)
  • heading (int or float) – initial pen directions (default 180)
  • speed (int 0..10) – initial pen speed (default 0)

Attributes

pencolor

The pen color of this pen.

The pen color is used for drawing lines and circles. All subsequent draw commands draw using this color. If the color changes, it only affects future draw commands, not past ones.

This color is only used for lines and the border of circles. It is not the color used for filling in solid areas (if the fill attribute is True). See the attribute fillcolor for solid shapes.

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

fillcolor

The fill color of this turtle.

The fill color is used for filling in solid shapes. If the fill attribute is True, all subsequent draw commands fill their insides using this color. If the color changes, it only affects future draw commands, not past ones.

This color is only used for filling in the insides of solid shapes. It is not the color used for the shape border. See the attribute pencolor for the border color.

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

fill

The fill status of this pen.

If the fill status is True, then the pen will fill the insides of any polygon or circle subsequently traced by its drawLine() and drawCircle() method. If the attribute changes, it only affects future draw commands, not past ones. Switching this attribute between True and False allows the pen to draw both solid and hollow shapes.

invariant: Value must be an bool.

speed

The animation speed of this pen.

The speed is an integer from 0 to 10. Speed = 0 means that no animation takes place. The drawLine() and drawCircle() methods happen instantly with no animation.

Speeds from 1 to 10 enforce increasingly faster animation of line drawing. 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 pen’s icon is visible.

Drawing commands will still work while the pen icon is hidden. There will just be no indication of the pen’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 pen.

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

This attribute may not be (directly) altered

y

The y-coordinate of this pen.

To change the y coordinate, use one of the drawing methods.

This attribute may not be (directly) altered

Methods

Drawing Methods

drawLine(dx, dy)

Draws a line segment (dx,dy) from the current pen position

The line segment will run from (x,y) to (x+dx,y+dy), where (x,y) is the current pen position. When done, the pen will be at position (x+dx,y+dy)

Parameter:
  • dx (int or float) – change in the x position
  • dy (int or float) – change in the y position
drawCircle(r)

Draws a circle of radius r centered on the pen.

The center of the circle is the current pen coordinates. When done, the position of the pen will remain unchanged

Parameter:r (int or float) – radius of the circle
drawTo(x, y)

Draws a line from the current pen position to (x,y)

When done, the pen will be at (x, y).

Parameter:
  • x (int or float) – finishing x position for line
  • y (int or float) – finishing y position for line

Other Methods

move(x, y)

Moves the pen to given position without drawing.

If the fill attribute is currently True, this method will complete the fill before moving to the new region. The space between the original position and (x,y) will not be connected.

Parameter:
  • x (int or float) – new x position for turtle
  • y (int or float) – new y position for turtle
clear()

Deletes the pen’s drawings from the window.

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

reset()

Deletes the pen’s drawings from the window.

This method re-centers the pen and resets all attributes to their defaults.