【发布时间】:2017-10-02 18:54:17
【问题描述】:
我在 Psychopy 中创建了两个刺激(红色和绿色矩形)作为刺激。另外,我已经启用了四个方向的鼠标移动。使用 Psychopy 中的函数 [mouse.getPressed()] 来选择刺激,我遇到了一些问题。 基本上,我希望鼠标向四个方向移动,当鼠标到达红色/绿色矩形刺激时,我需要选择该刺激并将其颜色更改为蓝色。
任何人都可以调查这个问题并帮助我解决同样的问题吗?
这是我的代码:
from psychopy import visual, core, event
import numpy as np
# Create a window.
# For configuring and debugging the code turn off full screen.
fullscr = False
win = visual.Window(
[1200,1000],
monitor="testMonitor",
units="deg",
fullscr=fullscr
)
#cursor = visual.Circle(win, radius=0.2)
#cursor = visual.CustomMouse(win,
# leftLimit=-10, topLimit=10, rightLimit=10, bottomLimit=-10,
# showLimitBox=True, clickOnUp=True)
pos_zero = (0, 0)
cursor = visual.Rect(
win=win,
size=400,
pos=pos_zero,
opacity=0
)
mouse = event.Mouse(visible=True)
# Sinusoidal control version.
freq_one = 0.5
freq_two = 1.5
# Colors of the rectangles.
#color_zero='black'
color_one = 'red'
color_two = 'green'
# Positions of the rectanges.
pos_one = (-10, 0)
pos_two = (10, 0)
start = core.getTime()
cnt = 0
cursor.pos = mouse.getPos()
print cursor.pos
while cnt<600:
second = core.getTime() - start
sin_val_one = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_one))
sin_val_two = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_two))
#while not mouse.getPressed()[0]:
# Do something if mouse moved
for key in event.getKeys():
if key == 'escape':
core.quit()
elif key == "right":
cursor.pos = cursor.pos + (2,0)
elif key =="left":
cursor.pos = cursor.pos - (2,0)
elif key =="up":
cursor.pos = cursor.pos + (0,2)
elif key =="down":
cursor.pos = cursor.pos - (0,2)
#if cursor.pos == pos_one:
# mouse.getpressed(rect_one)
#elif cursor.pos == pos_two:
# mouse.getpressed(rect_two)
mouse.setPos(cursor.pos)
mouse.lastPos = cursor.pos
rect_one = visual.Rect(
win=win,
fillColor=color_one,
lineColor=color_one,
size=15,
pos=pos_one,
opacity=sin_val_one
)
rect_two = visual.Rect(
win=win,
fillColor=color_two,
lineColor=color_two,
size=15,
pos=pos_two,
opacity=sin_val_two
)
#images = [rect_one, rect_two]
#for image in images:
# if mouse.isPressedIn(image):
# pressed_shape = shape
#
# pressed_image.fillColor = 'blue'
# pressed_image.draw()
# print pressed_image.name
rect_one.draw()
rect_two.draw()
cursor.draw()
win.flip()
cnt += 1
win.close()
非常感谢任何帮助。谢谢!
【问题讨论】:
-
您想在光标重叠 (
cursor_object.overlaps(rect_one)) 或鼠标按下 (mouse.isPressedIn(rect_one)) 时更改颜色吗?还有一个更笼统的问题:为什么要用键盘来控制鼠标?如果受试者永远不会使用鼠标,那么只需使用键盘移动图像左右即可。 -
我希望鼠标到达图像时颜色会发生变化。它可以是任何一个。我不知道重叠功能,所以我想到了使用mouse.isPressed。我计划将它与大脑信号采集板集成,信号将用于鼠标移动。但就目前而言,我正在制作一个原型来测试键盘功能。我希望在光标到达那里时选择图像。该模块用于与更大的模块集成。谢谢。
标签: python-2.7 mouseevent psychopy