了解 Pyglet 中的 anchor_x 和 anchor_y 属性
Understanding anchor_x and anchor_y attributes in Pyglet
我是 Python 和游戏开发的新手,刚开始学习 Pyglet (1.3.2)。我正在尝试使用标签及其定位。
我了解 x 和 y 坐标表示 labels/images 在 window 上的像素位置。在图像或 sprint 的情况下,锚值确定图像锚定到屏幕的位置(这反过来会影响它们的移动)。
但是,我并不完全了解标签中锚点的影响。根据 this pyglet 文档,它说:
The position of the text is given by the x and y coordinates. The
meaning of these coordinates is given by the anchor_x and anchor_y
parameters.
这里隐含着什么样的含义?如果他们没有移动,他们的位置不应该不管锚值如何都保持不变吗?
此外,当我 运行 使用这些标签编程时:
label1 = pyglet.text.Label('Window1',font_name='Times New Roman', font_size=36, x=200, y=100,anchor_x='center', anchor_y='center')
label2 = pyglet.text.Label('Window2',font_name='Times New Roman', font_size=36, x=200, y=100,anchor_x='left', anchor_y='center')
它产生的输出再次难以理解,因为 Window1 (anchor_x='center') 比 Window2 (anchor_x='left') 更靠左:
谁能详细说明anchor的意义,以及label的最终位置是如何计算的。谢谢!
所以锚点的工作方式是,它们只是简单地定义对象的放置点。意思是,如果你有 center
作为你的锚点,对象中心将被放置在给定的坐标 (在你的例子中,x=200, y=100
)
左下角的锚点将告诉 Pyglet 将对象放置在给定位置的左下角,而不是这些坐标的中心。
如果你分析一下你自己的形象,这个就很清楚了,这里两个点是直接连在一起的:
它们位于同一坐标,但因为您重新定义了锚点 - 它会将中心或角放在该位置。
红色表示您已将 center
定义为锚点。在蓝色中,您定义了 bottom left
(默认值)。
(忽略这些点没有完全对齐,在摇摆的火车上用笔记本电脑触摸板做是很棘手的)
希望这是有道理的。另请参阅我上面的评论以了解另一种描述方式。
我是 Python 和游戏开发的新手,刚开始学习 Pyglet (1.3.2)。我正在尝试使用标签及其定位。
我了解 x 和 y 坐标表示 labels/images 在 window 上的像素位置。在图像或 sprint 的情况下,锚值确定图像锚定到屏幕的位置(这反过来会影响它们的移动)。
但是,我并不完全了解标签中锚点的影响。根据 this pyglet 文档,它说:
The position of the text is given by the x and y coordinates. The meaning of these coordinates is given by the anchor_x and anchor_y parameters.
这里隐含着什么样的含义?如果他们没有移动,他们的位置不应该不管锚值如何都保持不变吗?
此外,当我 运行 使用这些标签编程时:
label1 = pyglet.text.Label('Window1',font_name='Times New Roman', font_size=36, x=200, y=100,anchor_x='center', anchor_y='center')
label2 = pyglet.text.Label('Window2',font_name='Times New Roman', font_size=36, x=200, y=100,anchor_x='left', anchor_y='center')
它产生的输出再次难以理解,因为 Window1 (anchor_x='center') 比 Window2 (anchor_x='left') 更靠左:
谁能详细说明anchor的意义,以及label的最终位置是如何计算的。谢谢!
所以锚点的工作方式是,它们只是简单地定义对象的放置点。意思是,如果你有 center
作为你的锚点,对象中心将被放置在给定的坐标 (在你的例子中,x=200, y=100
)
左下角的锚点将告诉 Pyglet 将对象放置在给定位置的左下角,而不是这些坐标的中心。
如果你分析一下你自己的形象,这个就很清楚了,这里两个点是直接连在一起的:
它们位于同一坐标,但因为您重新定义了锚点 - 它会将中心或角放在该位置。
红色表示您已将 center
定义为锚点。在蓝色中,您定义了 bottom left
(默认值)。
(忽略这些点没有完全对齐,在摇摆的火车上用笔记本电脑触摸板做是很棘手的)
希望这是有道理的。另请参阅我上面的评论以了解另一种描述方式。