Python Pygame对象没有属性';rect';(pygame)

Python Pygame对象没有属性';rect';(pygame),python,pygame,attributes,Python,Pygame,Attributes,我目前正在制作一款学校游戏《记忆》,其中包括匹配隐藏在封面下的8对图像。我成功地设置了4x4网格,带有一个记分计时器,并对图像的位置进行了随机化,但是移除瓷砖上的盖子给了我一些问题。我最终得到了错误:builtins.AttributeError:type对象“Tile”没有属性“rect” 回溯是: Traceback (most recent call last): File "/home/user/Documents/Lab 4/memory2cover.py", line 179,

我目前正在制作一款学校游戏《记忆》,其中包括匹配隐藏在封面下的8对图像。我成功地设置了4x4网格,带有一个记分计时器,并对图像的位置进行了随机化,但是移除瓷砖上的盖子给了我一些问题。我最终得到了错误:builtins.AttributeError:type对象“Tile”没有属性“rect”

回溯是:

Traceback (most recent call last):
  File "/home/user/Documents/Lab 4/memory2cover.py", line 179, in <module>
    main()
  File "/home/user/Documents/Lab 4/memory2cover.py", line 21, in <module>
    game.play()
  File "/home/user/Documents/Lab 4/memory2cover.py", line 92, in <module>
    self.handle_events()
  File "/home/user/Documents/Lab 4/memory2cover.py", line 117, in <module>
    Tile.handle_click(Tile, event)
  File "/home/user/Documents/Lab 4/memory2cover.py", line 175, in <module>
    if self.rect.collidepoint(pos):
builtins.AttributeError: type object 'Tile' has no attribute 'rect'
我不太清楚为什么它说它没有“rect”属性

这是我的全部代码

# Memory 1

import pygame
import random

# User-defined functions
# board[row_index][col_index]

def main():
   # initialize all pygame modules (some need initialization)
   pygame.init()
   # create a pygame display window
   pygame.display.set_mode((500, 400))
   # set the title of the display window
   pygame.display.set_caption('Memory')   
   # get the display surface
   w_surface = pygame.display.get_surface() 
   # create a game object
   game = Game(w_surface)
   # start the main game loop by calling the play method on the game object
   game.play() 
   # quit pygame and clean up the pygame window
   pygame.display.quit()
   pygame.quit() 


# User-defined classes

class Game:
   # An object in this class represents a complete game.

   def __init__(self, surface):
      # Initialize a Game.
      # - self is the Game to initialize
      # - surface is the display window surface object

      # === objects that are part of every game that we will discuss
      self.surface = surface
      self.bg_color = pygame.Color('black')

      self.FPS = 70
      self.game_Clock = pygame.time.Clock()
      self.close_clicked = False
      self.continue_game = True

      # === game specific objects
      Tile.set_surface(self.surface) # this is how you call a class method
      self.board_size = 4
      self.board = []
      self.score = 0
      self.load_images()
      self.create_board()
      self.image_index = 0


   def load_images(self):
      # loads a specified amount of images into a list
      # that list of images is then shuffled so an images index is randomized
      self.image_list = []
      for self.image_index in range(1,9):
         self.image_list.append(pygame.image.load('image' + str(self.image_index) + '.bmp'))
      self.full_list = self.image_list + self.image_list
      random.shuffle(self.full_list)

   def create_board(self):
      # multiple rows and columns are appended to the board with the tiles added
      # utlizes the different images for each tile
      index = 0
      img = self.full_list[0]
      width = img.get_width()
      height = img.get_height()
      for row_index in range(0,self.board_size):
         self.row = []
         for col_index in range(0,self.board_size):
            #item = (row_index,col_index)
            # game = Game(), dot1 = Dot(), student = Student()
            image = self.full_list[index]
            x = width * col_index
            y = height * row_index      
            a_tile = Tile(x,y,width,height, image)
            self.row.append(a_tile)
            index = index + 1
         self.board.append(self.row)


   def play(self):
      # Play the game until the player presses the close box.
      # - self is the Game that should be continued or not.

      while not self.close_clicked:  # until player clicks close box
         # play frame
         self.handle_events()
         self.draw()            
         if self.continue_game:
            self.update()
         self.game_Clock.tick(self.FPS) # run at most with FPS Frames Per Second 

   def draw_score(self):
      fg = pygame.Color('white')
      bg = pygame.Color('black')
      font = pygame.font.SysFont('',70)
      text_box = font.render(str(self.score),True,fg,bg)
      #x = self.surface.get_width() - text_box.get_width()
      #y = self.surface.get_height() - text_box.get_height()
      location = (self.surface.get_width() -70,0)
      self.surface.blit(text_box,location)   

   def handle_events(self):
      # Handle each user event by changing the game state appropriately.
      # - self is the Game whose events will be handled

      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True
         for item in self.row:
            Tile.handle_click(Tile, event)

   def draw(self):
      # Draw all game objects.
      # - self is the Game to draw
      coordinates = self.create_board()

      # clear the display surface first
      self.surface.fill(self.bg_color) 
      # drawing of the board
      for row in self.board:
         for tile in row:
            tile.draw()
      self.draw_score()
      pygame.display.update() # make the updated surface appear on the display

   def update(self):
      # Update the game objects for the next frame.
      # - self is the Game to update
      self.score = pygame.time.get_ticks()//1000



class Tile:
   # An object in this class represents a Tile 
   # Shared or Class Attributes
   surface = None
   fg_color = pygame.Color('black')
   border_width = 10
   cover = pygame.image.load('image0.bmp')

   @classmethod
   def set_surface(cls,surface_from_Game):
      cls.surface = pygame.display.get_surface()

   def __init__(self, x,y,width,height, image):
      # Initialize a Tile.
      # - self is the Tile to initialize
      # - x,y top left coordinates of the rectangle
      # - - width, height are the dimensions of the rectangle
      # Instance Attributes or Object Attributes
      self.rect = pygame.Rect(x,y,width,height)
      self.image = image
      self.covered = True

   def draw(self):
      # Draw the dot on the surface
      # - self is the Tile
      # then blit the tile content onto the surface
      pygame.draw.rect(Tile.surface, Tile.fg_color, self.rect,Tile.border_width)
      if self.covered:
         Tile.surface.blit(Tile.cover, self.rect)
      else:
         Tile.surface.blit(self.image, self.rect)

   def handle_click(self, event):
      if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
         pos = pygame.mouse.get_pos()
         if self.rect.collidepoint(pos):
            self.covered = False


main()
问题是:

Tile.handle_click( Tile, event )
代码混淆了
Tile
对象的类定义和实例化对象,比如它的“实时副本”~
a_Tile
。因此,当它调用
Tile.something()
时,
\uuu init\uuuu()
从未被调用过,因此
.rect
不存在。当然,
a_tile
有一个
.rect
,因为它已经被实例化了

可能应该是这样的:

   def handle_events(self):
      # Handle each user event by changing the game state appropriately.
      # - self is the Game whose events will be handled

      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True
         for item in self.row:
            item.handle_click( event )    # <-- HERE
def handle_事件(自):
#通过适当更改游戏状态来处理每个用户事件。
#-赛尔夫是将处理其事件的游戏
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:
self.close\u clicked=True
对于self.row中的项目:
item.handle_click(event)#问题是:

Tile.handle_click( Tile, event )
代码混淆了
Tile
对象的类定义和实例化对象,比如它的“实时副本”~
a_Tile
。因此,当它调用
Tile.something()
时,
\uuu init\uuuu()
从未被调用过,因此
.rect
不存在。当然,
a_tile
有一个
.rect
,因为它已经被实例化了

可能应该是这样的:

   def handle_events(self):
      # Handle each user event by changing the game state appropriately.
      # - self is the Game whose events will be handled

      events = pygame.event.get()
      for event in events:
         if event.type == pygame.QUIT:
            self.close_clicked = True
         for item in self.row:
            item.handle_click( event )    # <-- HERE
def handle_事件(自):
#通过适当更改游戏状态来处理每个用户事件。
#-赛尔夫是将处理其事件的游戏
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:
self.close\u clicked=True
对于self.row中的项目:

item.handle_click(event)#请更新此问题的相关信息。我们希望代码能够重现问题,以及完整的错误消息——包括回溯。您让我们不得不猜测问题出现在30行代码中的何处,以及是什么变量或参数值引发了问题。请包括调用
Tile.draw()
的代码。添加了完整代码,对此表示抱歉。请更新此问题。我们希望代码能够重现问题,以及完整的错误消息——包括回溯。您让我们不得不猜测问题出现在30行代码中的何处,以及是什么变量或参数值引发了问题。请包括调用
Tile.draw()
的代码。添加了完整代码,对此表示抱歉。这很有意义,最终解决了回溯问题。不过现在我的封面还没揭掉,所以我不得不在我的瓷砖课上胡闹。谢谢你的回答和时间。不用担心,很高兴我能帮上忙。这很有意义,最终解决了回溯问题。不过现在我的封面还没揭掉,所以我不得不在我的瓷砖课上胡闹。谢谢你的回答和时间。不用担心,很高兴我能帮上忙。