【问题标题】:Python Turtle - Issue with custom shape in Snake programPython Turtle - Snake 程序中的自定义形状问题
【发布时间】:2022-01-07 09:03:49
【问题描述】:

我有一个使用海龟制作游戏“蛇”的python 程序。它功能齐全。在这一点上,我正在添加 .gif 图像来美化它。 当我尝试使用自定义形状作为蛇的头部时,就会出现问题。如果我使用乌龟的基本形状,如“三角形”或“箭头”,它可以正常工作。它正确转动并按预期打印在第一个主体部分上。当我将其更改为我的自定义图像时,问题是它在第一个正文段下打印出来并且无法转动。

控件示例

    if direction == "up":
        if snake_direction != "down":
            snake_direction = "up"
            head_stamper.setheading(90)

用于制作多个部分的印章,以及第一部分的头部。

        for segment in snake:
            stamper.goto(segment[0], segment[1]) #These are the body segments, they work fine.
            stamper.stamp()
            head_stamper.goto(new_head)

在这里显示两个压模。

# Stamper for each body section
stamper = turtle.Turtle()
stamper.shape(bod_segment)
stamper.shapesize(25 / 20)
stamper.penup()

# Special stamper just for the snake's head.
head_stamper = turtle.Turtle()
# head_stamper has no issues when I make the shape "arrow" or "triangle", etc.
head_stamper.shape(head_segment)
stamper.shapesize(25 / 20)
head_stamper.penup()

我认为这是与问题相关的所有代码。

【问题讨论】:

    标签: python tkinter graphics turtle-graphics python-turtle


    【解决方案1】:

    当我将它更改为我的自定义图像时,问题是它被打印了 在第一个身体部分下出来,不能转动。

    至于不转向,这在register_shape() aka addshape() 的 Python 海龟文档中得到了解决:

    注意: 转动乌龟时图像形状不会旋转,所以它们会 不显示海龟的航向!

    至于重叠问题,我只能猜测。通常,turtle 中的规则是最后移动的东西在顶部。因此,转动您的通用头部形状会将其放在顶部,但是由于您的图像形状实际上并没有转动,它会在底部结束。同样,只是猜测。

    【讨论】:

    • 我已经对其进行了更多测试,我认为您是对的。谢谢!我认为我的解决方案必须是用海龟本身绘制一个自定义形状。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多