绘图所用的库是turtle,下面用程序来绘制螺旋线
绘制一
import turtle
turtle.speed(10) # 速度调最快
turtle.pensize(2)
turtle.bgcolor("white")
colors = ["red", "yellow", "purple", "blue","green"]
# turtle.tracer(False)
for x in range(700):
turtle.forward(x + 1)
turtle.color(colors[x % 5])
# 调节绘制方向
turtle.left(91)
turtle.done()
展示结果:
绘制二
from turtle import *
hideturtle() # 隐藏画笔
title("螺旋线") # 设置标题
speed(10) # 速度调最快
colormode(255) # 将RGB颜色值设置成整数输入
r = 255
g = 0
b = 0
for i in range(51):
color(r, g, b)
fd(i)
left(91)
g += 5 # 颜色由红色变为黄色(将绿色值增加),这里的5不是随便取的,是为了与循环次数51相乘得255
for i in range(51):
color(r, g, b)
fd(51 + i)
left(91)
r -= 5 # 颜色变为绿色(红色值减少)
for i in range(51):
color(r, g, b)
fd(51 * 2 + i)
left(91)
b += 5 # 颜色变为天蓝色(蓝色值增加)
for i in range(51):
color(r, g, b)
fd(51 * 3 + i)
left(91)
g -= 5 # 变为深蓝(绿色值减少)
for i in range(51):
color(r, g, b)
fd(51 * 4 + i)
left(91)
r += 5 # 变紫(红色值增加)
for i in range(51):
color(r, g, b)
fd(51 * 5 + i)
left(91)
b -= 5 # 变红,后面省略
for i in range(51):
color(r, g, b)
fd(51 * 6 + i)
left(91)
g += 5
for i in range(51):
color(r, g, b)
fd(51 * 7 + i)
left(91)
r -= 5
for i in range(51):
color(r, g, b)
fd(51 * 8 + i)
left(91)
b += 5
for i in range(51):
color(r, g, b)
fd(51 * 9 + i)
left(91)
g -= 5
for i in range(51):
color(r, g, b)
fd(51 * 10 + i)
left(91)
r += 5
done()
结果显示:
参考:https://www.jb51.net/article/235201.htm