填充颜色使用的默认模式1.0,所以就直接用random随机函数,如果turtle.colormode(255)则请使用randint函数,或者直接自定定好颜色列表导入
# encoding:utf-8
import turtle
from random import random
def draw_square(r):
t.fillcolor(random(), random(), random())
t.begin_fill()
for _ in range(4):
t.fd(r)
t.left(90)
# 填充关键点,正方形填充一半就结束,然后立即开始圆形填充
if _ == 1:
t.end_fill()
t.fillcolor(random(), random(), random())
t.begin_fill()
t.circle(r, 90)
t.end_fill()
return
if __name__ == '__main__':
t = turtle.Turtle()
t.hideturtle()
t.up()
t.goto(-130, 50)
t.down()
t.pensize(3)
# 黄金比例
scale = 0.618
# 初始半径
r = 50
for _ in range(5):
draw_square(r)
r /= scale
turtle.mainloop()