In Python’s Turtle module, you can control how the turtle draws on the screen using penup()
, pendown()
, and color()
. You can fill shapes with a color using begin_fill()
, end_fill()
and fillcolor()
.
penup()
: This command tells the turtle to lift its pen off the screen. After calling penup()
, the turtle will move without drawing any lines.pendown()
: This command lowers the pen back onto the screen. Once you’ve called pendown()
, the turtle will draw lines again as it moves.color()
: This command changes the color of the turtle’s pen. You can set it to common color names like 'red'
, 'blue'
, or 'green'
.
begin_fill()
: This command tells the turtle to start keeping track of the shape you are drawing so it can fill it with color later.end_fill()
: This command tells the turtle to stop keeping track and fill the shape you’ve drawn with the current fill color. The fill color is the same as the pen color unless you specify a different fill color using fillcolor()
.fillcolor()
: This allows you to choose a color for the inside of a shape, which can be different from the outline color set by color()
.Examples:
Using penup()
and pendown()
:
import turtle as t
t.forward(50) # Draws a line 50 units long
t.penup() # Lifts the pen up
t.forward(50) # Moves forward 50 units without drawing
t.pendown() # Puts the pen down
t.forward(50) # Draws another line 50 units long
In this example, the turtle draws a line, moves without drawing, and then draws another line.
Changing the pen color with color()
:
import turtle as t
t.color('red') # Sets the pen color to red
t.forward(100) # Draws a red line
t.penup()
t.forward(50) # Moves forward 50 units without drawing
t.pendown()
t.color('blue') # Changes the pen color to blue
t.forward(100) # Draws a blue line
This example shows how to change the pen color and draw lines in different colors.
Drawing a filled circle with begin_fill()
, end_fill()
and fillcolor()
:
import turtle as t
t.color('red') # Sets the pen color to red
t.fillcolor("yellow") # Sets the fill color to yellow
t.begin_fill() # Start filling the shape
t.circle(50) # Draws a circle with a radius of 50 units
t.end_fill() # Fill the circle with yellow color
This example shows how to draw and fill a circle.
By using penup()
, pendown()
, and color()
, you can create drawings where the turtle moves around the screen, drawing lines only when you want it to, and in the colors you choose. By using begin_fill()
and end_fill()
, you can easily create filled shapes like circles, squares, and other polygons.
If you want to experiment, you can do it online on trinket.io/turtle1.
Command | Explanation |
---|---|
penup() |
The pen is lifted from the canvas and doesn’t draw when moving. |
pendown() |
The pen is pressed against the canvas and draws when moving. |
color("c") |
Choose the colour named c . |
begin_fill() |
Start colouring a shape. |
end_fill() |
Stop colouring a shape. |
fillcolor("c") |
Choose the colour to fill the inside of a shape named c . |
stamp() |
Show where the turtle is. |
Use these sources to find more information: