我正在编写的代码在屏幕上放置一个形状,并允许通过向上/向下箭头键操作该形状。我一直试图让它做的是使形状变化的数量取决于按键的顺序;只要输入与初始按键相同,形状变化量就会很大。然而,当按键的第一次“反转”发生时(例如,进行微调),在该点之后的每一次按键(不管它是否与最初的按键相同)都应该以小得多的比例改变圆圈(不是交互的,只是 0.1cm 变化而不是 2cm)。代码是用 Psychopy 编写的。
我想我还没有掌握应该为此设置循环的方式,但我看不出如何改变它们来做我想做的事情。为实际代码道歉,而不是一个最小的例子 - 任何建议都非常感谢。
for thisTrial in trials:
endKey = 0
nKeypress = 0
count = 0
counting = 0
if thisTrial == 'ellipse':
ellipseHeightinit = 7.6,1.9 + (round(numpy.random.uniform(-1,1),1))
elif thisTrial == 'circle':
ellipseHeightinit = 7.6,7.6 + (round(numpy.random.uniform(-1,1),1))
ellipseHeight = ellipseHeightinit
ellipseStim.setSize(ellipseHeight, log = False) # set the initial size of the shape
while endKey == 0:
ellipseStim.setAutoDraw(True)
win.flip() # flip the window to see the stimuli
allKeys = event.waitKeys()
if count < 1: #store the first keypress made
for thisKey in allKeys:
firstKeypress = thisKey
count += 1
event.clearEvents()
for thisKey in allKeys: # change the size of the shape depending on key pressed
if thisKey == 'up':
nKeypress = nKeypress + 1
elif thisKey == 'down':
nKeypress = nKeypress - 1
elif thisKey == 'space':
endKey = 1
while counting < 1: # attempt to make step size large until reversal
if thisKey == firstKeypress:
ellipseHeight = 7.6, ellipseHeightinit[1] + nKeypress*20
break
elif thisKey != firstKeypress:
ellipseHeight = 7.6, ellipseHeightinit[1] + nKeypress*0.1
counting += 1
break
ellipseStim.setSize(ellipseHeight, log = False) # set new shape size
ellipseStim.setAutoDraw(False)