python 3

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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值