如何在 Psychopy 中使用累积按键更新多边形大小

How to update polygon size with cumulative key press in Psychopy

我正在使用 Psychopy 创建心理任务。对于给定的例程,我希望多边形(矩形)的高度随着每次按键(每次相同的键)而增加,直到达到最大按键次数(例如 10)。我不知道如何创建一个循环来计算同一例程中的按键次数,也不知道如何使用它来生成一个不断更新多边形大小的变量。

这是我在例程中尝试过的代码,它卡在 while 循环中...我不确定循环是否应该进入代码 "Before Routine" 或 "Each Frame"

total_key_count = 0 
while True: 
    resp_key = event.waitKeys(keyList=['1'])  
if resp_key == '1':
   total_key_count = total_key_count + 1 
# .. or break out of the loop if reach 10
elif total_key_count == 10:
   break  

谢谢!

切勿在 Builder 代码组件中使用 event.waitKeys()。 Builder 围绕一个绘图循环构建,该循环需要更新屏幕并在每次屏幕刷新时响应事件。如果您调用 waitKeys(),您将完全暂停执行,直到按下某个键,这将完全破坏 Builder 的时间结构。

Begin routine 选项卡中,输入:

key_count = 0
max_keys = 10

Each frame 选项卡中,输入:

key_press = event.getKeys('1')
if key_press: # i.e. if list not empty
    key_count = key_count + 1
    if key_count <= max_keys:
        # increment the height of the stimulus by some value
        # (use what is appropriate to its units):
        your_stimulus.size[1] = your_stimulus.size[1] + 0.1
    else:
        # terminate the routine (if required)
        continueRoutine = False

请注意,getKeys()waitKeys() 不同,它只是即时检查按键。即它不会暂停,等待钥匙。不过这很好,因为此代码将在每次屏幕刷新时 运行,直到按下所需数量的键。

想必您还需要保存一些关于响应的数据。这最好在 End routine 选项卡中完成,例如

thisExp.addData('completion_time', t) # or whatever needs recording