| Reference | Downloads | Github

Stimuli changes colour when clicked but 'flickering' and not responding immediately

Hi there,
I’ve created a demo experiment where participants are required to memorise a pattern of black and white squares, and then click on the squares in a blank grid to reproduce the pattern. I want participants to be able to change their response if they make a mistake, and when they’re happy with it continue to the next trial. I’ve got it working with the following code component in the ‘Each Frame’ tab, to change squares from white-black and black-white:

for square in [R1, R2, R3, R4]:
# Response when a square has not already been clicked:
        if mouse.isPressedIn(square) and square.fillColor == 'white':
            square.fillColor = 'black'
 #Response when a square has already been clicked:
            if mouse.isPressedIn(square) and square.fillColor == 'black':
                square.fillColor = 'white'

However the colour doesn’t always change instantly but instead ‘flickers’ and sometimes takes several clicks to finally change. I think this may be to do with the timing of the click and the screen refresh rate but I’m not sure if there is a work-around. Can anyone help?
Thank you! :slight_smile:

Hi @auriga, the images are flickering because they are constantly switching between black and white whenever the mouse is pressed in the shape. If you hold the mouse down in the shapes, you are satisfying one or the other colour changing conditions on each screen refresh, hence the flicker. Instead, you want a situation where you only change the square colour if it is the same as before the mouse button was pressed:

# When no mouse button is pressed, store the square colours
if mouse.getPressed()[0] == 0:
    fills = [R1.fillColor, R2.fillColor]

for index, square in enumerate([R1, R2]):
    # If mouse pressed in square, match current square to square stored in fills list
    # Check if the colour has not changed. If it has not changed, change the colour of the square 
    # that contains the mouse click 
    if mouse.isPressedIn(square) and fills[index] == 'white':
        square.fillColor = 'black'

    if mouse.isPressedIn(square) and fills[index] == 'black':
        square.fillColor = 'white'

This works a treat - thank you so much!

Hello, I am currently making a similar grid where participants must replicate a pattern they saw previously. Since shapes don’t appear over images in builder, I’ve been using a text “X” for participants to mark the spot in the grid. What does the R1, R2 refer to and why is this list needed for the code to work? If in builder, what do I put in the field “color” if the color will be changing and not constant, I want it to call info from the code but I know $ does not do that.

Hi, I don’t know if you still need an answer, but the R1, R2 in my code referred to square shapes that were colour filled either black or white, and changed as a participant clicked on them, so I used shapes rather than images. I think your design using a text ‘X’ is slightly different to mine so sorry I can’t help any further - I hope you can get an answer elsewhere though!