I am running an offline experiment in PsychoPy Builder where one of the tasks that participants are prompted to do is to draw the line. I have adapted a custom code to track the mouse position when the brush component is used and it works by itself perfectly. However, the problem that seems to be occurring is that the entire experiment ends after I finish the drawing routine even though I have more routines after. I am guessing that if I add something to the End Routine tab of the code component, the drawing routine can progress into the next routine, however, I am having difficulty coming up with a solution. You can find the code component in draw_small_square Routine, the code itself is under Begin Routine. Any help would be appreciated. exp.imagery.psyexp (101.9 KB)
# set the circle to the mouse position
circ = visual.Circle(win, size=0.02, lineColor='white', fillColor='white', pos=(0, 0))
line_paths = [[0, 0]]
line_path = []
wait_click = True
# wait for first click and start tracking mouse movements
while wait_click:
mouse_click = mouse.getPressed()
if mouse_click[0] or mouse_click[1] or mouse_click[2] == 1:
# First we just need to wait for the button to be released
mouse_down = True
while mouse_down:
mouse_click = mouse.getPressed()
if mouse_click[0] or mouse_click[1] or mouse_click[2] == 1:
pass
else:
mouse_down = False
wait_click = False
# Now we start a separate while loop for the drawing.
# None of this will execute until the above code waiting for the mouse press and release has finished
x_start, y_start = mouse.getPos()
line_path.append(visual.Line(win, start=(x_start, y_start), end=(x_start, y_start), lineColor='white', lineWidth=2))
create_line = True
mouse_down = False
mouse.clickReset()
while create_line:
# start tracking mouse movements and draw line every frame
mouse_pos = mouse.getPos()
line_paths.append(mouse_pos)
#append the line path with start at the last mouse position and end at the current mouse position
line_path.append(visual.Line(win, start=(line_paths[-2][0], line_paths[-2][1]), end=(mouse_pos[0], mouse_pos[1]), lineColor='white', lineWidth=4))
for line in line_path:
line.setAutoDraw(True)
circ.pos = mouse_pos
circ.autoDraw = True
win.flip()
mouse_click = mouse.getPressed()
# Setting it up this way so it will continue to draw until they RELEASE the mouse button
if mouse_click[0] or mouse_click[1] or mouse_click[2] == 1 and not mouse_down:
mouse_down = True
elif mouse_down: # i.e., the mouse WAS down, but now none of the buttons are pressed, end loop
create_line = False
This has not been written to work well with PsychoPy Builder. Avoid while and flip statements.
Instead you should write code that works in an Each Frame tab (which has an automatic win.flip() at the end of each frame) and don’t stop the programme while waiting for the mouse press – just stop the appropraite code from being executed.
Thanks for a quick reply! Do you have suggestions on how exactly I can tweak the code that I have, what would I replace a while loop with (for loop?)? I just wrap my head around how the experiment ends even though I never specified that it should end in the code. I hope I don’t have to rewrite the entire code because it does exactly what I need. But I am not that comfortable with how PsychoPy Coder works, and coding in general, and especially how it combines the code component into the overall code, so I’ve been stuck with problems for some time now. Any advice would be super helpful!