How to fill colors in Python’s turtle module?
In Python, you can use the begin_fill() and end_fill() methods from the Turtle library to fill colors. Here are the specific steps:
- Import the Turtle library.
import turtle
- Create a Turtle object.
t = turtle.Turtle()
- start filling the shape
t.begin_fill()
- Draw a shape, such as drawing a square.
for _ in range(4):
t.forward(100)
t.right(90)
- filling complete
t.end_fill()
t.fillcolor("green") # 设置填充颜色为绿色
- completed
turtle.done()
The complete sample code is shown below:
import turtle
t = turtle.Turtle()
t.begin_fill()
for _ in range(4):
t.forward(100)
t.right(90)
t.end_fill()
t.fillcolor("green")
turtle.done()
In the code above, a square with a side length of 100 is drawn and filled with green color. You can customize the drawing effect by modifying parameters such as shape, size, and fill color as needed.