简体   繁体   中英

How can I modify my code so that images from a list appear in rows and tiles?

What I am doing is creating a memory Tile game that requires a list of 8 images to appear at once twice each. This is the portion of my code that is have issues.

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)

The error message that I keep getting is: builtins.TypeError: 'pygame.Surface' object is not subscriptable

I found out that this means that the getitem () property isn't reading my images as a list? I think that's what it means. The images appear (only one image from the list, which fills all 16 indexes) if I remove the list [row_index*self.board_size+col_index].

I know it's an easy fix but I'm brand new to python and I've been at this for weeks now... Thank you in advance if anyone is able to help!

Looking at the code, it should be selecting from collect_images rather than image which is just the last (single) image loaded (not a list). The code's almost there, just needs a tweak.

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)

There's no code for the updating of image_index . Please ensure this is incrementing from 0 to within the maximum number of images in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM