Python3 内存游戏随机洗牌图像

标签 python 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()

最佳答案

主要问题是,打乱的图像包含在 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 将装载的 Surface 传送到窗口:

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))

关于Python3 内存游戏随机洗牌图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59164645/

相关文章:

python - 有没有一种方法可以减少pygame的更新?

python - Pygame Sprite 双跳

python - 使用 Python,np.gradient 计算 u 和 v 的风散度

python - Selenium 和Python : Trouble with clicking a button with "data-disable-with" attribute

python-3.x - 更改文本文档的值时遇到问题

python - 在 Python 中使用 concurrent.futures 的多线程不起作用

python - 通过 sqlalchemy 的 impala 连接

python - 我无法让 'player' 在 pygame 中双向移动,我该怎么做?

python - 在 Pandas 数据框中的特定时间之间选择数据

python - flask 管理员/ flask -SQLAlchemy : set user_id = current_user for INSERT