![](/img/trans.png)
[英]How to I modify my code so I can search for multiple inputs(from the same column) at the same time in Python
[英]How can I modify my code so that images from a list appear in rows and tiles?
我正在做的是创建一个记忆瓷砖游戏,它需要一个包含 8 个图像的列表,每个图像一次出现两次。 这是我的代码中有问题的部分。
self.images_1_2_8 = ['image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image8.bmp','image7.bmp','image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp']
self.collect_images = []
for select_images in self.images_1_2_8:
image = pygame.image.load(select_images)
#width = image.get_width()
#height = image.get_height()
random.shuffle(self.self.images_1_2_8)
for row_index in range(0, self.board_size):
row = []
for col_index in range(0,self.board_size):
width = image.get_width()
height = image.get_height()
x = width * col_index
y = height * row_index
tile = Tile(x,y, self.select_images_1_2_8[image_index], self.surface) row.append(tile)
self.board.append(row)
我不断收到的错误消息是:builtins.TypeError: 'pygame.Surface' object is not subscriptable
我发现这意味着getitem () 属性没有将我的图像作为列表读取? 我想这就是它的意思。 如果我删除列表 [row_index*self.board_size+col_index],图像就会出现(列表中只有一张图像,它填充了所有 16 个索引)。
我知道这是一个简单的修复,但我是 python 的新手,我已经在这工作了几个星期......如果有人能够提供帮助,请提前致谢!
看代码,应该从选择collect_images
而不是image
这仅仅是最后一个加载的(单一)图像(不是列表)。 代码就在那里,只需要调整一下。
self.images_1_2_8 = ['image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image8.bmp','image7.bmp','image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp']
self.collect_images = []
for select_images in self.images_1_2_8:
image = pygame.image.load(select_images)
self.collect_images.append(image) # <-- make the list in collect_images
#width = image.get_width()
#height = image.get_height()
random.shuffle(self.collect_images) # <-- shuffle loaded images
for row_index in range(0, self.board_size):
row = []
for col_index in range(0,self.board_size):
width = image.get_width()
height = image.get_height()
x = width * col_index
y = height * row_index
tile = Tile(x,y, collect_images[image_index], self.surface) # <-- use the image-list
row.append(tile)
self.board.append(row)
没有用于更新image_index
代码。 请确保这是从 0 增加到列表中的最大图像数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.