如何用x,y坐标重新调整图像(海龟图形)

2024-10-01 19:28:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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()

Tags: 方法importnoneifdeffill形状turtle
1条回答
网友
1楼 · 发布于 2024-10-01 19:28:56

要更改所绘制图形的比例,请将xy位置的所有偏移量乘以比例因子:

def MapleLeaf(x=0, y=0, scale=1):
    turtle.penup()
    turtle.goto(1*scale+x,-3*scale+y)
    turtle.pendown()
    turtle.goto(5*scale+x,-4*scale+y)
    turtle.goto(4*scale+x,-3*scale+y)
    turtle.goto(9*scale+x,1*scale+y)
    # ...

请注意,我在一开始就删除了您的if语句,因为您可以使用0作为默认值。只有在可变的默认值(如列表)下,您才需要始终使用一个sentinel,如None来表示需要默认值。在

由于scale和xy偏移重复太多,您可能需要将它们进一步分解到函数中:

^{pr2}$

另一种选择可能是将偏移量放入一个列表中,然后在循环中迭代它们。在

相关问题 更多 >

    热门问题