1 回答

TA貢獻1934條經(jīng)驗 獲得超2個贊
移動方向必須是該類的屬性Invader。如果精靈位于窗口的左側(cè)或右側(cè),則更改方向:
class Invader(pygame.sprite.Sprite):
def __init__(self, settings, picture, x, y):
super().__init__()
self.settings = settings
self.x = x
self.y = y
self.image = pygame.image.load(os.path.join(self.settings.imagepath, picture)).convert_alpha()
self.image = pygame.transform.scale(self.image, (63,38))
self.rect = self.image.get_rect()
self.rect.center = [self.x, self.y]
self.direction = 1 # <---
def update(self):
if self.rect.right >= 800:
self.direction = -1
if self.rect.left <= 0:
self.direction = 1
self.rect.x += self.direction
添加回答
舉報