Difference between revisions of "Mouse input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with " <syntaxhighlight lang=python> if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # left click print("Left Click")...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
Pygame accesses the mouse clicks separately from the mouse position. You can create 2 variable to store each:
 +
 +
<syntaxhighlight lang=python>
 +
user_click = pygame.mouse.get_pressed()
 +
user_move = pygame.mouse.get_pos()
 +
</syntaxhighlight>
 +
 +
You can then have if statements to check the button clicked:
 +
<syntaxhighlight lang=python>
 +
if user_click[0] == 1 :
 +
    print("left button clicked")
 +
elif user_click[0] == 3 :
 +
    print("right button clicked")
 +
</syntaxhighlight>
 +
 +
You can use user_move as a vector or you can access the X or Y coordinate:
 +
<syntaxhighlight lang=python>
 +
print(user_move[0]) # X coordinate
 +
print(user_move[1]) # Y coordinate
 +
print(user_move) # the vector of X & Y
 +
</syntaxhighlight>
 +
 +
 +
Inside the event loop you can check for other events:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>

Latest revision as of 17:50, 22 February 2018

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)