Answers for "mouse event in pygame"

3

mouse in pygame

pygame.mouse.get_pos() #-> get the mouse cursor position on the screen in taple
#-> (x, y)

if event.type == pygame.MOUSEBUTTONDOWN:
    print(event.button)

#------------------------#
1 - left click
2 - middle click
3 - right click
4 - scroll up
5 - scroll down
#------------------------#
Posted by: Guest on July-28-2021
0

event.type == pygame.MOUSEBUTTONDOWN click

def handle_event(self, screen, event, player):
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.MOUSEMOTION:
            pygame.draw.rect(screen, BACKGROUND, (0,0, self.screen_width, SQUARESIZE))
            posx = event.pos[0]
            if player == 1:
                pygame.draw.circle(screen, P1STONE, (posx, int(SQUARESIZE/2)), RADIUS)
            else:
                pygame.draw.circle(screen, P2STONE, (posx, int(SQUARESIZE/2)), RADIUS)
            pygame.display.update()    
            return None
        
        if event.type == pygame.MOUSEBUTTONDOWN:
            pygame.draw.rect(screen, BACKGROUND, (0,0, self.screen_width, SQUARESIZE))
            posx   = event.pos[0]
            action = int(np.floor(posx/SQUARESIZE))
            return action
Posted by: Guest on July-11-2020

Python Answers by Framework

Browse Popular Code Answers by Language