Change the position of the origin in PyGame coordinate system

做~自己de王妃 提交于 2019-12-06 09:33:11

Unfortunately, pygame does not provide any such functionality. The simplest way to do it would be to have a function to convert coordinates, and use it just before drawing any object.

def to_pygame(coords, height):
    """Convert coordinates into pygame coordinates (lower-left => top left)."""
    return (coords[0], height - coords[1])

This will take your coordinates and convert them into pygame's coordinates for drawing, given height, the height of the window, and coords, the top left corner of an object.

To instead use the bottom left corner of the object, you can take the above formula, and subtract the object's height:

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!