在鼠标单击位置检测对象

Detect object at mouse click location

我在屏幕上的不同位置呈现了大量不同的图像刺激。

当参与者点击刺激物时,我需要该刺激物项目的名称可用于脚本的其余部分。 例如,您可以在 E-Prime 中使用 SlideState.HitTest(x, y) 方法实现此目的,其中 x 和 y 是鼠标坐标。

我在 Psychopy 中看到的唯一类似的东西是 mouse.isPressedIn(shape) 方法。但是,因为您必须提供一个特定的对象作为参数,所以您似乎需要为每个刺激提供一个 if 子句,这看起来很乱(尤其是对于大量项目)

有更好的方法吗?我还在学习,所以我可能会遗漏一些东西。

谢谢!

不,我不这么认为。但是,如果您只是将所有对象添加到列表中并循环遍历它们,代码将足够简洁。

# List of (pointers to) stimulus objects
shapes = [shape1, shape2, shape3, shape4, shape5]

# Loop over them and check if mouse is pressed on each one of them
for shape in shapes:
    if mouse.isPressedIn(shape):
        # Set this variable to point to the latest pressed shape
        pressed_shape = shape

# Now you can do stuff with this object
pressed_shape.fillColor = 'red'
pressed_shape.draw()
print pressed_shape.name

请注意,如果鼠标单击的位置有两个对象,pressed_shape 是此解决方案列表中的最新对象。