首页 > 解决方案 > 将图像对象与屏幕位置坐标配对——python

问题描述

我正在使用 Python 构建游戏。为了跳过细节并直接进入问题,我每次在随机位置的屏幕上显示 3 张图像。

......
position1 = (250,0)
position2 = (0,0)
position3 = (-250,0)

all_combinations = list(itertools.permutations([position1, position2, position3]))
random.shuffle(all_combinations)

for combination in all_combinations:
    image1 = stimuli.Picture('images\image1.png', position=combination[0])
    image2 = stimuli.Picture('images\image2.png', position=combination[1])
    image3 = stimuli.Picture('images\image3.png', position=combination[2])
....

稍后在代码中,我将在屏幕上呈现这些图像。玩家必须使用键盘选择一张图像。我如何将每个图像与其屏幕坐标位置配对,因为每次都是随机的?最终目标是:如果 image1 在左侧,并且他们按下左键说“如果选择 image1 ... 执行此操作”,但我找不到指定哪个图像在左侧的方法。

谢谢!

标签: pythonimagecoordinatesscreen

解决方案


我不知道你为什么使用permutationshuffle如果你只需要shuffle

我会打乱文件名并以相同的顺序保持位置,然后第一个图像将在左侧,因为它具有最小的x( -250)

positions = [(-250,0), (0,0), (250,0)]

filenames = ['images\image1.png', 'images\image2.png', 'images\image3.png']
          
random.shuffle(filenames)

images = []

for name, pos in zip(filenames, positions):
    img = stimuli.Picture(name, position=pos)
    images.append( img )
    
left_img  = images[0]
left_pos  = positions[0]
left_name = filenames[0]

center_img  = images[1]
center_pos  = positions[1]
center_name = filenames[1]

right_img  = images[2]
right_pos  = positions[2]
right_name = filenames[2]

推荐阅读