Circle object select/de-select

Hello!

I am having a problem with one part of my experiment. I have coded a multiple object tracking task. I have below the “runCondition” def that show circles objects, make them move around the screen and then have participants select the circles they tracked.

My problem is with the circle selection part, toward the bottom. What I would like (what I tried to code) is to have participants be able to select and de-select circles as they want within the 15 seconds they have to answer.

The problem is that there is a weird behavior happening when I run the task: I have to click several times on the “correct” circles to select or de-select them. It is bugging, and I cannot pinpoint what would do that in my code.

Here is the code:

def runCondition(self, nRedCircles, initialCircleTime, circlesMovingTime1, redCircleTime, circlesMovingTime2):
    mouse = event.Mouse()
    self.resetCircles()
    # Show circles
    for c in self.circles:
        c.visual.autoDraw = True
    self.win.flip()
    core.wait(self.initialCircleTime)
    # Move circles
    self.moveCircles(circlesMovingTime1)
    # Change circles color
    for c in self.circles[:nRedCircles]:
        c.setColor("maroon", self.cDefaultLineWidth)
    self.win.flip()
    core.wait(redCircleTime)
    # Reset circles colors
    for c in self.circles[:nRedCircles]:
        c.setColor(self.cDefaultColor, self.cDefaultLineWidth)
    self.win.flip()
    core.wait(self.resetCircleTime)
    # Moving circles
    self.moveCircles(circlesMovingTime2)
    # Clean keys buffer
    event.getKeys()
    # Circle selection
    selection = [False] * self.nCircles
    reactionStartTime = time.perf_counter()
    selectionStartTime = time.perf_counter()
    selectionEndTime = selectionStartTime + self.selectionTimeout
    nSelections = 0
    spacebarPressed = False
    mouse = event.Mouse()
    click_delay = 0.05
    while not (spacebarPressed and nSelections > 0) and time.perf_counter() < selectionEndTime:
        if mouse.getPressed()[0]:
            clicked_circles = [i for i, c in enumerate(self.circles) if mouse.isPressedIn(c.visual)]
            for i in range(len(self.circles)):
                c = self.circles[i]
                if i in clicked_circles:
                    if not selection[i]:  # If the circle is not selected, select it
                        c.visual.lineColor = self.cSelectionLineColor
                        c.visual.lineWidth = self.cSelectionLineWidth
                        selection[i] = True
                        nSelections += 1
                        spacebarPressed = False
                        print(f"Circle {i} selected. nSelections: {nSelections}")
                    else:  # If the circle is already selected, deselect it
                        c.visual.lineColor = self.cDefaultColor
                        c.visual.lineWidth = self.cDefaultLineWidth
                        selection[i] = False
                        nSelections -= 1
                        spacebarPressed = False
                        print(f"Circle {i} deselected. nSelections: {nSelections}")
        self.win.flip()
        core.wait(click_delay)
        spacebarPressed = len(event.getKeys(keyList=["space"])) > 0
    self.exitIfEscPressed()
    reactionElapsedTime = time.perf_counter() - reactionStartTime
    # Hide circles
    for c in self.circles:
        c.visual.autoDraw = False
    # Results
    correctAnswers = selection[:nRedCircles].count(True)
    wrongAnswers = selection[nRedCircles:].count(True)
    return reactionElapsedTime, correctAnswers, wrongAnswers

I would appreciate any help in changing this code, or implementing a new one if somebody had the same problem before! I can also share the entire code if necessary.

Thank you!