【发布时间】:2021-03-25 04:11:37
【问题描述】:
我的任务是设计一个方块。它将随机从屏幕的一侧运行到另一侧。有时会发出哔哔声。一听到哔声,就必须按下按钮(这里是键盘)。问题是,当我使用“getkeys”方法时,它总是返回一个空列表。 RT 显示它主要获取缓冲区中的第一个输入。我也试过eventclear,但似乎没用。
这里是代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from psychopy import visual, core, event,gui
from psychopy import sound as sd
#from psychopy.iohub.client.keyboard import Keyboard as kbd
import random, math,time
from psychopy.iohub import launchHubServer
import numpy as np
win = visual.Window(fullscr = False, size = [960,540], color = 'black', units = 'pix')
io = launchHubServer()
kbd = io.devices.keyboard
x,y = win.size
ran = [random.randint(-400,400),random.randint(-400,400),random.randint(-150,150),random.randint(-150,150)]
mode = random.randint(0,3)
start_pos = [[ran[mode],-269],[ran[mode],269],[-479,ran[mode]],[479,ran[mode]]]
rect = visual.Rect(win, width = 50, height = 50, fillColor='white', pos = start_pos[mode], name = 'target')
timer = core.Clock()
dx = [0,0,1,-1]
dy = [1,-1,0,0]
def present(rect,t):
timer.reset()
while timer.getTime() < t:
rect.draw()
win.flip()
def movement(rect,time):
event.clearEvents()
x,y = win.size
timer.reset()
timer_2 = core.Clock()
timer_2.reset()
jump = True
while timer.getTime() < time and (abs(rect.pos[0])< x/2 and abs(rect.pos[1])< y/2):
timeUse = timer_2.getTime()
timer_2.reset()
a,b = rect.pos
rect.pos += [dx[mode]*timeUse*100,dy[mode]*timeUse*100]
if int(timer.getTime()) == 3 :
if jump :
sd.Sound('D', octave = 5).play()
#print("D" , timer.getTime())
if jump :
timer_jump = core.Clock()
timer_jump.reset()
print(timer_jump.getTime(),'++++++++++')
event.clearEvents()
if timer.getTime() < time and jump :
print('============')
#k_1 = event.getKeys(keyList = ['a','z'])
while event.getKeys() is not None:
RT = timer_jump.getTime()
print(RT,'--------')
break
jump = False
#print([timer.getTime(),time])
#print(rect.pos)
rect.draw()
win.flip()
present(rect,1)
movement(rect,15)
我会非常感谢谁提供帮助。
食堂
【问题讨论】:
-
@Michael MacAskill 也许你能帮帮我