Python代码:绘制彩色螺旋

· · 算法·理论

虽然说Python在信息比赛中不常用,但是一门非常方便快捷的语言,利用其中的turtle模块可以绘制许多图形。

这是一个用Python绘制彩色螺旋的代码,分为七种颜色。

import turtle
import time
turtle.bgcolor('black')
turtle.speed(0)
colors = ['red', 'orange', 'yellow','green','DeepSkyBlue','blue', 'purple']

def spiral():
    turtle.speed(0)
    for i in range(420):
        turtle.pencolor(colors[i%7])
        turtle.pensize(1+i/100)
        turtle.fd(i)
        turtle.right(360/7-1)

def spin(b):
    turtle.penup()
    turtle.home()
    turtle.left(b*n)
    turtle.pendown()

n=0
try:
    while True:
        turtle.clear()
        spin(4)
        spiral()
        n=n+1
        turtle.update()
        time.sleep(0.01)
except:
    print('See you')