And*_*rés 2 python pygame coordinates
我在PyGame中使用矢量和物理进行一些操作,默认坐标系对我来说不方便.通常情况下,该(0, 0)
点位于左上角,但我宁愿原点位于左下角.我宁愿改变坐标系,也不愿转换我必须绘制的每一件东西.
是否可以在PyGame中更改坐标系以使其像这样工作?
不幸的是,pygame没有提供任何这样的功能.最简单的方法是使用函数转换坐标,并在绘制任何对象之前使用它.
def to_pygame(coords, height):
"""Convert coordinates into pygame coordinates (lower-left => top left)."""
return (coords[0], height - coords[1])
Run Code Online (Sandbox Code Playgroud)
这将获取您的坐标并将它们转换为pygame的坐标,用于绘制,给定height
,窗口的高度以及coords
对象的左上角.
要改为使用对象的左下角,可以采用上面的公式,并减去对象的高度:
def to_pygame(coords, height, obj_height):
"""Convert an object's coords into pygame coordinates (lower-left of object => top left in pygame coords)."""
return (coords[0], height - coords[1] - obj_height)
Run Code Online (Sandbox Code Playgroud)