python - 用 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()
在这里,您可以看到代码中的图形...
更新:
解决方案
非常简化的答案:
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)
但是当然你需要考虑在每个循环之后你会转向哪里,以及你是否需要继续前进。
推荐阅读
- vba - VBA Excel 365 64位Winsock实现问题
- excel - 如何打印列表 pdf 文件并在 excel 列中命名它们?
- visual-studio - VS2019 addin/extensions 如何启用/禁用菜单项
- php - 如何从 PHP 中的十六进制值中找到低字节和高字节?
- javascript - 访问文件属性 JavaScript 时 JSON 未定义
- spring - 弹簧靴;登录后将用户的名字传递给 welcome.jsp
- javascript - Jquery multiselect为每个选定的值创建div
- twilio - Twilio API 接收我传递给调用的额外参数
- python - 如何提取除冠词和所有格之外的名词?
- java - Tomcat 设置 URL 并加载速度 VM 文件而不是 index.html