Pygame import for mouse click check (online experiment)

Hi there,

I’m using PsychoPy version 2022.2.4. I’m new to both PsychoPy and python and trying to get my very first PsychoPy experiment working.

I have imported pygame in the “Before experiment” section of the code component in PsychoPy and then in the “Each frame” section I included the below to test if it works:

    if event.type == pygame.MOUSEBUTTONUP:
        print("MOUSE CLICKED")
        pos = pygame.mouse.get_pos()
        # if the click event of the mouse cursor actually happened on my image then replay my audio from the start with no loops
        if clicked_image.rect.collidepoint(pos)
              # make sure the audio file is stopped and replay from the beginning
              mysound.stop()
              mysound.play(loops=None)

Eventually when this works, I want it to capture each new click that happens on a specific image, replay an audio file from the beginning (no audio loop).

However I’m getting this error:

AttributeError: module 'psychopy.event' has no attribute 'type'

So I feel I’m on stalemate for the past week on this…

I need something that will work and successfully translated in PsychoJS eventually, since my experiment will be online.

I’ve read on stackoverflow that is better to use MOUSEBUTTONUP rather than mouse.get_pressed()[0] as there are issues with potential infinite loops with the latter if not refreshed properly each time.

Many thanks,
Constantina

Hi There,

If you are planning to run the study online, I wouldn’t use pygame (it is a python library, so it won’t run in browser) - but you should be able to just use the mouse component for mouse responses - is there a reason to not use the mouse component? :mouse:

Becca

Here is just one of the tutorials we have on using the mouse component that could be helpful Using Multiple Mouse Clicks in One Trial in PsychoPy 🖱️ - YouTube