在Python Turtle中绘制填充颜色的形状
turtle是python中的一个内置模块。它提供了使用屏幕(纸板)和Turtle(笔)的绘画。为了在屏幕上画东西,我们需要移动Turtle。为了移动Turtle,有一些函数,如forward(), backward()等。
为了在turtle绘制的图形中填充颜色,turtle提供了三个函数–
fillcolor() 。这有助于选择用于填充形状的颜色。它将输入参数作为颜色名称或颜色的十六进制值,并将所选的颜色填充到即将封闭的地理对象中。颜色名称是基本的颜色名称,即红色、蓝色、绿色、橙色。
颜色的十六进制值是一串(以’#’开头)的十六进制数字,即:#RRGGBB。R、G和B是十六进制数字(0、1、2、3、4、5、6、7、8、9、A、B、C、D、E、F)。
begin_fill() 。这个函数告诉Turtle,所有即将关闭的图形对象需要被选定的颜色填充。
end_fill() : 这个函数告诉Turtle停止填充即将关闭的图形对象。
绘制彩色填充的方形
# draw color-filled square in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the side of the square
s = int(input("Enter the length of the side of the square: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the square of side s
for _ in range(4):
t.forward(s)
t.right(90)
# ending the filling of the color
t.end_fill()
输入:
200
green
输出 :
绘制彩色填充的三角形
# draw color filled triangle in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the side of the triangle
s = int(input("Enter the length of the side of the triangle: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the triangle of side s
for _ in range(3):
t.forward(s)
t.right(-120)
# ending the filling of the color
t.end_fill()
输入:
200
red
输出 :
绘制彩色填充的六边形
# draw color-filled hexagon in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the side of the hexagon
s = int(input("Enter the length of the side of the hexagon: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the hexagon of side s
for _ in range(6):
t.forward(s)
t.right(-60)
# ending the filling of the color
t.end_fill()
输入:
100
#113300
输出 :
绘制彩色填充的星星
# draw color filled star in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the side of the star
s = int(input("Enter the length of the side of the star: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the star of side s
for _ in range(5):
t.forward(s)
t.right(144)
# ending the filling of color
t.end_fill()
输入:
200
#551122
输出 :
绘制彩色填充的圆
# draw color filled circle in turtle
import turtle
# creating turtle pen
t = turtle.Turtle()
# taking input for the radius of the circle
r = int(input("Enter the radius of the circle: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the circle of radius r
t.circle(r)
# ending the filling of the color
t.end_fill()
输入:
100
blue
输出 :