PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it

“I was born not knowing and have had only a little time to change that here and there.”
Richard P. Feynman


Mouse

MOUSEBUTTONUP vs mouse.get_pressed()

Mouse and mouse event

Related Stack Overflow questions:

The pygame.event.Event object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked. Each mouse button is associated a value. For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down. When multiple keys are pressed, multiple mouse button events occur. Further explanations can be found in the documentation of the module pygame.event.

📁 minimal example - Detect mouse button click events

repl.it/@Rabbid76/PyGame-MouseEvents

📁 minimal example - Detect click on Sprite

Pygame - Mouse clicks not getting detected

The coordinates which are returned by pygame.mouse.get_pos() are evaluated when the events are handled. You need to handle the events by either pygame.event.pump() or pygame.event.get().

See pygame.event.get():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

The current position of the mouse can be determined via pygame.mouse.get_pos(). The return value is a tuple that represents the x and y coordinates of the mouse cursor. pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down. When multiple buttons are pressed, multiple items in the list are True. The 1st, 2nd and 3rd elements in the list represent the left, middle and right mouse buttons. If a specific button is pressed, this can be evaluated by subscription:

buttons = pygame.mouse.get_pressed()
if buttons[0]:
    print("left button pressed")

If any button is pressed, this can be evaluated with the any function:

buttons = pygame.mouse.get_pressed()
if any(buttons):
    print("button pressed")

Further explanations can be found in the documentation of the module pygame.mouse.

📁 minimal example - Detect mouse button states

repl.it/@Rabbid76/PyGame-MouseStates

📁 minimal example - Detect Sprite on hover

Pygame - Mouse clicks not getting detected

For instance:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
  
    buttons = pygame.mouse.get_pressed()

    # if buttons[0]:  # for the left mouse button
    if any(buttons):  # for any mouse button
        print("You are clicking")
    else:
        print("You released")

    pygame.display.update()

If you just want to detect when the mouse button is pressed respectively released, then you have to implement the MOUSEBUTTONDOWN and MOUSEBUTTONUP (see pygame.event module):

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            print("clicked", event.button)
        if event.type == pygame.MOUSEBUTTONUP:
            print("released", event.button)

    pygame.display.update()

While pygame.mouse.get_pressed() returns the current state of the buttons, the MOUSEBUTTONDOWN and MOUSEBUTTONUP occurs only once a button is pressed.

Mouse cursor

Related Stack Overflow questions:

📁 Minimal example - Image on mouse cursor

Change mouse position

Related Stack Overflow questions:

Mouse Drag

Related Stack Overflow questions:

📁 Minimal example - Drag rectangle

📁 Minimal example - Drag Sprite

repl.it/@Rabbid76/PyGame-MouseDragSimple

Mouse drag and rotate

Related Stack Overflow questions:

Mouse speed

Related Stack Overflow questions:

Touch input

Related Stack Overflow questions:

📁 Minimal example - Touch event