我正在使用PyGame中的矢量和物理进行一些操作,默认坐标系对我来说不方便。通常(0, 0)
点位于左上角,但我宁愿原点位于左下角。我宁愿改变坐标系,也不愿改变我必须绘制的每一件东西。
是否可以在PyGame中更改坐标系以使其像这样工作?
答案 0 :(得分:9)
不幸的是,pygame不提供任何此类功能。最简单的方法是使用函数转换坐标,并在绘制任何对象之前使用它。
def to_pygame(coords, height):
"""Convert coordinates into pygame coordinates (lower-left => top left)."""
return (coords[0], height - coords[1])
这将获取坐标并将其转换为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)
答案 1 :(得分:0)
如果要渲染的对象是垂直对称的(例如矩形),则只需翻转屏幕即可将坐标系设置为左下角,如下所示:
display_surface = pygame.display.get_surface()
display_surface.blit(pygame.transform.flip(display_surface, False, True), dest=(0, 0))
您可以水平/从右上方/右下方执行相同的操作。重要方法记录在这里: