Search code examples
pythonpsychopy

PsychoPy - Pygame import not working for MOUSEBUTTONUP?


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 want to capture each new click that happens on a specific image, to replay an audio file from the beginning (no audio loop).

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)

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.

Many thanks, Constantina


Solution

  • The problem you're having is that you're mixing pygame constructs with PsychoPy constructs.

    I really recommend you use the graphical Builder interface to get your experiment going. That has Mouse Component to insert and that has Clickable Stimuli where you can identify the objects that are valid 'buttons'. As well as producing code for you that's fully internally compatible with the rest of the PsychoPy lib, your experiment is much more likely to port online. If you write your experiment by hand in python code then it will not run in a browser