Randomizing moving polygons

Hi! I am new to PsychoPy, and I am trying to make a dynamic setting.
It consists of sets of trials, that display circle polygons moving both vertically and horizontally. The movement of each polygon should be picked from a list of positions at random each trial.

I tried to randomize the movement using a script at the Each frame tab (code below).

I can make the movement work nicely (stable movement from side to side), but the issue is that the positions are not randomly picked from the list “positions”. Instead the first item is always picked. I tried shuffle function, and while it kind of works it makes the polygon’s movement flicker and “duplicates” the polygon, which is not suitable for this. I also tried to include in an excel file, but then getting an error “cannot convert string to float”, since there is a little math for the movement.

import random

posA = [sin(t)*.5, 0]
posB = [sin(t)*.5, 0.5]
posC = [sin(t)*.5, -0.5]
posD = [sin(t)*.5, -0.2]

positions = [posA, posB, posC, posD]

random.choice(positions)

Thank you!

Hello

You tell the program to always pick one position, positions[0]. You can use tthis approach when you use shuffle(). random.choice() returns a randomly selected element from the specified sequence. However, you do not assign it to anything.

Specify

pos = random.choice(positions)

and use pos as a variable in the parameter Position[x,y].

Best wishes Jens

1 Like

Are you importing random every frame?

1 Like

Thank you for replying! That certainly was one problem - now it chooses different positions going forward in the trials BUT it still chooses the same positions for all of the polygons, instead of the individual polygons moving to different positions.

Also I think the code stops affecting the polygons after adding 2, after that I get an UnboundLocalError: local variable ‘pos’ referenced before assignment.

I was, yes. After Jens’ asnwer I moved the code bit to the “Begin routine” tab to make it work.

Hello @Lussesi

Yes, my code just specifies one position and not four. You need to adapt the code to suit your needs. However, random.choice() selects one element from a list. So, repeatedly calling random.choice might result in selecting a certain position more than once.

Initialize the variable pos in a Begin Experiment tab.

You mentioned that shuffle() does not work?

Best wishes Jens

Hello @Lussesi

I tried to program a toy version of your experiment based on what I deduced from your description. Ok, the only variable part is whether the y-coordinate is 0, .5, -.5 or -.2. The x-coordinate is always sin(t)*.5.
Move your code component to the top line of your routine. In the Begin experiment tab, initialise the following variables (not really necessary if you are running the experiment locally and offline) and import the random library (note that you cannot use Python libraries if you intend to run the experiment online).

import random

posY = []
posX = [sin(1)*.5]

positions = [0, 0.5, -0.5, -0.2]

Randomly select one of four y-coordinate in the Begin routine tab

posY = random.choice(positions)

Create the movement along the x-axis in the Each frame tab

posX = sin(t)*.5

Store the selected posY-value in the End-routine-tab.

thisExp.addData('posY', posY)

Specify the location of the polygon in the Layout-tab of the polygon component and set set every frame.

Best wishes Jens

Hi!

Thank you so much for the prototype, I think I now understand much better what goes into which code tab, that was a little confusing at first. I took your advice and adjusted it to my idea, and now I too have a working prototype to try out! Thank you again :slight_smile: