【问题标题】:How do I change the Pygame coordinate system so that the center of the window is (0,0)?如何更改 Pygame 坐标系,使窗口的中心为 (0,0)?
【发布时间】:2020-04-30 00:48:50
【问题描述】:

我想更改 pygame 坐标系,使窗口的中心为 (0,0) 并且具有负值,如 image。 有没有可能的方法来转换坐标?

https://i.stack.imgur.com/sGPFt.gif

【问题讨论】:

  • 您可以在窗口中间创建一个屏幕。然后添加四个坐标,并使用类来协调窗口坐标。工作量很大,我不知道具体该怎么做。
  • 你觉得有办法转换坐标吗?
  • 查看答案。

标签: python pygame


【解决方案1】:

PyGame 使用左上角为 (0,0) 的坐标系。这是无法改变的。

如果要将坐标系的原点移动到屏幕的中心,则必须自己平移坐标。例如编写一个转换坐标的函数:

def center_origin(surf, p):
    return (p[0] + surf.get_width() // 2, p[1] + surf.get_height() // 2)
screen.blit(my_image, center_origin(screen, (x, y)))

或者使用 lambda 表达式:

center_origin = lambda p: (p[0] + screen.get_width() // 2, p[1] + screen.get_height() // 2)
screen.blit(my_image, center_origin((x, y))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多