Reputation: 43
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.
Upvotes: 2
Views: 26120
Reputation: 7511
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()
Upvotes: 4
Reputation: 1490
You will need to poll for events in your main loop, and when you detect a MOUSEBUTTONDOWN event you will need to check if it's on the sprite you want, and if it is then start the music.
Upvotes: 0
Reputation: 4985
use events in your main loop
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN :
play_music()
Upvotes: 1