我有大量不同的图像刺激呈现在屏幕上的不同位置。
当参与者单击刺激时,我需要该刺激项目的名称,以便在脚本的其余部分中使用。例如,您可以使用SlideState.HitTest(x, y)方法在example中实现这一点,其中x和y是鼠标坐标。
我所看到的唯一类似的东西是mouse.isPressedIn(shape)方法。但是,由于必须提供一个特定的对象作为参数,所以似乎每个刺激都需要一个if子句,这看起来很麻烦(特别是对于数量更多的项目)。
有更好的方法吗?我还在学习所以我可能遗漏了什么。
谢谢!
发布于 2015-11-18 18:13:49
不,我想不是。但是,如果您只是将所有对象添加到一个列表中并对它们进行循环,那么代码就足够整洁了。
# 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是该解决方案列表中的最新内容。
https://stackoverflow.com/questions/33783997
复制相似问题