Ending routine after moving to 4 targets

Hi there,
I’ve been stuck on this issue for a while and can’t get anything that I’ve tried to work, so hopefully someone will be able to help! I’ve created a memory block in my task where participants are presented with a random 4 element sequence (targets change colour in a circle) which they must remember and then reproduce it on the following screen which is a blank template.

If the mouse is pressed in, or the polygons contain the mouse, then the colour of the target is changed to black and resets to white after .4 seconds - this all works perfectly. What I need is that after 4 targets have changed colour, for the routine to end and move onto the next one. However, I cannot get any sort of counter to work. Essentially, I need help creating a way to count the number of targets that have changed colour or had the mouse go over them, and after this occurs 4 times to skip the routine.

I have tried defining Count = 0 and count = [] in begin routine and adding to this in each frame following the target changing colour/mouse moving over the target with count = count +1 and count +=1, as well as trying to use len - but I couldn’t get these to work.

My current code looks like this if it helps:
Begin Routine:

mem_pos=[twelve_4, five_4, ten_4, fifteen_4, twenty_4, twenty_five_4, thirty_4, thirty_five_4, fourty_4, fourty_five_4, fifty_4, fifty_five_4]

timer = core.Clock()

Each Frame:

for polygon in mem_pos:
    if mem_production_mouse.isPressedIn(polygon) or polygon.contains(mem_production_mouse):
        timer.reset()
        polygon.color = 'black'
    if timer.getTime() >=.4:
        polygon.color = 'white'

Hopefully someone will be able to help! Do let me know if you need any more information.

Many thanks in advance!

Just some more details to add to this as I have played around with it a lot this last week. Using count +=1 doesn’t seem to work when trying to detect a colour change (EG: if twelve.color == 'black':), it just doesn’t count. Or if it does work when using polygon.contains(mouse) it gets triggered constantly - meaning it ends the routine & loop straight away - as the mouse is then present in the stimuli and doesn’t have a chance to move before the trial ends.

I can’t seem to find a way around this at all, hopefully this can provide some further context. This is one of the last stages of my task development and I cant quite get it!!

I have figured this out! Hopefully, I can help others if they get stuck with a similar issue. The way around this was to use arrays and lists.

I opened an array in begin routine for ‘count1’ and ‘b’, aswell as assigned ‘i = 1’. Then, for each stimuli in each frame, if polygon.contains(mouse), ‘i’ would then equal a different number. EG: polygon1.contains(mouse), i=1 or polygon2.contains(mouse), i=2. I then used count1.append(i) within each if loop. To get round the fact that this was then a massive list and constantly being triggered due to the mouse being present in the stimuli for a while, I used b = list(set(count1)) to count the number of unique entries in the list, and then if len(b) == 4: continueRoutine = False to get the desired number of 4 targets being hit.