如何更改 Pygame 坐标系,使窗口中心为 (0,0)? [英] How do I change the Pygame coordinate system so that the center of the window is (0,0)?
问题描述
我想更改 pygame 坐标系,以便窗口的中心为 (0,0) 并具有负值,如 图像.有没有办法转换坐标?
I want to change pygame coordinate system so that the center of the window is (0,0) and has negative values as in the image. Is there a possible way to convert the coordinates?
https://i.stack.imgur.com/sGPFt.gif
推荐答案
PyGame 使用左上角为 (0,0) 的坐标系.这是无法改变的.
PyGame uses a coordinate system where the top left corner is (0,0). That cannot be changed.
如果你想把坐标系的原点移动到屏幕中心,那么你必须自己平移坐标.例如编写一个转换坐标的函数:
If you want to move the origin of the coordinate system to the center of the screen, then you have to translate the coordinates by yourself. For instance write a function that translates the coordinates:
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 表达式:
Or use a lambda expression:
center_origin = lambda p: (p[0] + screen.get_width() // 2, p[1] + screen.get_height() // 2)
screen.blit(my_image, center_origin((x, y))
这篇关于如何更改 Pygame 坐标系,使窗口中心为 (0,0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!