在我的实验中,我有10次试验,在每次试验中,参与者在看到动画中的颜色变化后必须尽快按下一个键("空格"(。作为确认按键的反馈,我想在按键按下后进入下一个试用(移动到下一个循环迭代(。我试图用break
和continue
在我的代码中实现这个想法,但它不起作用:
for i in range(nTrials):
# Start with fixation cross
fixation.draw()
win.flip()
core.wait(2)
# Play the video for 200 frames
for Nframes in range(200):
optic_flow_movie.draw()
fixation.draw()
win.flip()
# Get Participants responses
keys = psychopy.event.getKeys(keyList=["space"],timeStamped=clock)
if (keys[0][0] == 'space') is True:
break
else:
continue
# Take only the timestamps from the variable key and store it in the variable RTs
RTs = [sublist[1:2] for sublist in keys] # This stores only the timestamps
RTs = [item for sublist in RTs for item in sublist] # This converts from list of lists to a flat list
非常感谢您的帮助!
目前还不完全清楚试验的结构是什么,但是如果要在动画期间监视响应,则需要将event.getKeys()
调用嵌入到绘图循环中(即在for Nframes in range(200):
内(。这样,您就可以在每次屏幕刷新时检查按键,以便可以实时停止动画。
目前,代码显示整个动画,然后才检查键盘(但每次试用一次(。无论那里发生什么,下一次试验都将开始,因为这是主试验循环中的最后一个代码(即for i in range(nTrials):
.
最后,event
模块已弃用键盘响应。您真的应该转移到较新的Keyboard
类以获得更好的性能:
https://www.psychopy.org/api/hardware/keyboard.html
https://discourse.psychopy.org/t/3-ways-to-get-keyboard-input-which-is-best/11184/3