class Enemy(GameBase): def __init__(self): # 随机图片 enemy_pic = ["images/enemy1.png", "images/enemy2.png", "images/enemy3.png"] self.img_path = random.choice(enemy_pic) # 随机速度 speed_y = random.randint(2, 3) super().__init__(self.img_path, 0, speed_y) # 敌机的初始化位置 self.rect.bottom = 0 self.rect.x = random.randint(0, SCREEN_SIZE.width - self.rect.width) self.update_count = 0 self.using = self.img_path self.destroy_count = 0 def update(self, screen: Surface): super().update(screen) if self.img_path == "images/enemy3.png": self.update_head() if self.rect.top >= SCREEN_SIZE.bottom: self.kill() def update_head(self): self.update_count += 1 if self.update_count >= 20: if self.using == "images/enemy3.png": self.using = "images/enemy3_n2.png" else: self.using = "images/enemy3.png" self.update_count = 0 self.img = pygame.image.load(self.using) def destroy(self): self.destroy_count += 1 if self.using == "images/enemy3.png" or self.using == "images/enemy3_n2.png": if self.destroy_count // 10 + 1 > 6: self.kill() return self.img = pygame.image.load(f"images/enemy3_down{self.destroy_count // 10 + 1}.png") elif self.using == "images/enemy2.png": if self.destroy_count // 10 + 1 > 4: self.kill() return self.img = pygame.image.load(f"images/enemy2_down{self.destroy_count // 10 + 1}.png") else: if self.destroy_count // 10 + 1 > 4: self.kill() return self.img = pygame.image.load(f"images/enemy1_down{self.destroy_count // 10 + 1}.png")