Mouse input

From TRCCompSci - AQA Computer Science
Revision as of 17:50, 22 February 2018 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Pygame accesses the mouse clicks separately from the mouse position. You can create 2 variable to store each:

user_click = pygame.mouse.get_pressed()
user_move = pygame.mouse.get_pos()

You can then have if statements to check the button clicked:

if user_click[0] == 1 :
    print("left button clicked")
elif user_click[0] == 3 :
    print("right button clicked")

You can use user_move as a vector or you can access the X or Y coordinate:

print(user_move[0]) # X coordinate 
print(user_move[1]) # Y coordinate 
print(user_move) # the vector of X & Y


Inside the event loop you can check for other events:

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1: # left click 
                    print("Left Click")
                elif event.button == 3: # right click shrinks radius 
                    print("Right Click")
            
            if event.type == pygame.MOUSEMOTION:
                # if mouse moved, get the position 
                print(event.pos)