Ejemplo n.º 1
0
    def on_update(self, delta_time: float):
        """
        Update the positions and statuses of all game objects
        If paused, do nothing
        :param delta_time: Time since the last update
        """
        if not self.game_over or not self.paused:
            self.physics_engine.step()

        # handle background music
        self.play_music()

        if self.screen_wipe_rect:  # when the game is transitioning to a new level/restarting a level
            self.screen_wipe_rect.center_x += self.screen_wipe_rect.change_x
            self.screen_wipe_rect.center_y = self.view_bottom + (SCREEN_HEIGHT / 2)
            # handle loading level here, after screen is covered in blue wipe
            if (self.screen_wipe_rect.center_x > SCREEN_WIDTH) and (self.update_level):
                self.load_level(self.level)
                self.update_level = False  # lower flag when level begins to load
            if self.screen_wipe_rect.center_x > SCREEN_WIDTH * 2:
                self.screen_wipe_rect = None

        # Update everything
        self.player_list.update()
        self.all_sprites.update()
        self.enemies_list.update()
        self.moving_platforms_list.update()
        self.cannons_list.update()

        Controls.handle_control_actions(self)
        if self.player.in_water:
            Controls.handle_water_physics(self)

        self.process_damage()
        self.track_moving_sprites(delta_time)
        self.cannon_toggle()
        self.in_water_physics()

        if ar.check_for_collision_with_list(self.player, self.keys_list) and not self.update_level:
            current_key = ar.check_for_collision_with_list(self.player, self.keys_list)[0]
            if self.key_colors[current_key] == WHITE:
                # this if statement is redundant so that we don't immediately enter the else statement below
                if self.hidden_platform_list:
                    self.orb_off_sound.play(volume=0.2)
                    for platform in self.hidden_platform_list:
                        self.physics_engine.remove_sprite(platform)
                    self.hidden_platform_list = None
                self.player.color = WHITE
            else:
                if not self.hidden_platform_list:
                    self.orb_touched_sound.play(volume=0.2)
                    self.load_layer("Hidden Platforms", self.key_colors[current_key])
                    self.player.color = self.key_colors[current_key]

        # if player gets to the right edge of the level, go to next level
        if self.player.right >= self.end_of_map:
            self.update_level = True  # raise this flag to properly restart level
            self.level += 1  # switch to next level

            self.screen_wipe_rect = Transition()
            self.screen_wipe_rect.setup()
            self.player_teleported = True

            self.physics_engine.set_horizontal_velocity(self.player, 0)
            self.physics_engine.set_position(self.player, self.player.spawnpoint)

        # if the player hits the bottom of the level, player dies and respawns at the start of the level
        if self.player.bottom <= 0:
            self.update_level = True  # raise this flag to properly restart level

            self.screen_wipe_rect = Transition()
            self.screen_wipe_rect.setup()

            self.player_teleported = True

            self.physics_engine.set_horizontal_velocity(self.player, 0)
            self.physics_engine.set_position(self.player, self.player.spawnpoint)

        if self.physics_engine.is_on_ground(self.player) and self.player.jumping:
            self.player.jumping = False

        # track if we need to change the view port
        changed = False

        # Scroll left
        left_bndry = self.view_left + VIEWPORT_LEFT_MARGIN
        if self.player.left < left_bndry and self.player.left > VIEWPORT_LEFT_MARGIN:
            self.view_left -= left_bndry - self.player.left
            changed = True

        # Scroll right
        right_bndry = self.view_left + SCREEN_WIDTH - VIEWPORT_RIGHT_MARGIN
        if self.player.right > right_bndry and (self.player.right < (self.end_of_map - VIEWPORT_RIGHT_MARGIN)):
            self.view_left += self.player.right - right_bndry
            changed = True

        # Scroll up
        top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN_TOP
        if self.player.top > top_bndry and (self.player.top < (self.top_of_map - VIEWPORT_MARGIN_TOP)):
            self.view_bottom += self.player.top - top_bndry
            changed = True

        # Scroll down
        bottom_bndry = self.view_bottom + VIEWPORT_MARGIN_BOTTOM
        if self.player.bottom < bottom_bndry and self.player.bottom > VIEWPORT_MARGIN_BOTTOM:
            self.view_bottom -= bottom_bndry - self.player.bottom
            changed = True

        # If we need to scroll, go ahead and do it.
        # also change viewport when player has teleported (when switching levels, respawning, etc)
        if changed:
            self.view_left = int(self.view_left)
            self.view_bottom = int(self.view_bottom)
            ar.set_viewport(self.view_left,
                            SCREEN_WIDTH + self.view_left,
                            self.view_bottom,
                            SCREEN_HEIGHT + self.view_bottom)

        if self.player_teleported and self.screen_wipe_rect.center_x > SCREEN_WIDTH:
            self.view_left = 0
            self.view_bottom = 0
            ar.set_viewport(self.view_left,
                            SCREEN_WIDTH + self.view_left,
                            self.view_bottom,
                            SCREEN_HEIGHT + self.view_bottom)
            self.player_teleported = False