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:

  1. Import the Turtle library.
import turtle
  1. Create a Turtle object.
t = turtle.Turtle()
  1. start filling the shape
t.begin_fill()
  1. Draw a shape, such as drawing a square.
for _ in range(4):
    t.forward(100)
    t.right(90)
  1. filling complete
t.end_fill()
t.fillcolor("green")  # 设置填充颜色为绿色
  1. 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.

广告
Closing in 10 seconds
bannerAds