Esempio n. 1
0
class NoticeBar(Sprite):
    """靶机启动提示条"""
    def __init__(self, settings, screen, target):
        super().__init__()
        self.settings = settings
        self.screen = screen
        self.rect = pygame.Rect(target.rect.left, target.rect.bottom,
                                target.rect.width, 3)
        self.y = float(self.rect.y)
        self.color = (0, 0, 0)
        self.speed = 3
        self.timer = MyTimer()

    def update(self):
        """向下移动提示条"""
        if self.speed != 0:
            if self.y >= self.screen.get_rect(
            ).bottom - self.settings.y_target_boundary:  # 到达底端时停止移动
                self.speed = 0
            else:
                self.y += self.speed
                self.rect.y = self.y

    def draw_bar(self):
        """绘制提示条"""
        pygame.draw.rect(self.screen, self.color, self.rect)

    def start_timer(self):
        self.timer.begin()

    def stop_timer(self):
        self.timer.stop()

    def reset_timer(self):
        self.timer.reset()
Esempio n. 2
0
class Target(TargetSample, Sprite):
    """靶机类"""
    def __init__(self, settings, stats, screen, *args):
        """初始化靶机并设置其起始位置"""
        TargetSample.__init__(self)
        Sprite.__init__(self)
        self.settings = settings
        self.screen = screen
        self.stats = stats
        self.screen_rect = screen.get_rect()
        self.args = args
        self.target_shield_image = pygame.image.load(
            dir_path + r'\images\target_shield.png')

        # 靶机初始位置
        self.rect.x = self.settings.x_target_position + args[
            0] * self.rect.width * 2
        self.rect.y = self.settings.y_target_boundary

        # 存储靶机准确位置
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

        self.timer = MyTimer()
        self.shield_timer = MyTimer()
        self.moving = False

        self.delay = self.args[4]  # 靶机延时启动时长
        if self.args[3]:  # 带盾
            self.image = self.target_shield_image
            self.life = 2
        else:  # 无盾
            self.life = 1

    def update_shield(self):
        """更新护盾状态"""
        if self.args[3] and self.shield_timer.pass_time > 5:  # 带盾靶机每5秒会重新生成护盾
            self.shield_timer.reset()
            if self.life < 2:  # 重新生成护盾,靶机变为蓝色
                self.image = self.target_shield_image
                self.life = 2

    def check_edges(self):
        """如果靶机位于活动范围边缘,就返回True"""
        if self.rect.bottom > self.screen_rect.bottom - self.settings.y_target_boundary:
            return True
        elif self.rect.top < self.settings.y_target_boundary:
            return True
        return False

    def blitme(self):
        """在指定位置绘制靶机"""
        self.screen.blit(self.image, self.rect)

    def kill(self):
        """重写kill(),响应靶机被击中"""
        if self.timer.pass_time > 0.5:  # 0.5秒内的碰撞视为击中同一个靶机
            if self.life > 1:  # 击破护盾,靶机变为灰色
                if self.stats.sound_state:
                    shield_broken_sound.play()  # 击破护盾音效
                self.life -= 1
                self.image = self.target_image
                self.timer.reset()
            else:  # 击破靶机
                if self.stats.sound_state:
                    target_broken_sound.play()
                self.timer.stop()
                self.shield_timer.stop()
                super().kill()

    def start_timer(self):
        self.timer.begin()

    def stop_timer(self):
        self.timer.stop()

    def reset_timer(self):
        self.timer.reset()

    def start_shield_timer(self):
        self.shield_timer.begin()

    def stop_shield_timer(self):
        self.shield_timer.stop()

    def reset_shield_timer(self):
        self.shield_timer.reset()