Python3 Memory游戏随机随机播放图像

时间:2019-12-03 20:12:17

标签: python-3.x pygame

我正在pygame中开发一款名为“ Memory”的游戏。我正在尝试使表面上的图像模糊不清。我想获得8张成对成对的图像,但每次运行随机排列。但是,它在4 * 4网格中显示16个相同的图像,而不是2组的8个不同的图像。我不知道应该在哪里更改以解决此问题。有人请帮帮我!非常感谢。

import pygame
import random
import time
screen = pygame.display.set_mode((525, 435))

def main():
   pygame.init()
   pygame.display.set_mode((535, 435))
   pygame.display.set_caption('Memory')
   w_surface = pygame.display.get_surface() 
   game = Game(w_surface)
   game.play()
   pygame.quit()

class Game:

   def __init__(self, surface):
      self.surface = surface
      self.bg_color = pygame.Color('black')
      self.FPS = 10
      self.game_Clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True
      Tile.set_surface(self.surface)
      self.grid_size = 4
      self.grid = [ [] ]
      image1 = './images/1.jpg'
      image2 = './images/2.jpg'
      image3 = './images/3.jpg'
      image4 = './images/4.jpg'
      image5 = './images/5.jpg'
      image6 = './images/6.jpg'
      image7 = './images/7.jpg'
      image8 = './images/8.jpg'
      self.images = [
         image1, image1,
         image2, image2,
         image3, image3,
         image4, image4, 
         image5, image5,
         image6, image6,
         image7, image7, 
         image8, image8
         ]
      self.shuffle = [i for i in range(len(self.images))]
      random.shuffle(self.shuffle) 
      self.create_grid(self.grid_size)


   def create_grid(self, grid_size):
      for row_num in range(grid_size):
         new_row = self.create_row(row_num, grid_size)
         self.grid.append(new_row)

   def create_row(self, row_num, size):
      tile_height = self.surface.get_height() // size
      tile_width = self.surface.get_width() // (size+1)
      one_row = [ ]
      for col_num in range(size):
         y = row_num * tile_height + 5
         x = col_num * tile_width + 5
         for i in range (self.grid_size):
            one_tile = Tile(x, y, tile_width, tile_height, self.images[i], self.surface)
            i += 1
         one_row.append(one_tile)
      return one_row

   def play(self):
      while not self.close_clicked:
         self.handle_events()
         self.draw()
         if self.continue_game:
            self.update()
            self.decide_continue()
         self.game_Clock.tick(self.FPS)

   def handle_events(self):
      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True

   def draw(self):
      self.surface.fill(self.bg_color)
      for row in self.grid:
         for tile in row:
            tile.draw()
      pygame.display.update()

   def update(self):
      pass

   def decide_continue(self):
      pass


class Tile:
   def __init__(self, pos_x, pos_y, tile_numx, tile_numy, imagelist, surface):
      self.pos_x = pos_x
      self.pos_y = pos_y
      self.numx = tile_numx
      self.numy = tile_numy
      self.imagelist = imagelist
      self.surface = surface

   @classmethod
   def set_surface(cls, surface):
      cls.surface = surface   

   def draw(self):
      x = 0
      y = 0
      screen.blit(pygame.image.load(self.imagelist),(self.pos_x,self.pos_y))



main()

1 个答案:

答案 0 :(得分:2)

市长的问题是,经过改组的图像包含在self.shuffle中,而不是self.images中:

one_tile = Tile(x, y, tile_width, tile_height, self.images[i], self.surface)

one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface) 

可以通过i = col_num*size + row_num计算图像的索引:

class Game:

   # [...]

   def create_grid(self, grid_size):
      for row_num in range(grid_size):
         new_row = self.create_row(row_num, grid_size)
         self.grid.append(new_row)

   def create_row(self, row_num, size):
      tile_height = self.surface.get_height() // size
      tile_width = self.surface.get_width() // (size+1)
      one_row = [ ]
      for col_num in range(size):
         x, y = col_num * tile_width + 5, row_num * tile_height + 5
         i = col_num*size + row_num
         one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface)
         one_row.append(one_tile)
      return one_row  

注意self.grid = [ [] ]是一个包含1个元素的列表,这是另一个列表。它必须是self.grid = []

  
screen.blit(pygame.image.load(self.imagelist),(self.pos_x,self.pos_y))

每次图像blit到窗口表面时,都会导致图像从文件加载。这将对性能产生影响。

预载图像:

class Game:

   def __init__(self, surface):
      self.surface = surface
      self.bg_color = pygame.Color('black')
      self.FPS = 10
      self.game_Clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True
      Tile.set_surface(self.surface)
      self.grid_size = 4
      self.grid = []

      imgnames = ['./images/' + str(i) + '.jpg' for i in range(1,9)]
      self.images = [pygame.image.load(name) for name in imgnames]
      self.shuffle = self.images + self.images
      random.shuffle(self.shuffle) 

      self.create_grid(self.grid_size)

blit到窗口的阶梯曲面:

class Tile:
   def __init__(self, pos_x, pos_y, tile_numx, tile_numy, image, surface):
      self.pos_x = pos_x
      self.pos_y = pos_y
      self.numx = tile_numx
      self.numy = tile_numy
      self.image = image
      self.surface = surface

   @classmethod
   def set_surface(cls, surface):
      cls.surface = surface   

   def draw(self):
      self.surface.blit(self.image,(self.pos_x,self.pos_y))