Exemplo n.º 1
0
    def on_update(self, delta_time):
        """ Movement and game logic """

        if self.lives_count < 0:
            self.music.stop(self.current_music_player)
            arcade.play_sound(self.gameover_sound, 1)
            # Wait the end of the song
            time.sleep(2.5)
            gameover_view = GameOver_file.GameOverView()
            self.window.show_view(gameover_view)
            arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)

        # Move the player with the physics engine
        self.physics_engine.update()

        # --- Manage Enemy movement ---
        # Update goombas position
        self.goomba_list.update()

        # Update koopas position
        self.koopa_list.update()

        # Update the fire balls position
        self.fireball_list.update()

        if self.coin_count >= 10:
            self.lives_count += 1
            self.coin_count -= 10

        # See if the goomba hits a boundary and needs to reverse direction.
        for goomba in self.goomba_list:

            if goomba.boundary_right and goomba.right > goomba.boundary_right and goomba.change_x > 0:
                goomba.change_x *= -1
            if goomba.boundary_left and goomba.left < goomba.boundary_left and goomba.change_x < 0:
                goomba.change_x *= -1

        # See if the koopa hits a boundary and needs to reverse direction.
        for koopa in self.koopa_list:

            if koopa.boundary_right and koopa.right > koopa.boundary_right and koopa.change_x > 0:
                koopa.change_x *= -1
            if koopa.boundary_left and koopa.left < koopa.boundary_left and koopa.change_x < 0:
                koopa.change_x *= -1

        # --- Manage moving platforms ---
        if self.level == 4:
            self.platform_list.update()
            for platform in self.platform_list:
                if platform.boundary_right and platform.right > platform.boundary_right and platform.change_x > 0:
                    platform.change_x *= -1
                if platform.boundary_left and platform.left < platform.boundary_left and platform.change_x < 0:
                    platform.change_x *= -1

        # Update the lives img
        self.lives_sprite.update()

        # --- Manage Collision ---

        # Manage Fire Ball extinction and collision
        for fireball in self.fireball_list:
            # Manage the auto destruction
            # Need >= and <= because center_x reflects the delta_time
            # After 10 block the fire ball will be extinguished
            if fireball.center_x >= (fireball.starting_x + 315):
                fireball.remove_from_sprite_lists()
            if fireball.center_x <= (fireball.starting_x - 315):
                fireball.remove_from_sprite_lists()

            # Manage the collision with a wall
            if arcade.check_for_collision_with_list(fireball, self.wall_list):
                fireball.exploded = True
                if fireball.exploded_animation_finished == True:
                    fireball.remove_from_sprite_lists()

            # Manage the collision with a goomba
            for goomba in self.goomba_list:
                if arcade.check_for_collision(fireball, goomba):
                    goomba.death = True
                    fireball.exploded = True
                    arcade.play_sound(self.enemy_killed, 0.5)
                    if goomba.death_animation_finished == True:
                        goomba.remove_from_sprite_lists()
                    if fireball.exploded_animation_finished == True:
                        fireball.remove_from_sprite_lists()

            # Manage the collision with a koopa
            for koopa in self.koopa_list:
                if arcade.check_for_collision(fireball, koopa):
                    koopa.death = True
                    fireball.exploded = True
                    arcade.play_sound(self.enemy_killed, 0.5)
                    if koopa.death_animation_finished == True:
                        koopa.remove_from_sprite_lists()
                    if fireball.exploded_animation_finished == True:
                        fireball.remove_from_sprite_lists()

        # See if we hit a a coin
        for coin in self.coin_list:
            if arcade.check_for_collision(self.player_sprite, coin):
                arcade.play_sound(self.coin_sound, 0.5)
                coin.remove_from_sprite_lists()
                self.coin_count += 1

        # See if we hit any enemy or lava
        if arcade.check_for_collision_with_list(
                self.player_sprite,
                self.goomba_list) or arcade.check_for_collision_with_list(
                    self.player_sprite,
                    self.koopa_list) or arcade.check_for_collision_with_list(
                        self.player_sprite, self.lava_list):
            self.lives_count -= 1
            self.player_sprite.powerup = False
            if self.checkpoint_sprite.reached == False:
                self.player_sprite.change_x = 0
                self.player_sprite.change_y = 0
                self.player_sprite.center_x = PLAYER_START_X
                self.player_sprite.center_y = PLAYER_START_Y
                # Set the camera to the start
                self.view_left = 0
                self.view_bottom = 0
                arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
            else:
                self.player_sprite.change_x = 0
                self.player_sprite.change_y = 0
                self.player_sprite.center_x = self.checkpoint_sprite.center_x
                self.player_sprite.center_y = self.checkpoint_sprite.center_y
                self.view_bottom = 0

        # See if we reached the checkpoint
        if arcade.check_for_collision(self.player_sprite,
                                      self.checkpoint_sprite):
            self.checkpoint_sprite.reached = True
            if self.first_time == True:
                arcade.play_sound(self.checkpoint_sound, 0.34)
                self.first_time = False
        # Update checkpoint animation only if the checkpoint has been reached
        self.checkpoint_sprite.update_animation()

        # See if we caught the power up
        for powerup in self.powerup_list:
            if arcade.check_for_collision(self.player_sprite, powerup):
                powerup.remove_from_sprite_lists()
                self.player_sprite.powerup = True
                self.player_sprite.powerup_start = int(self.total_time)
                arcade.play_sound(self.powerup_taken, 0.5)

        # See if we reached the door
        if arcade.check_for_collision(self.player_sprite, self.door_sprite):
            self.door_sprite.animate()
            # Advance to the next level
            if self.door_sprite.animation_ended == True and self.level < 4:
                self.door_sprite.animation_ended = False
                self.first_time = True
                self.level += 1
                self.final_time += self.total_time
                arcade.play_sound(self.level_finished, 1)
                # Wait the end of the song
                time.sleep(5.5)
                self.player_sprite.powerup = False
                self.setup(self.level)
                # Set the camera to the start
                self.view_left = 0
                self.view_bottom = 0
                arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)

            # That was the last level so the game ends
            if self.door_sprite.animation_ended == True and self.level == 4:
                self.music.stop(self.current_music_player)
                self.final_time += self.total_time
                arcade.play_sound(self.level_finished, 1)
                # Wait the end of the song
                time.sleep(5.5)
                gamewon_view = game_won_file.GameWonView(
                    False, self.final_time, self.lives_count)
                self.window.show_view(gamewon_view)
                arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)

        # --- Manage Scrolling ---
        # Track if we need to change the viewport
        changed = False

        # Scroll left
        left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
        if self.player_sprite.left < left_boundary:
            self.view_left -= left_boundary - self.player_sprite.left
            changed = True

        # Scroll right
        right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
        if self.player_sprite.right > right_boundary:
            self.view_left += self.player_sprite.right - right_boundary
            changed = True

        # Scroll up
        top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
        if self.player_sprite.top > top_boundary:
            self.view_bottom += self.player_sprite.top - top_boundary
            changed = True

        # Scroll down
        bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
        if self.player_sprite.bottom < bottom_boundary:
            self.view_bottom -= bottom_boundary - self.player_sprite.bottom
            changed = True

        if changed and self.view_left > 0:
            # Only scroll to integers. Otherwise we end up with pixels that
            # don't line up on the screen
            if self.level <= 2:
                if (self.view_left + SCREEN_WIDTH) < 7875:
                    self.view_bottom = int(self.view_bottom)
                    self.view_left = int(self.view_left)

                    # Do the scrolling
                    arcade.set_viewport(self.view_left,
                                        SCREEN_WIDTH + self.view_left,
                                        self.view_bottom,
                                        SCREEN_HEIGHT + self.view_bottom)
            else:
                if (self.view_left + SCREEN_WIDTH) < 18900:
                    self.view_bottom = int(self.view_bottom)
                    self.view_left = int(self.view_left)

                    # Do the scrolling
                    arcade.set_viewport(self.view_left,
                                        SCREEN_WIDTH + self.view_left,
                                        self.view_bottom,
                                        SCREEN_HEIGHT + self.view_bottom)

        # --- Manage Position ---
        # Too left
        if self.player_sprite.center_x <= 10:
            self.player_sprite.change_x = 0

        # Too right
        if self.level <= 2:
            if self.player_sprite.center_x >= 7865:
                self.player_sprite.change_x = 0
        if self.level > 2:
            if self.player_sprite.center_x >= 18890:
                self.player_sprite.change_x = 0

        # --- Manage Animations ---
        self.player_list.update_animation(delta_time)
        self.fireball_list.update_animation(delta_time)
        self.goomba_list.update_animation(delta_time)
        self.koopa_list.update_animation(delta_time)
        self.coin_list.update_animation(delta_time)

        # Update the timer
        self.total_time += delta_time

        # Useful to know if the song is still running
        position = self.music.get_stream_position(self.current_music_player)

        # The position pointer is reset to 0 right after we finish the song.
        # This makes it very difficult to figure out if we just started playing
        # or if we are doing playing.
        if position == 0.0:
            self.advance_song()
            self.play_song()