在 Python Turtle 中绘制颜色填充的形状

2025-11-02 07:48:237326

按照以下步骤绘制具有所需颜色的填充形状-

通过调用fillcolor()函数选择填充颜色并以#RRGGBB 格式传递颜色名称或颜色。在第 1 步之后,您必须调用begin_fill()并使用 Turtle 函数开始绘图。完成绘图后,调用end_fill()函数以使用所选颜色填充绘制的图形。在 Python Turtle 中绘制颜色填充的正方形

#Python program to draw color filled square in turtle programming

import turtle

t = turtle.Turtle()

t.fillcolor('blue')

t.begin_fill()

for i in range(4):

t.forward(150)

t.right(90)

t.end_fill()

上述程序的输出

在 Python Turtle 中绘制颜色填充三角形

#Python program to draw color filled triangle in turtle programming

import turtle

t = turtle.Turtle()

t.fillcolor('#FFA500')

t.begin_fill()

for i in range(4):

t.forward(150)

t.right(-120)

t.end_fill()

上述程序的输出

在 Python Turtle 中绘制颜色填充的星星

#Python program to draw color filled star in turtle programming

import turtle

t = turtle.Turtle()

t.fillcolor('orange')

t.begin_fill()

for i in range(5):

t.forward(150)

t.right(144)

t.end_fill()

上述程序的输出

在 Python Turtle 中绘制颜色填充的六边形

#Python program to draw color filled hexagon in turtle programming

import turtle

t = turtle.Turtle()

t.fillcolor('orange')

t.begin_fill()

for i in range(6):

t.forward(150)

t.right(60)

t.end_fill()

上述程序的输出

在 Python Turtle 中绘制彩色填充圆圈

#Python program to draw color filled circle in turtle programming

import turtle

t = turtle.Turtle()

t.fillcolor('orange')

t.begin_fill()

t.circle(100)

t.end_fill()

上述程序的输出