Python Turtle - Snake 程序中的自定义形状问题
Python Turtle - Issue with custom shape in Snake program
我有一个 python 程序使用 turtle 来制作游戏“Snake”。它功能齐全。此时我正在添加 .gif 图像以美化它。
当我尝试使用自定义形状作为蛇头时,问题就出现了。如果我使用 turtle 的基本形状,如“三角形”或“箭头”,它就可以正常工作。它正确旋转并按预期打印在第一个 body 段上。当我将其更改为我的自定义图像时,问题是它在第一个 body 段下打印出来并且无法转动。
控件示例
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()
我认为这是与问题相关的所有代码。
When I change it to my custom image, the problem is that it is printed
out under the first body segment and cannot turn.
至于不转动,这在 register_shape()
的 Python 乌龟文档中解决了,又名 addshape()
:
Note: Image shapes do not rotate when turning the turtle, so they do
not display the heading of the turtle!
至于重叠问题,我只能猜测。一般来说,turtle 中的规则是最后移动的东西在最上面。因此,转动您的通用头部形状使其位于顶部,但由于您的图像形状实际上并未转动,因此它最终落在了底部。再一次,只是一个猜测。
我有一个 python 程序使用 turtle 来制作游戏“Snake”。它功能齐全。此时我正在添加 .gif 图像以美化它。 当我尝试使用自定义形状作为蛇头时,问题就出现了。如果我使用 turtle 的基本形状,如“三角形”或“箭头”,它就可以正常工作。它正确旋转并按预期打印在第一个 body 段上。当我将其更改为我的自定义图像时,问题是它在第一个 body 段下打印出来并且无法转动。
控件示例
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()
我认为这是与问题相关的所有代码。
When I change it to my custom image, the problem is that it is printed out under the first body segment and cannot turn.
至于不转动,这在 register_shape()
的 Python 乌龟文档中解决了,又名 addshape()
:
Note: Image shapes do not rotate when turning the turtle, so they do not display the heading of the turtle!
至于重叠问题,我只能猜测。一般来说,turtle 中的规则是最后移动的东西在最上面。因此,转动您的通用头部形状使其位于顶部,但由于您的图像形状实际上并未转动,因此它最终落在了底部。再一次,只是一个猜测。