Trouble with advancing to next trial

Hi everyone, I am trying to create an experiment where participants will see 12 unique line lengths, and then after the 500 ms blank, there will be a reproduction where they will use the right and left keys to increase and decrease the line they just saw. So far, I created the lines but I want them to press “d” to receive the next stimulus but this part doesn’t work. What could be the problem? I am attaching screenshots of the code.

Thanks!!





Loads of that code is stuff that’s already handled by the builder itself, so I think the issues you’re running into are due to 1) conflict between what the builder/components and code are doing, and 2) having extensive code in the Begin Routine tab which is probably preventing the trial from advancing due to the while True statement.

I think you’ll need to re-work it to check for left/right keypresses in the Every Frame tab, in a manner that doesn’t rely on staying inside the loop given this is designed to repeat every 16ms (e.g., at the start of each frame, check whether left/right was pressed, increase/decrease the line if so, or end the trial if d was pressed). You’ll also need to set the keyboard component so it doesn’t end the routine, as this would apply to any of the “Allowed keys”:

Begin Routine:

keyIncrement = 8
currentLineLength = 100

Every Frame:

keys = event.getKeys(['left', 'right', 'd'])
if 'd' in keys:
    continueRoutine = false
if 'left' in keys:
    currentLineLength -= keyIncrement
if 'right' in keys:
    currentLineLength += keyIncrement

I don’t think you’ll want to set up a window in the Before Experiment tab there either - I think this will just get overwritten by the builder’s one anyway

1 Like

Hi, thanks!-so I deleted the window set up in before experiment, unchecked the force end routine box and first just wrote the code you said but in this reproduction trial participants are supposed to reproduce a line with key presses and you don’t think that part is needed?:



Update line endpoints based on adjusted length (vertical line)

start_y = -currentLineLength / 2

end_y = currentLineLength / 2

x_pos = 0 # Keep x-coordinate at zero for vertical line

# Create a vertical line

line = visual.Line(

win=win, start=(x_pos, start_y), end=(x_pos, end_y),

lineWidth=2.0, lineColor=‘white’, units=‘pix’

)

# Draw the updated line

line.draw()

# Update the PsychoPy window

win.flip()

and then the other one(attaching the screenshot below) but now with both ones I don’t see the lines in the reproduction trial. I just see the first line and then a blank page. (I was seeing the lines with the initial code I wrote in the image I attached.)**

And sometimes when I run the experiment I get:window not defined or something like that so I thought I should initialize it at the before experiment.

Crib sheet also states that don’t use win.flip() , and visual.Line instead use visual.Polygon or visual.ShapeStim but I was still able to run the study. Should i also change these as well?

Many thanks!!

I am also trying another response version of this, instead of getting keypress response, participants will adjust the line length with mouse scroll up& downs. I already have this code but this one didn’t work as well. What do you think is the issue? (While true?)

my_mouse = Mouse()
my_canvas = Canvas()
my_mouse.show_cursor(True)
my_mouse.set_pos(pos=(0.0,0.0))
my_canvas.show()

while True:
var.init_pos = my_mouse.get_pos()
button, start_pos, timestamp = my_mouse.get_click(timeout=20, visible=True)
var.start_resp = timestamp
if button is not None:
var.sx, var.sy = start_pos
ex, ey = start_pos

    while any(my_mouse.get_pressed()):
        (var.ex, var.ey), timestamp = my_mouse.get_pos()
        var.end_resp = timestamp
        my_canvas.clear()
        my_canvas.line(var.sx, var.sy, var.ex, var.ey)
        my_canvas.show()
    break

Thanks!!!

I’m not especially familiar with visual.line or using a polygon to draw a line tbh, but you will need code to draw/update the line if this isn’t handled by a component. I expect you’ll want identical code in the Begin Routine and Each Frame tabs to draw/update the line (first before the trial begins, then in Each Frame to continuously update it based on keypresses), but win.flip() shouldn’t be neccesary

Okay, but what do you think is the problem with the keyboard response? I did what you said and now I can’t see the line in the reproduction phase.

Thanks!!

Do you have a keyboard component and event.getKeys in code both monitoring the keyboard?