Touchscreen doesn't work even with the 'contains' function

Hi everyone,

I have a function that runs a simple fish-catching game. In this game, a fish appears in a random position each time, and participants need to [single] tap the fish on the touchscreen to move on to the next trial. I am using fish.contains(mouse) to detect single taps from the touchscreen, as suggested by many people experienced with touchscreen interfaces. However, this approach still doesn’t work. The fish could never got tapped (i.e., no response after single or double tapping). Could anyone provide suggestions on what the possible issues might be?

Here is the function:
def fish_catching_practice():
fish = visual.ImageStim(win, image=‘stimuli/fish.png’, size=(0.2, 0.2))

# Run the routine for 10 iterations
for _ in range(10):
    event.clearEvents()
    # Set a random position for the fish within the screen
    fish.pos = (random.uniform(-0.4, 0.4), random.uniform(-0.4, 0.4))
    
    # Display the white background and fish
    white_background.draw()
    fish.draw()
    win.flip()
    
    # Start the timer for the 3-second display
    trial_clock = core.Clock()
    trial_duration = 3.0  # seconds
    
    # Wait for a touch on the fish or until 3 seconds have passed
    while trial_clock.getTime() < trial_duration:
        # Check if the mouse (touch) is within the fish stimulus
        if fish.contains(mouse):
            break

I’ve pre-defined the mouse object for the experiment: mouse = event.Mouse(win=win)

I’m using:
Python 3.8.10; PsychoPy v2022.2.4; Windows-10-10.0.19045-SP0

You need to use mouse.getPos() to get the x,y location alone, which is what “contains” is looking for.

Thanks for your response!

I initially tried using mouse.getPos(), but that didn’t resolve the issue. However, I found that placing the code that displays the stimuli inside the while loop fixed it:

# Run the routine for 10 iterations
for _ in range(10):
    event.clearEvents()
    # Set a random position for the fish within the screen
    fish.pos = (random.uniform(-0.4, 0.4), random.uniform(-0.4, 0.4))

    # Start the timer for the 3-second display
    trial_clock = core.Clock()
    trial_duration = 3.0  # seconds
    # Wait for a touch on the fish or until 3 seconds have passed
    while trial_clock.getTime() < trial_duration:
        # Display the white background and fish
        white_background.draw()
        fish.draw()
        win.flip()
        # Check if the mouse (touch) is within the fish stimulus
        if fish.contains(mouse):
            break