1

I'm trying to get mouse button input in pygame but it doesn't work. I've tried

if pygame.mouse.get_pressed() == True:
    "do something"

,

if pygame.mouse.get_pressed() == (1,0,0):
    "do something"

,

if pygame.mouse.get_pressed()[0]:
    "do something"

...

I think everything from the docs but it does nothing and the if pygame.mouse.get_pressed() == (1,0,0): always worked before. (if mouse_pos == (PlayCoordsx, PlayCoordsy) and pygame.mouse.get_pressed() == (1, 0, 0):) Can someone tell me what I'm doing wrong?

1 Answer 1

2

To begin with, it is not recommended to use mouse.get_pressed(). Instead use event.get() and check what the events are. Eg.

while ... # This is your main loop
  # get all recent events
  events = pygame.event.get()

  # proceed events
  for event in events:

    # handle MOUSEBUTTONUP, ie. click release
    if event.type == pygame.MOUSEBUTTONUP:
      pos = pygame.mouse.get_pos()

      #Do your stuff
Sign up to request clarification or add additional context in comments.

2 Comments

it is not recommended to use mouse.get_pressed(): Why?
Not unless you absolutely have to, because you will need to start using flags and stuff to compensate for the multitude of triggers it will generate. And you are likely to miss a lot of presses as you wont have a queue of inputs. Just use get event in the main loop instead, it's more elegant and less error prone. Even the pygame docks for get_pressed() says it's better to use event.get() instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.