Exemplo n.º 1
0
class MarioGame(GameState):
    def __init__(self, game_info, sound_manager):
        GameState.__init__(self, GameState.STATE_GAME, GameState.STATE_LOAD)

        self.game_info = game_info
        self.sound_manager = sound_manager

        self.game_info.set_timer_in_seconds(300)

        self.mario_game_time = -1
        self.world_shift = 0

        self.player = Player(self.sound_manager, game_info)

        self.active_sprite_list = pygame.sprite.Group()
        self.active_sprite_list.add(self.player)

        self.level = Level(game_info, self.player, self.sound_manager)

        if constants.DEBUG:
            self.debug_overlay = DebugOverlay(self.game_info, self.player)

        self.sound_manager.play_music(constants.MUSIC_MAIN_THEME)

    def process_events(self, events):
        for event in events:
            if event.type == pygame.KEYDOWN:
                key = event.key
                if key == pygame.K_LEFT:
                    self.player.go_left()
                if key == pygame.K_RIGHT:
                    self.player.go_right()
                if key == pygame.K_UP:
                    self.player.jump()
                if key == pygame.K_DOWN:
                    self.player.crouch()
                if key == pygame.K_a:
                    self.player.transition(constants.POWER_LARGE)
                if key == pygame.K_s:
                    self.player.transition(constants.POWER_SMALL)
                if key == pygame.K_t:
                    self.player.start_death_sequence()
                if key == pygame.K_ESCAPE or key == pygame.K_q:
                    # TODO should be pause
                    pass

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT and self.player.x_vel < 0:
                    self.player.stop()
                if event.key == pygame.K_RIGHT and self.player.x_vel > 0:
                    self.player.stop()
                if event.key == pygame.K_DOWN:
                    self.player.stop()

    def update(self, game_time):
        if self.mario_game_time == -1:
            self.mario_game_time = game_time
        else:
            self.game_info.timer -= (game_time - self.mario_game_time)
            self.mario_game_time = game_time

        if self.player.rect.bottom > constants.SCREEN_HEIGHT and self.player.transition_state is not constants.TRANSITION_DEATH_SEQUENCE:
            self.player.start_death_sequence()

        if self.player.state == constants.STATE_DEAD:
            self.switch = True
            self.game_info.num_lives -= 1
            if self.game_info.num_lives < 0:
                self.set_next_state(GameState.STATE_GAME_OVER)

        self.level.update(game_time)

    def draw(self, screen):
        self.level.draw(screen)
        self.active_sprite_list.draw(screen)

        if constants.DEBUG:
            self.debug_overlay.draw(screen)
Exemplo n.º 2
0
class GamePlayingState:
    def __init__(self, game):
        self._game = game
        self._paused = False
        self._game_over = False

    def enter(self):
        self.create_level()

    def create_level(self):
        """ Creates a new player and restarts level sprites. """
        self._paused = False
        self._game.ui.clear()
        self._player = PlayerCtrl()
        self._level = Level('level_1.tmx', self._player, self)
        self._timer = Timer()

    def exit(self):
        # Save score or something
        self._game.ui.clear()

    def process_events(self, event):
        if not self._game_over:
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_p:
                    self.pause()

    def pause(self):
        if self._paused:
            self._game.ui.pop_menu()
            Timer.unpause_timers()
        else:
            actions = [{
                'action': self.pause,
                'text': 'Resume'
            }, {
                'action': self.create_level,
                'text': 'Restart'
            }, {
                'action':
                lambda: self._game.set_state(GameNotPlayingState),
                'text':
                'Main Menu'
            }]
            self._game.ui.make_menu("Game Paused", actions, 24, cfg.WHITE)
            Timer.pause_timers()
        self._paused = not self._paused

    def game_over(self):
        actions = [{
            'action': lambda: self._game.set_state(GamePlayingState),
            'text': 'Play Again'
        }, {
            'action': lambda: self._game.set_state(GameNotPlayingState),
            'text': 'Main Menu'
        }, {
            'action': self._game.quit,
            'text': 'Quit'
        }]
        if self._player.alive():
            title = "You win!"
        else:
            title = "Game Over!"
        self._game.ui.make_menu(title, actions, 24, cfg.WHITE)
        self._timer.pause()
        self._game_over = True

    def handle_input(self, active_bindings, mouse_state, mouse_x, mouse_y):
        if not (self._paused or self._game_over):
            self._player.handle_keys(active_bindings)
            self._player.handle_mouse(mouse_state, mouse_x, mouse_y)

    def update(self, dt):
        if not self._paused:
            self._level.update(dt)

    def draw(self):
        self._level.draw(self._game.screen)
        self._game.ui.draw(self._game.screen)
        total_secs = self._timer.elapsed_time() // 1000
        mins = total_secs // 60
        secs = total_secs % 60
        gtext.render(self._game.screen,
                     f"{mins:02d}:{secs:02d}",
                     24,
                     cfg.WHITE,
                     location='n')