【发布时间】:2021-12-29 08:03:29
【问题描述】:
您好,我正在开发 AlienInvasion 游戏,我对 pygame.sprite.Group 有点困惑。它如何知道要调用哪个pygame.sprite.Group()?
我习惯于引用这样的类。self.ship = Ship(self)
但是这个是这样称呼的。它如何知道它正在调用哪个精灵组?我目前只有 1 个精灵组 ATM,所以这是有道理的,但如果我有更多呢?self.bullets = pygame.sprite.Group()
然后我可以像这样引用它的方法self.bullets.update()
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
def _update_bullets(self):
"""update position of bullets and get rid of old bullets"""
# update bullet positions
self.bullets.update()
class Bullet(Sprite):
"""A class to manage bullets fired from the ship"""
def __init__(self, ai_game):
"""Create a bullet object at the ship's current position."""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color=self.settings.bullet_color
【问题讨论】: