Search code examples
pythonargumentspygamecollision

Pygame Rect, what are the arguments?


I know this may sound stupid, but the pygame documentation on their website says that it is:

x = pygame.Rect(left,top,width,height)

However, in my program, I cannot figure out if that is true, or if the arguments are actually two sets of coordinates. I'm not nearly experienced to find out by looking through the pygame source code.


Solution

  • Both of them work:

    class pygame.Rect
        pygame object for storing rectangular coordinates
        Rect(left, top, width, height) -> Rect
        Rect((left, top), (width, height)) -> Rect
        Rect(object) -> Rect

    So, if you have coordinates (x1, y1) and (x2, y2), both of the following would work:

    pygame.Rect(x1, y1, x2-x1, y2-y1)
    
    pygame.Rect((x1, y1), (x2-x1, y2-y1))