0
我不斷收到此錯誤。這是完整的追溯。AttributeError:'pygame.Surface'對象沒有屬性'rect'
Traceback (most recent call last):
File "C:/Users/Luc/PycharmProjects/game_practice/game.py", line 23, in
<module>
run_game()
File "C:/Users/Luc/PycharmProjects/game_practice/game.py", line 13, in
run_game
player = Player(ai_settings, screen)
File "C:\Users\Luc\PycharmProjects\game_practice\player.py", line 8, in
__init__
self.rect.centerx = screen.rect.left
AttributeError: 'pygame.Surface' object has no attribute 'rect'
下面是這是使用的問題「玩家」文件中的代碼:
class Player:
def __init__(self, ai_settings, screen):
self.screen = screen
self.screen_rect = screen.get_rect()
self.rect = pygame.Rect((0, 0), (ai_settings.player_width,
ai_settings.player_height))
self.rect.centerx = screen.rect.left
self.rect.centery = screen.rect.ai_settings.screen_height/2
self.color = ai_settings.player_color
def draw_player(self):
pygame.draw.rect(self.screen, self.color, self.rect)
當我評論這兩條線:
self.rect.centerx = screen.rect.left
self.rect.centery = screen.rect.ai_settings.screen_height/2
該程序將在初始化的矩形運行(0,0)就像它應該的那樣,所以我不確定爲什麼當我包含其他行時該對象有一個屬性錯誤。