How to make a color changer using random colors

Hi! I’m new to PsychoPy. I want to make a color changer: anytime the participant clicks on colored boxes, an empty polygon color changes to the color clicked. I’d like to output the name of the color clicked and the time of the clicks.

So far I can’t change the polygon color more than 2 times on every trial, regardless how much I click on the colored boxes.

The boxes’ colors came from a conditions file and are randomized as the trial loops.

Begin Routine

polygons = [box1, box2, box3]
clickClock=core.Clock()
lastClickTime = 0
clicks = []
clickedlist = []
clicked_things =[]

Every Frame


clickables = [box1, box2, box3]

for clickable in clickables:
    if mouse.isPressedIn(clickable):
        clickedlist = clicked_things.append(clickable.name)
        thisExp.addData('box', clickedlist)
        thisClickTime = clickClock.getTime()
        clicks = thisClickTime
        thisExp.addData('RT', clicks)

for clickable in clickables:
    if clickable.name in clicked_things:
        polygon.color = clickable.fillColor

Hi @ariomester,

I am not sure that this does what you want it to do:

That’s because clicked_things will contain multiple values after multiple clicks and therefore will change polygon.color rapidly multiple times each frame and always end on the color of box3 (last element in clickables). So, what you need is

for clickable in clickables:
    if clickable.name == clicked_things[-1]:
        polygon.color = clickable.fillColor

This should set polygon.color to the color of the most recently clicked box.

Also another caveat: If your participants hold the mouse button down for more than a frame (which they probably will), you will end up with a lot of redundant and confusing data. What I usually do to prevent this is checking whether the mouse was NOT pressed in the last frame and only then run the code that should be run when the mouse is pressed.

Hi!

Thank you a lot, there were things I wasn’t considering… I got this error:

if clickable.name == clicked_things[-1]:
IndexError: list index out of range

################# Experiment ended with exit code 1 [pid:9680] #################

What I’m tryig to do:

1 - The participant see 4 boxes on the screen, 3 are colored and clickable (box1, box2, box3). As the participant clicks the colored boxes, the fourth box (polygon, not clickable) will change its color to match the color clicked.

It is a kind of conrifmation - click on red box, the empty polygon will turn red and so forth.

2 - The trial have a fixed duration (15 seconds). During that time, the participant may click as much as he/she wants and everytime the 3 boxes are clicked, the fourth box change its color accordingly.

3 - I need to output to the XLS file the time each box was clicked (reaction time) and the color of the box. So it makes it possible to compare clicking patterns among participants.

4 - I’m using random colors for the clickable boxes, from a conditions file. The loop randomizes the colors each time it runs the trials.

Ah, yes. The for loop should only run if something was already clicked. So,

if len(clicked_things) > 0:
    for clickable in clickables:
        if clickable.name == clicked_things[-1]:
            polygon.color = clickable.fillColor

Thank you a lot!! It worked like a charm.

But I still didn’t got how to retrive the clicks on a file. I used the code below but I’m not sure if it is the better way to make it

for clickable in clickables:
    if mouse.isPressedIn(clickable):
        clickedlist = clicked_things.append(clickable.color)
        thisExp.addData('color', clickedlist)
        thisClickTime = clickClock.getTime()
        clicks = thisClickTime
        thisExp.addData('RT', clicks)

clicks = [ ] is a vector which positions are the times mouse was clicked on a clickable

clickedlist = [ ] is a vector which positions are the names of the box colors’ clicked

Merging both vectors I expect to have a list containing the name of the color clicked and the times it was clicked.

The color names (‘blue’, ‘red’ etc) came from the conditions file and are randomized on each loop trial.

What I would do to optimize this and make it more stringent is the following:

Begin routine

clickables = [box1, box2, box3]
clickClock = core.Clock()

clickedBoxes = []
clickedColors = []
clickedTimes = []

lastState = False

Every frame

currentState = mouse.getPressed()[0]

if currentState and not lastState:
    for clickable in clickables:
        if mouse.isPressedIn(clickable):
            clickedColors.append(clickable.color)
            clickedTimes.append(clickClock.getTime())
            clickedBoxes.append(clickable.name)

if len(clickedBoxes) > 0:
    for clickable in clickables:
        if clickable.name == clickedBoxes[-1]:
            polygon.color = clickable.fillColor

lastState = currentState

End routine

thisExp.addData('box', clickedBoxes)
thisExp.addData('color', clickedColors)
thisExp.addData('RT', clickedTimes)
1 Like

The code is running great, but I couldn’t retrieve the clicks though. I have no problems running the experiment. However, checking the XLS data file generated it have the columns “box”, “color”, “RT” as follows:

box

[‘box1’, ‘box1’, ‘box3’, ‘box2’, ‘box1’, ‘box2’]

color

[<method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>, <method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>, <method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>, <method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>, <method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>, <method-wrapper ‘getattribute’ of attributeSetter object at 0x0000024E634900A0>]

RT

[0.8506655001547188, 1.1000177999958396, 3.7660185999702662, 6.981777100125328, 9.980472000082955, 12.06351160001941]

There are 6 repetitions on the experiment, so it is recording only the last clicked box? It would be nice to have all valid clicks on the boxes, as well the respective color of the box clicked and the time clicks occurred relative to the beginnig of the routine.

I tried changing mouse data to “routine” but it didn’t worked.

Is this the output of one or of multiple rows?

Concerning the color, maybe you have to change clickedColors.append(clickable.color) to clickedColors.append(clickable.fillColor) to extract the color as a string.

1 Like

Probably I’m missing out something on generating the XLS file. The experiment have 5 repetitions (loop), but I don’t know if this file recorded all clicks during the entire experiment or just the last click of each repetition.

Check this out: https://knowledgebase.constantcontact.com/tutorials/KnowledgeBase/6269-convert-a-text-file-to-an-excel-file?lang=en_US

Thank you!! But I don’t know why Psychopy aren’t recording all clicks during the trials. I guess it is retrieveing only the last run of the trials loop.

box

[‘box1’, ‘box3’, ‘box2’, ‘box1’, ‘box3’, ‘box2’, ‘box1’]

color

[array([ 1, 1, -1]), array([1. , 0.50588235, 0.59215686]), array([ 1, -1, -1]), array([ 1, 1, -1]), array([1. , 0.50588235, 0.59215686]), array([ 1, -1, -1]), array([ 1, 1, -1])]

RT

[4.099462900077924, 6.065001300070435, 7.032026400091127, 8.06427580001764, 10.080554499989375, 11.980671500088647, 13.895296700065956]

Could you please upload your psyexp and conditions file so so can have a look?

Sure!

Previa-Doutorado-v3.psyexp (82.0 KB)

conditions-teste.xlsx (8.5 KB)

You have to check “Is trials” in your loop settings.

Thank you a lot!! It is working just fine!

I have only two question regarding the interpretations of the results. I used a likert scale made with a slider (no code). The results are the response (point on slider) and the response time (rt), but I don’t know how to interpret them, as they are labelled as big numbers.

Should I mark on the mouse to get the time relative to the routine instead of the experiment?

Another question is regarding trials.thisindex. This index is the sequence of the presented stimulus during randomized trials?

  1. Could you confirm these high numbers by opening the csv file in a text editor? Sometimes Excel does weird things to numbers and there is no reason why these numbers should be this high.
  2. This index tells you which row this condition has in the original conditions file.

For instance I copied a segment of a row, both from the CSV on Wordpad and the same CSV imported into Excel:

Still don’t got what are these numbers. Are they the position of mouse on the slider, ranging from 1 to 10? And the .rt are the response times to click on the slider relative to the presentation of the slider?

.started and .stopped are the times on the experiment the mouse was started and stopped on a given routine?

Yes and Yes.

1 Like