Hu Jack
Hu Jack

Reputation: 65

Python3 Memory game random shuffle images

I am working on a game called "Memory" in pygame. I am trying to blit images on the surface of the surface. I want to get 8 images blitted in pairs but random order each run. However, It shows 16 same images instead of 2 groups of 8 different images in the 4*4 grid. I don't know where should I change to solve this problem. Someone, please help me! Thank you very much.

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

Upvotes: 2

Views: 575

Answers (1)

Rabbid76
Rabbid76

Reputation: 210889

The mayor issue is, that the shuffled images are contained in self.shuffle, rather than 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) 

The index of the image can be calculated by 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  

Note self.grid = [ [] ] is a list with 1 element, which is another list. It has to be self.grid = [].

The line

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

would cause that the images are load form the file every time when a image is blit to the window surface. This would gain an performance impact.

Preload the images:

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 the laded Surfaces to the window:

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

Upvotes: 2

Related Questions