Draw colorful spirals with turtle in python

Table of contents


The library used for drawing is turtle, and the following program is used to draw the spiral line

draw one

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()

Display the result:
insert image description here

draw two

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()

The result shows:
insert image description here
Reference: https://www.jb51.net/article/235201.htm

Guess you like

Origin blog.csdn.net/ximu__l/article/details/129190385