python - 将图像对象与屏幕位置坐标配对——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 ... 执行此操作”,但我找不到指定哪个图像在左侧的方法。
谢谢!
解决方案
我不知道你为什么使用permutation
,shuffle
如果你只需要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]
推荐阅读
- node.js - 由于 stream-mmmagic,NPM 安装失败
- html - 什么是 HTML5 视频时间舍入阈值
- android - Android Studio:即使您在应用程序中滚动,如何创建一个保持锁定位置的视图?
- javascript - Ajax 不能通过从数据库中检索大文件来工作
- javascript - 在“orderBy”之后保留“GroupBy”对象格式
- vim - 为什么 Vim 将缩进与括号/冒号对齐?
- go - 如何调用实现接口的结构的特定方法
- python - django.urls.exceptions.NoReverseMatch:找不到“/”的反向。“/”不是有效的视图函数或模式名称
- php - Laravel VPS 安装问题 - Ubuntu 20.04
- heroku - 登录 Heroku CLI 时 IP 地址不匹配