Example #1
0
def dash(
    sprite: arcade.Sprite,
    dir_x: int,
    dir_y: int,
    collide_with: arcade.SpriteList,
) -> None:
    """Move sprite until it reaches something."""
    sprite.bottom += 1
    prev_x = sprite.left
    prev_y = sprite.bottom
    default_x = sprite.left
    default_y = sprite.bottom

    for x, y in zip_longest(
            range(floor(sprite.left), floor(sprite.left + dir_x),
                  weird_sign(dir_x)),
            range(floor(sprite.bottom), floor(sprite.bottom + dir_y),
                  weird_sign(dir_y)),
    ):
        sprite.left = x if x else prev_x
        sprite.bottom = y if y else prev_y
        if sprite.collides_with_list(collide_with):
            sprite.left = prev_x
            sprite.bottom = prev_y
            break
        if x:
            prev_x = x
        if y:
            prev_y = y
Example #2
0
def _move_sprite(moving_sprite: arcade.Sprite, platforms: arcade.SpriteList,
                 dt):

    if moving_sprite.bottom <= 0 and moving_sprite.change_y < 0:
        moving_sprite.change_y = 0
        moving_sprite.bottom = 0

    moving_sprite.center_y += moving_sprite.change_y * moving_sprite.speed_multiplier * (
        dt / (1 / 60))

    hit_list_y = arcade.check_for_collision_with_list(moving_sprite, platforms)

    for platform in hit_list_y:
        if platform.color != moving_sprite.current_background:
            if moving_sprite.change_y > 0:
                if not platform.change_y:
                    moving_sprite.top = platform.bottom
                    moving_sprite.change_y = 0

            elif moving_sprite.change_y < 0:
                moving_sprite.bottom = platform.top
                if platform.change_y:
                    if platform.change_y < 0:
                        moving_sprite.center_y -= 2
                        moving_sprite.change_y = -3
                    else:
                        moving_sprite.change_y = 0
                else:
                    moving_sprite.change_y = 0
                if platform.change_x:
                    moving_sprite.momentum = platform.change_x

    moving_sprite.center_x += (moving_sprite.change_x + moving_sprite.momentum
                               ) * moving_sprite.speed_multiplier * (dt /
                                                                     (1 / 60))

    hit_list_x = arcade.check_for_collision_with_list(moving_sprite, platforms)

    for platform in hit_list_x:
        if platform.color != moving_sprite.current_background and platform not in hit_list_y:
            if moving_sprite.change_x > 0 and not platform.change_x:
                moving_sprite.right = platform.left
            elif moving_sprite.change_x < 0 and not platform.change_x:
                moving_sprite.left = platform.right

            if platform.change_y:
                if platform.change_y >= 0:
                    moving_sprite.change_x = 0
            else:
                moving_sprite.change_x = 0

    if len(hit_list_y) <= 0 and moving_sprite.momentum != 0:
        moving_sprite.change_x += moving_sprite.momentum
        moving_sprite.momentum = 0
def _move_sprite(moving_sprite: Sprite, walls: SpriteList, ramp_up: bool):
    # Rotate
    moving_sprite.angle += moving_sprite.change_angle

    hit_list = check_for_collision_with_list(moving_sprite, walls)

    if len(hit_list) > 0:
        # Resolve any collisions by this weird kludge
        _circular_check(moving_sprite, walls)

    # --- Move in the y direction
    moving_sprite.center_y += moving_sprite.change_y

    # Check for wall hit
    hit_list_x = check_for_collision_with_list(moving_sprite, walls)
    # print(f"Post-y move {hit_list_x}")
    complete_hit_list = hit_list_x

    # If we hit a wall, move so the edges are at the same point
    if len(hit_list_x) > 0:
        if moving_sprite.change_y > 0:
            while len(check_for_collision_with_list(moving_sprite, walls)) > 0:
                moving_sprite.center_y -= 1
            # print(f"Spot X ({self.player_sprite.center_x}, {self.player_sprite.center_y})"
            #       f" {self.player_sprite.change_y}")
        elif moving_sprite.change_y < 0:
            # Reset number of jumps
            for item in hit_list_x:
                while check_for_collision(moving_sprite, item):
                    # self.player_sprite.bottom = item.top <- Doesn't work for ramps
                    moving_sprite.center_y += 0.25

                if item.change_x != 0:
                    moving_sprite.center_x += item.change_x

            # print(f"Spot Y ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
        else:
            pass
            # TODO: The code below can't execute, as "item" doesn't
            # exist. In theory, this condition should never be arrived at.
            # Collision while player wasn't moving, most likely
            # moving platform.
            # if self.player_sprite.center_y >= item.center_y:
            #     self.player_sprite.bottom = item.top
            # else:
            #     self.player_sprite.top = item.bottom
        moving_sprite.change_y = min(0.0, hit_list_x[0].change_y)

    # print(f"Spot D ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
    moving_sprite.center_y = round(moving_sprite.center_y, 2)
    # print(f"Spot Q ({self.player_sprite.center_x}, {self.player_sprite.center_y})")

    # --- Move in the x direction
    moving_sprite.center_x += moving_sprite.change_x

    check_again = True
    while check_again:
        check_again = False
        # Check for wall hit
        hit_list_y = check_for_collision_with_list(moving_sprite, walls)
        complete_hit_list = hit_list_x
        for sprite in hit_list_y:
            if sprite not in complete_hit_list:
                complete_hit_list.append(sprite)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list_y) > 0:
            change_x = moving_sprite.change_x
            if change_x > 0:
                if ramp_up:

                    for _ in hit_list_y:
                        # print(f"Spot 1 ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
                        # See if we can "run up" a ramp
                        moving_sprite.center_y += change_x
                        if len(
                                check_for_collision_with_list(
                                    moving_sprite, walls)) > 0:
                            # No, ramp run-up doesn't work.
                            moving_sprite.center_y -= change_x
                            moving_sprite.center_x -= 1
                            # print(f"Spot R ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
                            check_again = True
                            break
                        # else:
                        # print("Run up ok 1")
                        # print(f"Spot 2 ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
                else:
                    while len(
                            check_for_collision_with_list(
                                moving_sprite, walls)) > 0:
                        moving_sprite.center_x -= 1

            elif change_x < 0:
                if ramp_up:
                    for item in hit_list_y:
                        # See if we can "run up" a ramp
                        moving_sprite.center_y -= change_x
                        if len(
                                check_for_collision_with_list(
                                    moving_sprite, walls)) > 0:
                            # Can't run up the ramp, reverse
                            moving_sprite.center_y += change_x
                            moving_sprite.left = max(item.right,
                                                     moving_sprite.left)
                            # print(f"Reverse 1 {item.right}, {self.player_sprite.left}")
                            # Ok, if we were shoved back to the right, we need to check this whole thing again.
                            check_again = True
                            break
                        # print(f"Spot 4 ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
                else:
                    while len(
                            check_for_collision_with_list(
                                moving_sprite, walls)) > 0:
                        moving_sprite.center_x += 1

            else:
                print(
                    "Error, x collision while player wasn't moving.\n"
                    "Make sure you aren't calling multiple updates, like "
                    "a physics engine update and an all sprites list update.")

        # print(f"Spot E ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
    return complete_hit_list