Change the position of the origin in PyGame coordinate system

前端 未结 2 556
无人及你
无人及你 2020-12-18 06:54

I am doing some operations with vectors and physics in PyGame and the default coordinate system is inconvenient for me. Normally the (0, 0) point is at the top-

2条回答
  •  时光取名叫无心
    2020-12-18 07:33

    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)
    

提交回复
热议问题