Python3:记忆游戏中的碰撞点错误

时间:2019-12-04 18:31:34

标签: python-3.x pygame

我正在编写一个名为“ Memory”的游戏,该游戏包含8对图像(16个图像)以随机顺序排列。游戏开始时,其中16个必须显示相同的背景图像。玩家单击任何图块后,图块将从背景图像翻转到前景图像。 目前,我无法使用碰撞点方法翻转图块。请帮我。非常感谢你!

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 = []
      self.score = 0
      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)

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

   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
         elif event.type == pygame.MOUSEBUTTONUP:
            position = pygame.mouse.get_pos()
            if pygame.MOUSEBUTTONUP.collidepoint(one_tile):
               self.flip = True


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

   def update(self):
      self.score = pygame.time.get_ticks() // 1000

   def decide_continue(self):
      pass

   def draw_score(self):
      score_string = str(self.score)    
      score_font = pygame.font.SysFont('', 48)
      score_fg_color = pygame.Color('white')
      score_image = score_font.render(score_string, True, score_fg_color)
      self.surface.blit(score_image, (500,10))

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
      self.flip = False

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

   def draw(self):
      x = 0
      y = 0
      i = 0
      if self.flip == False:
         screen.blit(pygame.image.load('./images/bg.jpg'),(self.pos_x,self.pos_y))
      elif self.flip == True:
         screen.blit(self.image,(self.pos_x,self.pos_y))



main()

1 个答案:

答案 0 :(得分:2)

将类型为pygame.Rect.rect属性添加到类Tile中。矩形必须具有图块的位置和图像的大小。可以通过get_rect()从图像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.rect = self.image.get_rect(topleft = (self.pos_x,self.pos_y))
      self.surface = surface
      self.flip = False

   # [...]

当按下鼠标按钮时,遍历网格中的所有图块。使用collidepoint()验证鼠标是否在包含图块覆盖区域的矩形(tile.rect)上:

class Game:

   # [...] 

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

            # get mouse position
            position = event.pos
            # position = pygame.mouse.get_pos() # <--- this would do the same

            # for all tiles
            for row in self.grid:
               for tile in row:

                  # test if mouse is on the tile and flip it
                  if tile.rect.collidepoint(position):
                     tile.flip = True