How To Get Mouse Input In Pygame

Mouse Input Options

Just like keyboard input, there are two different ways to get mouse input:

  • Using the event handler
  • Calling specific mouse functions

Using the event handler

Pygame’s built in events for mouse clicks are MOUSEBUTTONDOWN & MOUSEBUTTONUP and the demo below shows them being used. Run the code and notice the output in the console when clicking and releasing the mouse buttons.

				
					import pygame

pygame.init()

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

run = True
while run:

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

    if event.type == pygame.MOUSEBUTTONDOWN:
      print("Click")
    if event.type == pygame.MOUSEBUTTONUP:
      print("Release")   

pygame.quit()
				
			

Notice that the event is triggered regardless of which button is clicked, including the middle mouse button.

We can also print out the details of the event to see what other data it contains.

 

Replace print("Click") & print("Release") with print(event) and run the code again. This time when you click and release the mouse button, you will see the event details printed to the console.

The important values here are the pos and button variables. pos shows the x and y coordinates of the mouse and we will look into that a bit more shortly. The second variable corresponds to the button that has been clicked. 1 and 2 correspond to left and right clicking and button 3 is the middle mouse button. You can also detect if the mouse wheel is being scrolled. Buttons 4 and 5 correspond to the mouse scrolling up and down.

Using .get_pressed()

Alternatively, we can check the state of the mouse buttons using pygame.mouse.get_pressed(). Similarly to the .key_pressed() function for keyboard input, it checks the state of each button and returns True or False depending on it being clicked.

 

Running the code below will give you a continuous output showing the state of each button. Try clicking different mouse buttons and see what happens to the output.

				
					import pygame

pygame.init()

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

run = True
while run:

  print(pygame.mouse.get_pressed())

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

pygame.quit()
				
			

We can check for each button individually by using index values. Add [0] to the end of .get_pressed() and it will now only display the state of the left mouse button.

We can take this a step further and add specific checks with an if statement.

				
					import pygame

pygame.init()

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

run = True
while run:

  if pygame.mouse.get_pressed()[0]:
    print("Left mouse click")
  if pygame.mouse.get_pressed()[2]:
    print("Right mouse click")

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

pygame.quit()
				
			

Mouse Position

We’ve covered mouse clicks, so the next thing to look at is checking where on the screen the mouse cursor is. This is useful if you’re making anything that requires mouse input like clicking on buttons or shooting at enemies.

 

For this we use pygame.mouse.get_pos(), which returns the x and y coordinates of the mouse cursor.

Note that (0, 0) is in the top left of the screen as that is how the pygame coordinate system is setup.

				
					import pygame

pygame.init()

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

run = True
while run:

  pos = pygame.mouse.get_pos()
  print(pos)

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False

pygame.quit()
				
			

Related Posts

One thought on “How To Get Mouse Input In Pygame

Leave a Reply

Your email address will not be published. Required fields are marked *