Creating barriers in a grid

Dear all,

I’m building an experiment in the builder, but inserting some code events into it. Basically, the experiment is a visual grid – a rectangle of 1000 x 500 pix (20 x 10 squares of 50 pix each) – in which a blue circle, which represents the subject, can move over the grid by pressing the arrow keys. I did this with the following code in the “Begin routine” of a code component I named “Subject_position”:

#Subject initial position
global Subject_position
Subject_position = [-475, -225]

and on the same component, but in “each frame”:

#Updat position

Mov = 50

if event.getKeys(keyList = ["right"]) and Subject_position[0] < 475:
    Subject_position[0] += Mov
elif event.getKeys(keyList = ["left"]) and Subject_position[0] > -475:
    Subject_position[0] -= Mov
elif event.getKeys(keyList = ["up"]) and Subject_position[1] < 225:
    Subject_position[1] += Mov
elif event.getKeys(keyList = ["down"]) and Subject_position[1] > -225:
    Subject_position[1] -= Mov

this worked well to allow the subject to move within the grid and do not cross the grid’s limits.

I also want to allow the participants to “build barriers”, represented by black squares of 50 x 50 pix, that, once built, “Subject” can’t move through it anymore. I managed to make them build the barriers by pressing the space key by creating another coder component I named “Barriers” and has the following codes in its “begin routine” tab:

global Barrier_list
Barrier_list = []

and the following code in the “each frame” tab:

Barrier = visual.Rect(
    win=win, name='Barrier',units='pix',
    width=(49.5, 49.5)[0], height=(49.5, 49.5)[1],
    ori=0, pos=[0,0],
    lineWidth=0, lineColor=[1,1,1], lineColorSpace='rgb',
    fillColor=u'black', fillColorSpace='rgb',
    opacity=1, depth=-5.0, interpolate=True)


if event.getKeys(keyList = ['space']) and Subject_position != [-475, -225] and Subject_position != [475, 225]:
    Barrier_position = Subject_position
    Barrier_list.append(Barrier_position)
    for Barrier_position in Barrier_list:
        Barrier.setAutoDraw(True)
        Barrier.setPos(Barrier_position, log=False)

So far it’s working very well to build the barriers in the right positions, but I haven’t been able to make the position in which the participant “built” a barrier a position in which they cannot move to anymore. I tried to modify the “Subject_position” component, including another condition in different ways, but none of them worked. I’ll exemplify bellow the two options I can’t see why not working:

if event.getKeys(keyList = ["right"]) and Subject_position[0] < 475 and [(Subject_position[0] + Mov), Subject_position[1]] not in Barrier_list:
    Subject_position[0] += Mov
elif event.getKeys(keyList = ["left"]) and Subject_position[0] > -475 and [(Subject_position[0] - Mov), Subject_position[1]] not in Barrier_list:
    Subject_position[0] -= Mov
elif event.getKeys(keyList = ["up"]) and Subject_position[1] < 225 and [Subject_position[0], (Subject_position[1] + Mov)] not in Barrier_list:
    Subject_position[1] += Mov
elif event.getKeys(keyList = ["down"]) and Subject_position[1] > -225 and [Subject_position[0], (Subject_position[1] - Mov)] not in Barrier_list:
    Subject_position[1] -= Mov

OR

if event.getKeys(keyList = ["right"]) and Subject_position[0] < 475:
    if [(Subject_position[0] + Mov), Subject_position[1]] not in Barrier_list:
        Subject_position[0] += Mov
elif event.getKeys(keyList = ["left"]) and Subject_position[0] > -475:
    if [(Subject_position[0] - Mov), Subject_position[1]] not in Barrier_list:
        Subject_position[0] -= Mov
elif event.getKeys(keyList = ["up"]) and Subject_position[1] < 225 :
    if [Subject_position[0], (Subject_position[1] + Mov)] not in Barrier_list:
        Subject_position[1] += Mov
elif event.getKeys(keyList = ["down"]) and Subject_position[1] > -225:
    [Subject_position[0], (Subject_position[1] - Mov)] not in Barrier_list:
        Subject_position[1] -= Mov

Would someone have an idea on how can I do this?

If this my description is not clear I can send the experiment.

Thanks,

Felipe

Could the issue be that you check Barrier_list only for movements to the right?

Jan

Thanks, Jan.
Actually I forgot to type this in here, but in PsychoPy it was right, so I don’t think this is the problem… Sorry, I have already edited the post.
Should be working, shouldn’t it?
Best and thanks
Felipe

Hi Felipe,

I think the problem might be that you do not create a deep copy of Subject_position. Try the following:

import copy

# ---snip---

        Barrier_position = copy.deepcopy(Subject_position)

Jan

It worked perfectly Jan. Thanks so much!

Should I create a new query regarding the creation of output data files I mentioned above?

Best,

Felipe

Yes, that might be a good idea.

I’m glad the barriers now work. :slight_smile:

Jan

Will do that.
Thanks!