pygame " AttributeError: 'Star' object has no attribute 'add_internal "中的属性错误


class StarsGame:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen  = pygame.display.set_mode((self.settings.screen_width,  self.settings.screen_height))
self.stars = pygame.sprite.Group()
def run_game(self):
while True:
self._create_fleet()
self._update_screen()
self._check_events()
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

def _create_fleet(self):
star = Star(self)
self.stars.add(star)
def _update_screen(self):
self.screen.fill(self.settings.bg_color)
self.stars.draw(self.screen)
pgame.display.flip()

if __name__ == '__main__':
sg = StarsGame()
sg.run_game()
class Star:
def __init__(self, sg_game):
super().__init__()
self.screen = sg_game.screen
self.image = pygame.image.load('images/star.bmp')
self.rect = self.image.get_rect()
self.rect.x = self.rect.width
self.rect.y = self.rect.height
self.x = float(self.rect.x)
self.y = float(self.rect.y)

self.stars中添加星号时发现错误:

AttributeError: 'Star' object has no attribute 'add_internal' 

要将star添加到pygame.sprite.Group,Star必须从pygame.sprite.Sprite派生(参见继承):

class Star(pygame.sprite.Sprite):
def __init__(self, sg_game):
super().__init__()
# [...]

相关内容

  • 没有找到相关文章

最新更新