我用Python创造了一片枫叶。我已经找到了一种方法来在x和y轴上转换形状,但我还没有找到一种方法来调整枫叶的大小,同时保持它的原始形状。在
import turtle
def MapleLeaf(x=None,y=None):
if x==None:
x=0
if y==None:
y=0
turtle.penup()
turtle.goto(1+x,-3+y)
turtle.pendown()
turtle.goto(5+x,-4+y)
turtle.goto(4+x,-3+y)
turtle.goto(9+x,1+y)
turtle.goto(7+x,2+y)
turtle.goto(8+x,5+y)
turtle.goto(5+x,4+y)
turtle.goto(5+x,5+y)
turtle.goto(3+x,4+y)
turtle.goto(4+x,9+y)
turtle.goto(2+x,7+y)
turtle.goto(0+x,10+y)
turtle.goto(-2+x,7+y)
turtle.goto(-4+x,8+y)
turtle.goto(-3+x,3+y)
turtle.goto(-5+x,6+y)
turtle.goto(-5+x,4+y)
turtle.goto(-8+x,5+y)
turtle.goto(-7+x,2+y)
turtle.goto(-9+x,1+y)
turtle.goto(-4+x,-3+y)
turtle.goto(-5+x,-4+y)
turtle.goto(0+x,-3+y)
turtle.goto(2+x,-7+y)
turtle.goto(2+x,-6+y)
turtle.goto(1+x,-3+y)
turtle.hideturtle()
turtle.pencolor("black")
turtle.fillcolor("red")
turtle.begin_fill()
MapleLeaf(50,50)
turtle.end_fill()
turtle.done()
要更改所绘制图形的比例,请将
x
和y
位置的所有偏移量乘以比例因子:请注意,我在一开始就删除了您的
if
语句,因为您可以使用0
作为默认值。只有在可变的默认值(如列表)下,您才需要始终使用一个sentinel,如None
来表示需要默认值。在由于scale和
^{pr2}$x
和y
偏移重复太多,您可能需要将它们进一步分解到函数中:另一种选择可能是将偏移量放入一个列表中,然后在循环中迭代它们。在
相关问题 更多 >
编程相关推荐