Pen¶
import introcs.turtle
A graphics pen is like a Turtle
except that it does not have a heading, and
there is no drawmode
attribute. Instead, the pen relies on explicit drawing commands
such as drawLine()
or drawOval()
.
Another difference with the pen is that it can draw solid shapes. The pen has an
attribute called solid
. When this attribute is set to True
, it will fill the
insides of any polygon traced by its drawLine()
method. However, the fill will not
be completed until solid is set to False, or the move()
method is invoked.
Each pen is attached to a Window
upon creation, and this window cannot
be change. If the window is closed or deleted, the pen 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.Pen(screen, position=(0, 0), edgecolor='black', fillcolor='red', speed=10)¶
An instance represents a graphics pen.
The pen is attached to a window on creation, and this window cannot be changed. If the window is closed or deleted, the pen 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 pen position (origin is screen center)edgecolor (
RGB
,HSV
orstr
) – initial edge color (default black)fillcolor – initial fill color (default red)
speed (
int
0..10) – initial pen speed (default 10)
Mutable Attributes¶
- Pen.speed¶
The animation speed of this pen.
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.
- Pen.solid¶
The solid status of this pen.
If the solid status is True, then the pen will fill the insides of any polygon or oval subsequently traced by its
drawLine()
anddrawOval()
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
.
- Pen.edgecolor¶
The outline 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
solid
attribute is True). See the attributefillcolor
for solid shapes.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'
).
- Pen.fillcolor¶
The fill color of this pen.
The fill color is used for filling in solid shapes. If the
solid
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
edgecolor
for the border color.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'
).
- Pen.stroke¶
The stroke width of this pen.
By default, the pen 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
- Pen.dash¶
The dash pattern of this pen.
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. The dash only applies to lines and borders. The interior of solid shapes are not dashed.Invariant: Value must be
None
or a non-empty tuple of positive integers.
- Pen.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 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.
- Pen.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
- Pen.y¶
The y-coordinate of this pen.
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.
- Pen.move(x, y)¶
Moves the pen to given position without drawing.
If the
solid
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.- Parameters
x (
int
orfloat
) – new x position for turtley (
int
orfloat
) – new y position for turtle
- Pen.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)
- Parameters
dx (
int
orfloat
) – change in the x positiondy (
int
orfloat
) – change in the y position
- Pen.drawTo(x, y)¶
Draws a line from the current pen position to (x,y)
When done, the pen will be at (x, y).
- Parameters
x (
int
orfloat
) – finishing x position for liney (
int
orfloat
) – finishing y position for line
- Pen.drawOval(xradius, yradius)¶
Draws a oval with the given radii.
The center of the circle is the current pen coordinates. When done, the position of the pen will remain unchanged.
If
solid
is true, this will fill the shape when done.- Parameters
xradius (
int
orfloat
) – radius of the x-axisyradius (
int
orfloat
) – radius of the y-axis
- Pen.drawRectangle(width, height)¶
Draws a rectangle with the given width and height.
The current pen coordinates are the bottom left corner of the rectangle. When done, the position of the pen will remain unchanged.
If
solid
is true, this will fill the shape when done.- Parameters
width (
int
orfloat
) – the rectangle widthheight (
int
orfloat
) – the rectangle height
Update Methods¶
All of these methods are used to update the status of the associated Window
- Pen.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.
- Pen.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.
- Pen.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.