How to use pygame.MOUSEBUTTONDOWN?

17,211

Solution 1

See http://www.pygame.org/docs/ref/event.html.

Where buttons is your sprite group, which have Rect()s. You can define a click() function, for different sounds on each button.

for event in pygame.event.get():    
    if event.type == MOUSEBUTTONDOWN :
        x, y = event.pos
        for button in buttons:
            if button.rect.collidepoint(x, y):
                print("play sound here.")

                # or, if button handles on clicking, by a defined function:
                button.click()

Solution 2

use events in your main loop

for event in pygame.event.get():
    if event.type == MOUSEBUTTONDOWN :
        play_music()
Share:
17,211
user1150740
Author by

user1150740

Updated on June 27, 2022

Comments

  • user1150740
    user1150740 almost 2 years

    My simple question is how can I use pygame.MOUSEBUTTONDOWN on a sprite or item to trigger an event?

    e.g. I have item_A and want music to start when I press the object with my mouse.