@Wenrui , you can use the following in the “End Routine” tab, where your keyboard component is called “key_resp”, and the keyboard is set to record “all keys”:
def getRT(kb, player):
# Get first response or return false for no resp
idx = 0
for key in kb.keys:
if key == player:
return kb.rt[idx]
idx += 1
return False
def getDiff(p1, p2):
# draw
if abs(p1 - p2) < .050:
return 0
# p1 win
elif (p2 - p1) > .050:
return 1
# p2 win
elif (p1 - p2) > .050:
return 2
# Get first response from each player
p1RT = getRT(key_resp, 'z')
p2RT = getRT(key_resp, 'm')
# If both responded
if p1RT and p2RT:
result = getDiff(p1RT, p2RT)
thisExp.addData("result", result)
thisExp.addData("p1RT", p1RT)
thisExp.addData("p2RT", p2RT)
# no responses from p1 or p2
elif not p1RT and not p2RT:
thisExp.addData("result", 0)
# p2 no response - p1 wins
elif p1RT:
thisExp.addData("result", 1)
thisExp.addData("p1RT", p1RT)
# p1 no response - p2 wins
elif p2RT:
thisExp.addData("result", 2)
thisExp.addData("p2RT", p2RT)
You can put those functions in the Begin Experiment tab.