📌  相关文章
📜  在 Turtle 中绘制彩色填充形状 - Python

📅  最后修改于: 2022-05-13 01:55:06.411000             🧑  作者: Mango

在 Turtle 中绘制彩色填充形状 - Python

先决条件: Python Turtle 基础

turtle是Python中的一个内置模块。它使用屏幕(纸板)和海龟(笔)提供绘图。要在屏幕上绘制一些东西,我们需要移动海龟。要移动海龟,有一些函数,即forward()backward()等。

为了填充 turtle 绘制的形状中的颜色,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

输出 :