在Python中使用 Turtle 用圆圈着色花
先决条件:海龟编程基础
Turtle是Python中的一个内置模块。它使用屏幕(纸板)和海龟(笔)提供绘图。要在屏幕上绘制一些东西,我们需要移动海龟(笔)。要移动海龟,有一些函数,即 forward()、backward() 等。
画花:
Following steps are used :
- Import Turtle
- Set screen
- Make Turtle Object
- Define colors used for drawing
- Loop to draw circles oriented by angle.
下面是实现:
Python3
# import turtle package
import turtle
# screen object
sc = turtle.Screen()
# turtle object
pen = turtle.Turtle()
# Driver Code
# colors
col = ['violet', 'indigo', 'blue', 'green',
'yellow', 'orange', 'red']
# screen size
sc.setup(500,500)
# screen color
sc.bgcolor('black')
# turtle size
pen.pensize(4)
# turtle object
pen.speed(20)
# integer variable
# for accessing colors
i = 0
# loop to draw 30 circles
for angle in range(0, 360, 12):
# color of circle
pen.color(col[i])
if i == 6:
i = 0
else:
i += 1
# Set the orientation of
# the turtle to angle
pen.seth(angle)
# circle of radius 80
pen.circle(80)
# hide the turtle
pen.ht()