how to check for mouse press pygame code example

Example 1: pygame get mouse position

x,y = pygame.mouse.get_pos()
#get the mouse cursor position
#get_pos() -> (x, y)
#Returns the X and Y position of the mouse cursor.
#The position is relative to the top-left corner of the display.
#The cursor position can be located outside of the display window,
#but is always constrained to the screen.

Example 2: how to get a mouse press not hold in pygame

class Button(object):
    def __init__(self,x,y,width,height,color):
        self.rect = pygame.Rect(x,y,width,height)
        self.image=pygame.draw.rect(screen, color,(self.rect),)
        self.x=x
        self.y=y
        self.width=width
        self.height=height

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

Tags:

C Example