1 回答
TA贡献1934条经验 获得超2个赞
移动方向必须是该类的属性Invader。如果精灵位于窗口的左侧或右侧,则更改方向:
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
添加回答
举报