填充颜色使用的默认模式1.0,所以就直接用random随机函数,如果turtle.colormode(255)则请使用randint函数,或者直接自定定好颜色列表导入
# encoding:utf-8import turtlefrom random import randomdef 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()returnif __name__ == '__main__':t = turtle.Turtle()t.hideturtle()t.up()t.goto(-130, 50)t.down()t.pensize(3)# 黄金比例scale = 0.618# 初始半径r = 50for _ in range(5):draw_square(r)r /= scaleturtle.mainloop()