首页 > 解决方案 > 用 python turtle 重复函数

问题描述

我想知道如何制作函数循环以重新创建具有不同旋转和位置以及不同变量(例如颜色)的相同形状/图案(谷歌照片徽标)。下面是允许我制作具有正确角度但比例不准确的托盘之一的代码。也不想使用任何 goto/home 功能,因为我需要稍后重复此绘图。我应该使用左/右作为方向而不是设置航向吗?

def photo():
    speed(1) # turtle speed (debugging)
    #speed(0)
    length = 50

    penup()
    color("#4688f4") #Blue petal
    begin_fill() 
    setheading(25)
    forward(length/5.5)
    setheading(0)
    forward(length)
    setheading(227)
    forward(length*0.87)
    setheading(135)
    forward(length*0.8)
    end_fill()

    color("#3d6ec9") #Blue petal
    begin_fill() 
    setheading(250)
    forward(length/5)
    setheading(270)
    forward(length/2.6)
    setheading(0)
    forward(length/1.6)
    end_fill() 

在这里,您可以看到代码中的图形...

这就是上面描述的乌龟图

更新:

在此处输入图像描述

标签: pythonturtle-graphics

解决方案


非常简化的答案:

my_colors =['blue', 'yellow', 'red', 'green'] # replace with hex values

for i in range(4):

    photo(my_colors[i])
    right(90)

photo然后需要调整函数以获取一个关键字,它可能看起来像: def photo(my_color):,并且在函数中使用颜色的地方,你只需调用它color(my_color)

但是当然你需要考虑在每个循环之后你会转向哪里,以及你是否需要继续前进。


推荐阅读