Ending trial when the required number of squares are selected

I have built a task in which a number of squares appear in a grid for 0.5 seconds and then disappear. After a delay, the participants are directed to recall where the squares were located by clicking on the grid The grid squares that are selected turn red and the number of squares selected cannot exceed the number of squares that appeared on the screen.
I have also allowed participants to select and deselect squares in the grid. My problem is that I would like to ensure that participants can’t end the trial until the correct number of squares are selected (ie are red on the screen). Pressing space bar and having the correct number of responses are requirements for moving to the next trial and saving data from that trial.
This is the code I have so far (its in the each frame tab in code component):

Each Frame tab

import time

Set a delay duration (in seconds)

click_delay_duration = 0.1 # Adjust this as needed

Define the required number of red squares to select

required_red_squares = 2 # Adjust this as needed

Initialize a variable to track the selected red squares

selected_red_squares =

Initialize a variable to track whether the participant can end the trial

can_end_trial = False

if t >= 4.9:
for square_name in square_names:
red_square_name = square_name + “_red”
if mouse.isPressedIn(eval(square_name)):
click_time = time.time() # Record the time of the click
while mouse.getPressed()[0]: # Continue while the left mouse button is held down
win.flip() # Update the window
# Delay processing the click for the specified duration

        # Process the click after the delay
        if square_name not in clicked_squares and num_clicked < 2:
            eval(red_square_name).autoDraw = True
            clicked_squares = clicked_square_names.copy()
            num_clicked += 1
        elif square_name in clicked_squares:
            if len(clicked_squares) > 1:
                eval(red_square_name).autoDraw = False
                clicked_squares = clicked_square_names.copy()
                num_clicked -= 1
                # Deselect the square even if it's the first one selected
                eval(red_square_name).autoDraw = False
                clicked_squares = []
                num_clicked = 0

# Check for spacebar press to end the trial when the correct number is selected
if len(selected_red_squares) == required_red_squares and event.getKeys(keyList=["space"]):
    can_end_trial = True

# Check if the participant can end the trial (both conditions met)
if len(selected_red_squares) == required_red_squares and can_end_trial:
    # Your code to end the trial and begin a new one here
    # Make sure to reset selected_red_squares, can_end_trial, and other relevant variables for the new trial
    selected_red_squares = []
    can_end_trial = False

The second clause will always run if the first one does. Is it in End Routine?

Why are you using can_end_trial instead of continueRoutine = False?

If you are using Builder then you shouldn’t use win.flip() or time.sleep. You also don’t need to import time. Use something like:

if mouse.isPressedIn(eval(square_name)) and t > click_time + click_delay_duration:
     click_time = t