Example #1
0
class LevelState(GameState):
    def load(self):
        self.levels = Level.levels
        self.level = self.levels[self.asm.main.completed_levels]
        self.is_over = False
        self.saved = False
        self.over_timer = 0
        self.over_timeout = 1
        self.text = self.level[1]

        super().load()

        self.button = Button(self.res, (WIDTH - 120, HEIGHT - 70), 'Меню',
                             self.sprite_group)
        self.minimap = Minimap(300, 240, self.sprite_group)
        self.conversation = Conversation(self.res, self.sprite_group)
        self.police_effect = PoliceEffect(self.sprite_group)
        self.energy_line = EnergyLine(self.res, self.sprite_group)
        self.dollars = Dollars(self.res, self.sprite_group, level=True)

        self.sm = SoundManager(self.conversation, self.car, self.sprite_group)
        self.load_map(self.level[0])
        self.sm.player_car = self.car
        self.sm.set_background(self.res.music_bg_game, 0.1)

    def update(self, dt, events):
        self.dollars.value = self.car.dollars
        self.dollars.set_darkness_value(self.darkness.value)

        if self.is_over:
            self.over_timer += dt
        if self.over_timer >= self.over_timeout and not self.saved:
            self.saved = True
            if self.car.looted:
                self.asm.main.dollars += self.car.dollars
                self.asm.main.completed_levels = min(
                    self.asm.main.completed_levels + 1,
                    len(self.levels) - 1)
                self.asm.main.write_save()
                self.conversation.show(
                    ['Esc - выход, Space - следующий уровень'],
                    event_listener=self.end_event_listener)
            else:
                self.conversation.show(['Esc - выход, Space - начать заново'],
                                       event_listener=self.end_event_listener)

        if super().update(dt, events) is False:
            return
        self.energy_line.set_energy(self.car.energy)

        if not self.is_over and self.car.looted and not self.conversation.is_showing:
            self.conversation.show(['Вы победили'],
                                   lambda: self.__setattr__('is_over', True))

        if not self.is_over and not self.car.energy and not self.conversation.is_showing:
            self.sm.set_background(self.res.sound_lose, 1)
            self.conversation.show(['Вы потеряли энергию, игра окончена'],
                                   lambda: self.__setattr__('is_over', True))
            self.car.break_down()
Example #2
0
class DriftState(GameState):
    def load(self):
        super().load()

        self.button = Button(self.res, (WIDTH - 120, HEIGHT - 70), 'Меню',
                             self.sprite_group)
        self.minimap = Minimap(300, 240, self.sprite_group)
        self.conversation = Conversation(self.res, self.sprite_group)
        self.police_effect = PoliceEffect(self.sprite_group)

        self.sm = SoundManager(self.conversation, self.car, self.sprite_group)
        self.load_map(self.res.drift_map_path)
        self.sm.player_car = self.car
        self.sm.set_background(self.res.music_bg_menu, 0.1)

        self.car.queue_lines.extend(['Свободный режим'])

    def update(self, dt, events):
        super().update(dt, events)
        self.car.energy = 1
Example #3
0
class MenuState(State):
    def load(self):
        self.sprite_group = Group()

        self.label_font = self.res.font64
        self.button_play = Button(
            self.res, ((WIDTH - self.res.image_button.get_width()) / 2, 240),
            'Играть', self.sprite_group)
        self.button_drift = Button(
            self.res, ((WIDTH - self.res.image_button.get_width()) / 2, 300),
            'Дрифт', self.sprite_group)
        self.button_shop = Button(
            self.res, ((WIDTH - self.res.image_button.get_width()) / 2, 360),
            'Магазин', self.sprite_group)
        self.button_exit = Button(
            self.res, ((WIDTH - self.res.image_button.get_width()) / 2, 420),
            'Выход', self.sprite_group)

        self.dollars = Dollars(self.res, self.sprite_group)
        self.sm = SoundManager(None, None)
        self.sm.set_background(self.res.music_bg_menu, 0.3)

        # Задний фон для меню
        self.world = b2World()
        self.world.gravity = 0, 0
        self.contact_listener = ContactListener()
        self.world.contactListener = self.contact_listener
        self.obj_args = self.world, self.contact_listener, self.res
        self.animation_background = pygame.Surface((500, 300)).convert_alpha()
        self.animation_background.fill((0, 0, 0, 0))
        self.background_group = Group()
        self.spawn_random_car()
        self.spawn_timer = 0
        self.spawn_timeout = 1.5
        self.spawn_counter = 0
        self.background_camera = Camera(self.background_group)

    def reset(self):
        self.sm.set_background(self.res.music_bg_menu, 0.3)

    def spawn_random_car(self):
        r = random.randint(0, 1)
        car = PlayerCar(
            *self.obj_args, self.sm,
            CarSkin(self.res, self.asm.main.skins[self.asm.main.current_skin]),
            (random.randint(100, WIDTH - 100), r * HEIGHT + (r * 2 - 1) * 100),
            random.randint(-20, 20) + 180 * (r - 1), None,
            self.background_group)
        car.control_state |= C_UP
        car.dispose_timeout = 1
        for tire in car.tires:
            tire.set_control_state(car.control_state)

    def update(self, dt, events):
        self.dollars.value = self.asm.main.dollars
        self.sprite_group.update(dt, events)

        self.spawn_timer += dt
        if self.spawn_timer >= self.spawn_timeout:
            self.spawn_timer = 0
            self.spawn_random_car()
        self.world.Step(dt, 6, 2)
        self.background_group.update(dt, events)

        if self.spawn_counter:
            self.spawn_counter -= 1
            self.spawn_random_car()

        for i in self.background_group:
            if type(i) == PlayerCar:
                i.energy -= dt * 0.3
                if i.energy <= 0:
                    i.break_down()
        for i in events:
            if i.type == pygame.MOUSEBUTTONDOWN:
                if i.button == pygame.BUTTON_RIGHT:
                    Explosion.apply_impulses(self.world,
                                             pygame.mouse.get_pos(), 1500, 500,
                                             15, self)
                    particles = paint_images(self.res.explosion_particles,
                                             lambda x: (0, 0, 0, x[3]))
                    Explosion.spawn_particles(*self.obj_args,
                                              pygame.mouse.get_pos(),
                                              particles, 10, 10,
                                              self.background_group)
            if i.type == pygame.KEYDOWN:
                if i.key == pygame.K_SPACE:
                    self.spawn_counter += 5

        if self.button_play.is_clicked():
            self.asm.push(LevelState(self.asm, self.res))
        if self.button_drift.is_clicked():
            self.asm.push(DriftState(self.asm, self.res))
        if self.button_shop.is_clicked():
            self.asm.push(ShopState(self.asm, self.res))
        if self.button_exit.is_clicked():
            self.asm.pop()

    def render(self, sc: pygame.Surface):
        sc.fill((130, 130, 130))

        self.background_camera.draw(sc)
        render = self.label_font.render('Ограбление банка', True, (0, 0, 0))
        sc.blit(render, ((WIDTH - render.get_width()) / 2, 70))
        self.sprite_group.draw(sc)
        render = self.res.font36.render(
            f'Уровень: {self.asm.main.completed_levels + 1}', True, (0, 0, 0))
        sc.blit(render, (20, 80))

        render = self.res.font20.render(f'Не трогай Space!', True, (0, 0, 0))
        sc.blit(render, (WIDTH - render.get_width() - 10,
                         HEIGHT - render.get_height() - 10))
        render = self.res.font20.render(f'Не нажимай ПКМ!', True, (0, 0, 0))
        sc.blit(render, (10, HEIGHT - render.get_height() - 10))