コード例 #1
0
ファイル: physics_engines.py プロジェクト: yasoob/arcade
    def can_jump(self, y_distance=5) -> bool:
        """
        Method that looks to see if there is a floor under
        the player_sprite. If there is a floor, the player can jump
        and we return a True.

        :returns: True if there is a platform below us
        :rtype: bool
        """

        # Check for touching a ladder
        if self.is_on_ladder():
            return False

        # Move down to see if we are on a platform
        self.player_sprite.center_y -= y_distance

        # Check for wall hit
        hit_list = check_for_collision_with_list(self.player_sprite, self.platforms)

        self.player_sprite.center_y += y_distance

        if len(hit_list) > 0:
            self.jumps_since_ground = 0

        if len(hit_list) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps:
            return True
        else:
            return False
コード例 #2
0
ファイル: physics_engines.py プロジェクト: yasoob/arcade
 def is_on_ladder(self):
     # Check for touching a ladder
     if self.ladders:
         hit_list = check_for_collision_with_list(self.player_sprite, self.ladders)
         if len(hit_list) > 0:
             return True
     return False
コード例 #3
0
ファイル: physics_engines.py プロジェクト: Chris-m41/CS241
    def can_jump(self) -> bool:
        """
        Method that looks to see if there is a floor under
        the player_sprite. If there is a floor, the player can jump
        and we return a True.

        :returns: True if there is a platform below us
        :rtype: bool
        """
        # --- Move in the y direction
        self.player_sprite.center_y -= 2

        # Check for wall hit
        hit_list = check_for_collision_with_list(self.player_sprite,
                                                 self.platforms)

        self.player_sprite.center_y += 2

        if len(hit_list) > 0:
            self.jumps_since_ground = 0

        if len(
                hit_list
        ) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps:
            return True
        else:
            return False
コード例 #4
0
    def update(self):
        """
        Move everything and resolve collisions.
        """

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.walls)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_x > 0:
                for item in hit_list:
                    self.player_sprite.right = min(item.left,
                                                   self.player_sprite.right)
            elif self.player_sprite.change_x < 0:
                for item in hit_list:
                    self.player_sprite.left = max(item.right,
                                                  self.player_sprite.left)
            else:
                print("Error, collision while player wasn't moving.")

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.walls)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_y > 0:
                for item in hit_list:
                    self.player_sprite.top = min(item.bottom,
                                                 self.player_sprite.top)
            elif self.player_sprite.change_y < 0:
                for item in hit_list:
                    self.player_sprite.bottom = max(item.top,
                                                    self.player_sprite.bottom)
            else:
                print("Error, collision while player wasn't moving.")
コード例 #5
0
ファイル: physics_engines.py プロジェクト: pauleveritt/arcade
    def update(self):
        """
        Move everything and resolve collisions.
        """
        # --- Move in the x direction
        self.player_sprite.center_x += self.player_sprite.change_x

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.walls)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_x > 0:
                for item in hit_list:
                    self.player_sprite.right = min(item.left,
                                                   self.player_sprite.right)
            elif self.player_sprite.change_x < 0:
                for item in hit_list:
                    self.player_sprite.left = max(item.right,
                                                  self.player_sprite.left)
            else:
                print("Error, collision while player wasn't moving.")

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.walls)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_y > 0:
                for item in hit_list:
                    self.player_sprite.top = min(item.bottom,
                                                 self.player_sprite.top)
            elif self.player_sprite.change_y < 0:
                for item in hit_list:
                    self.player_sprite.bottom = max(item.top,
                                                    self.player_sprite.bottom)
            else:
                print("Error, collision while player wasn't moving.")
コード例 #6
0
    def collides_with_list(self, sprite_list: list) -> list:
        """Check if current sprite is overlapping with any other sprite in a list

        Args:
            self: current Sprite
            sprite_list: SpriteList to check against

        Returns:
            SpriteList of all overlapping Sprites from the original SpriteList
        """
        from arcade.geometry import check_for_collision_with_list
        return check_for_collision_with_list(self, sprite_list)
コード例 #7
0
ファイル: PhysicsEngine.py プロジェクト: ndarby/laser-tag
    def update(self):
        """
        Move everything and resolve collisions.
        """
        # --- Move in the x direction
        for char in self.characters:
            char.center_x += char.change_x

            # Check for wall hit
            hit_list = check_for_collision_with_list(char, self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if char.change_x > 0:
                    for item in hit_list:
                        char.right = min(item.left, char.right)
                elif char.change_x < 0:
                    for item in hit_list:
                        char.left = max(item.right, char.left)
                else:
                    raise RuntimeError(
                        f"Error, collision while {char} wasn't moving.")

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

            # Check for wall hit
            hit_list = check_for_collision_with_list(char, self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if char.change_y > 0:
                    for item in hit_list:
                        char.top = min(item.bottom, char.top)
                elif char.change_y < 0:
                    for item in hit_list:
                        char.bottom = max(item.top, char.bottom)
                else:
                    raise RuntimeError(
                        f"Error, collision while {char} wasn't moving.")
コード例 #8
0
    def update(self):
        """
        Move os objetos, verifica e resolve as colisões.
        """
        player_aux = copy.deepcopy(self.player_sprite)
        """
        Movimentação do eixo X
        """
        player_aux.center_x += player_aux.change_x

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(player_aux,
                                          self.walls)
        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            self.player_sprite.change_x = 0
        else:
            self.player_sprite.center_x += self.player_sprite.change_x
        """
        Movimentação do eixo Y
        """
        player_aux.center_y += player_aux.change_y

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(player_aux,
                                          self.walls)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            self.player_sprite.change_y = 0
        else:
            self.player_sprite.center_y += self.player_sprite.change_y

        #Limpeza de variaveis
        self.player_sprite.change_x = 0
        self.player_sprite.change_y = 0
コード例 #9
0
ファイル: physics_engines.py プロジェクト: mikemhenry/arcade
    def can_jump(self) -> bool:
        """
        Method that looks to see if there is a floor under
        the player_sprite. If there is a floor, the player can jump
        and we return a True.
        """
        # --- Move in the y direction
        self.player_sprite.center_y -= 2

        # Check for wall hit
        hit_list = check_for_collision_with_list(self.player_sprite, self.platforms)

        result = False

        if len(hit_list) > 0:
            result = True

        self.player_sprite.center_y += 2
        return result
コード例 #10
0
    def can_jump(self) -> bool:
        """
        Method that looks to see if there is a floor under
        the player_sprite. If there is a floor, the player can jump
        and we return a True.
        """
        # --- Move in the y direction
        self.player_sprite.center_y -= 2

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.platforms)

        self.player_sprite.center_y += 2

        if len(hit_list) > 0:
            return True
        else:
            return False
コード例 #11
0
    def update(self):
        """
        Move everything and resolve collisions.
        """
        # print(f"Spot A ({self.player_sprite.center_x}, {self.player_sprite.center_y})")

        # --- Add gravity
        self.player_sprite.change_y -= self.gravity_constant

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

        # Check for wall hit
        hit_list = check_for_collision_with_list(self.player_sprite,
                                                 self.platforms)

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

                    if item.change_x != 0:
                        self.player_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
            self.player_sprite.change_y = min(0.0, hit_list[0].change_y)

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

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

        check_again = True
        while check_again:
            check_again = False
            # Check for wall hit
            hit_list = check_for_collision_with_list(self.player_sprite,
                                                     self.platforms)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                change_x = self.player_sprite.change_x
                if change_x > 0:
                    for item in hit_list:
                        # print(f"Spot 1 ({self.player_sprite.center_x}, {self.player_sprite.center_y})")
                        # See if we can "run up" a ramp
                        self.player_sprite.center_y += change_x
                        if len(
                                check_for_collision_with_list(
                                    self.player_sprite, self.platforms)) > 0:
                            self.player_sprite.center_y -= change_x
                            self.player_sprite.right = min(
                                item.left, self.player_sprite.right)
                            # 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})")

                elif change_x < 0:
                    for item in hit_list:
                        # See if we can "run up" a ramp
                        self.player_sprite.center_y -= change_x
                        if len(
                                check_for_collision_with_list(
                                    self.player_sprite, self.platforms)) > 0:
                            # Can't run up the ramp, reverse
                            self.player_sprite.center_y += change_x
                            self.player_sprite.left = max(
                                item.right, self.player_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:
                    print(
                        "Error, collision while player wasn't moving.\nMake 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})")

        for platform in self.platforms:
            if platform.change_x != 0 or platform.change_y != 0:
                platform.center_x += platform.change_x

                if platform.boundary_left is not None \
                        and platform.left <= platform.boundary_left:
                    platform.left = platform.boundary_left
                    if platform.change_x < 0:
                        platform.change_x *= -1

                if platform.boundary_right is not None \
                        and platform.right >= platform.boundary_right:
                    platform.right = platform.boundary_right
                    if platform.change_x > 0:
                        platform.change_x *= -1

                if check_for_collision(self.player_sprite, platform):
                    if platform.change_x < 0:
                        self.player_sprite.right = platform.left
                    if platform.change_x > 0:
                        self.player_sprite.left = platform.right

                platform.center_y += platform.change_y

                if platform.boundary_top is not None \
                        and platform.top >= platform.boundary_top:
                    platform.top = platform.boundary_top
                    if platform.change_y > 0:
                        platform.change_y *= -1

                if platform.boundary_bottom is not None \
                        and platform.bottom <= platform.boundary_bottom:
                    platform.bottom = platform.boundary_bottom
                    if platform.change_y < 0:
                        platform.change_y *= -1
コード例 #12
0
    def update(self, direction=None, player_to_follow=None) -> None:
        """
        Move everything and resolve collisions
        :return: none
        """
        # player and other enemy detection
        if isinstance(player_to_follow, arcade.SpriteList):
            # checks for player and other entity collisions
            hit_list = \
                check_for_collision_with_list(self.player,
                                              player_to_follow)
            if len(hit_list) > 0:
                for item in hit_list:
                    if self.player.is_attack_state:
                        if not isinstance(item, Fireball):
                            item.health -= 1.5
                    if isinstance(item, Executioner):
                        item.is_player_hit = True
                    if isinstance(item, Slime):
                        item.is_player_hit = True
                    if isinstance(item, Fireball):
                        item.is_wall_hit = True
                        item.is_player_hit = True
                    if isinstance(item, Boss):
                        if self.player.is_attack_state:
                            item.health -= 0.5
        # if the player is the user
        elif isinstance(self.player, Player):
            if self.player.health < 1:
                self.player.game_over()
            # --- Move sprite
            self.player.move_player(direction)
            # update x position
            self.player.center_x += self.player.change_x
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_x > 0:
                    for item in hit_list:
                        self.player.right = min(item.left,
                                                self.player.right) - 5
                        self.player.direction = None
                        return arcade.key.RIGHT
                elif self.player.change_x < 0:
                    for item in hit_list:
                        self.player.left = max(item.right,
                                               self.player.left) + 5
                        self.player.direction = None
                        return arcade.key.LEFT
            # update y position
            self.player.center_y += self.player.change_y
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_y > 0:
                    for item in hit_list:
                        self.player.top = min(item.bottom, self.player.top) - 5
                        self.player.direction = None
                        return arcade.key.UP
                elif self.player.change_y < 0:
                    for item in hit_list:
                        self.player.bottom = max(item.top,
                                                 self.player.bottom) + 5
                        self.player.direction = None
                        return arcade.key.DOWN
            # check for trap hit
            hit_list = \
                check_for_collision_with_list(self.player, self.traps)
            if len(hit_list) > 0:
                self.player.health -= 0.1
            self.player.change_x, self.player.change_y = 0, 0

        # if it isn't the player
        elif isinstance(self.player, Slime):
            # --- Move sprite
            self.player.follow(player_to_follow)
            # update x position
            self.player.center_x += self.player.change_x
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_x > 0:
                    for item in hit_list:
                        self.player.right = min(item.left,
                                                self.player.right) - 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                elif self.player.change_x < 0:
                    for item in hit_list:
                        self.player.left = max(item.right,
                                               self.player.left) + 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True

            # update y position
            self.player.center_y += self.player.change_y
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)
            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_y > 0:
                    for item in hit_list:
                        self.player.top = min(item.bottom, self.player.top) - 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                elif self.player.change_y < 0:
                    for item in hit_list:
                        self.player.bottom = max(item.top,
                                                 self.player.bottom) + 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                else:
                    print("Error, collision while enemy wasn't moving.")
            self.player.change_x, self.player.change_y = 0, 0
        elif isinstance(self.player, Minion):
            # --- Move sprite
            self.player.follow(player_to_follow)
            if arcade.check_for_collision(self.player, player_to_follow):
                self.player.is_player_hit = True
            # update x position
            self.player.center_x += self.player.change_x
            self.player.center_y += self.player.change_y

            # Check for wall hit
            if self.player.center_x > 890:
                self.player.center_x = 890
            if self.player.center_x < 20:
                self.player.center_x = 20
            if self.player.center_y > 890:
                self.player.center_y = 890
            if self.player.center_y < 20:
                self.player.center_y = 20
            self.player.change_x, self.player.change_y = 0, 0

        elif isinstance(self.player, Executioner):
            # --- Move sprite
            self.player.follow(player_to_follow)
            # update x position
            self.player.center_x += self.player.change_x
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_x > 0:
                    for item in hit_list:
                        self.player.right = min(item.left,
                                                self.player.right) - 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                elif self.player.change_x < 0:
                    for item in hit_list:
                        self.player.left = max(item.right,
                                               self.player.left) + 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                else:
                    print("Error, collision while enemy wasn't moving.")

            # update y position
            self.player.center_y += self.player.change_y
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)
            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_y > 0:
                    for item in hit_list:
                        self.player.top = min(item.bottom, self.player.top) - 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                elif self.player.change_y < 0:
                    for item in hit_list:
                        self.player.bottom = max(item.top,
                                                 self.player.bottom) + 5
                        self.player.direction = None
                        self.player.movement = not self.player.movement
                        self.player.hit = True
                else:
                    print("Error, collision while enemy wasn't moving.")

            self.player.change_x, self.player.change_y = 0, 0
        elif isinstance(self.player, Fireball):
            # --- Move sprite
            # update x position
            self.player.center_x += self.player.change_x
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)

            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_x > 0:
                    for item in hit_list:
                        self.player.right = min(item.left,
                                                self.player.right) - 5
                elif self.player.change_x < 0:
                    for item in hit_list:
                        self.player.left = max(item.right,
                                               self.player.left) + 5
                else:
                    print("Error, collision while enemy wasn't moving.")
                self.player.is_wall_hit = True
                self.player.reset = True
            # update y position
            self.player.center_y += self.player.change_y
            # Check for wall hit
            hit_list = \
                check_for_collision_with_list(self.player,
                                              self.walls)
            # If we hit a wall, move so the edges are at the same point
            if len(hit_list) > 0:
                if self.player.change_y > 0:
                    for item in hit_list:
                        self.player.top = min(item.bottom, self.player.top) - 5
                elif self.player.change_y < 0:
                    for item in hit_list:
                        self.player.bottom = max(item.top,
                                                 self.player.bottom) + 5
                else:
                    print("Error, collision while enemy wasn't moving.")
                self.player.is_wall_hit = True
                self.player.reset = True
コード例 #13
0
ファイル: physics_engines.py プロジェクト: pauleveritt/arcade
    def update(self):
        """
        Move everything and resolve collisions.
        """

        # --- Add gravity
        self.player_sprite.change_y -= self.gravity_constant

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.platforms)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_y > 0:
                for item in hit_list:
                    self.player_sprite.top = min(item.bottom,
                                                 self.player_sprite.top)
            elif self.player_sprite.change_y < 0:
                for item in hit_list:
                    while check_for_collision(self.player_sprite, item):
                        self.player_sprite.bottom += 0.5
                    if item.change_x != 0:
                        self.player_sprite.center_x += item.change_x
            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
            self.player_sprite.change_y = min(0, hit_list[0].change_y)

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.platforms)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            change_x = self.player_sprite.change_x
            if change_x > 0:
                for item in hit_list:
                    # See if we can "run up" a ramp
                    self.player_sprite.center_y += change_x
                    if check_for_collision(self.player_sprite, item):
                        self.player_sprite.center_y -= change_x
                        self.player_sprite.right = \
                            min(item.left, self.player_sprite.right)

            elif change_x < 0:
                for item in hit_list:
                    # See if we can "run up" a ramp
                    self.player_sprite.center_y -= change_x
                    if check_for_collision(self.player_sprite, item):
                        self.player_sprite.center_y -= change_x
                        self.player_sprite.left = max(item.right,
                                                      self.player_sprite.left)
            else:
                print("Error, collision while player wasn't moving.")

        for platform in self.platforms:
            if platform.change_x != 0 or platform.change_y != 0:
                platform.center_x += platform.change_x

                if platform.boundary_left is not None \
                        and platform.left <= platform.boundary_left:
                    platform.left = platform.boundary_left
                    if platform.change_x < 0:
                        platform.change_x *= -1

                if platform.boundary_right is not None \
                        and platform.right >= platform.boundary_right:
                    platform.right = platform.boundary_right
                    if platform.change_x > 0:
                        platform.change_x *= -1

                if check_for_collision(self.player_sprite, platform):
                    if platform.change_x < 0:
                        self.player_sprite.right = platform.left
                    if platform.change_x > 0:
                        self.player_sprite.left = platform.right

                platform.center_y += platform.change_y

                if platform.boundary_top is not None \
                        and platform.top >= platform.boundary_top:
                    platform.top = platform.boundary_top
                    if platform.change_y > 0:
                        platform.change_y *= -1

                if platform.boundary_bottom is not None \
                        and platform.bottom <= platform.boundary_bottom:
                    platform.bottom = platform.boundary_bottom
                    if platform.change_y < 0:
                        platform.change_y *= -1
コード例 #14
0
    def update(self):
        """
        Move everything and resolve collisions.
        """

        # --- Add gravity
        self.player_sprite.change_y -= self.gravity_constant

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.platforms)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            if self.player_sprite.change_y > 0:
                for item in hit_list:
                    self.player_sprite.top = min(item.bottom,
                                                 self.player_sprite.top)
            elif self.player_sprite.change_y < 0:
                for item in hit_list:
                    while check_for_collision(self.player_sprite, item):
                        self.player_sprite.bottom += 0.5
                    if item.change_x != 0:
                        self.player_sprite.center_x += item.change_x
            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
            self.player_sprite.change_y = min(0, hit_list[0].change_y)

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

        # Check for wall hit
        hit_list = \
            check_for_collision_with_list(self.player_sprite,
                                          self.platforms)

        # If we hit a wall, move so the edges are at the same point
        if len(hit_list) > 0:
            change_x = self.player_sprite.change_x
            if change_x > 0:
                for item in hit_list:
                    # See if we can "run up" a ramp
                    self.player_sprite.center_y += change_x
                    if check_for_collision(self.player_sprite, item):
                        self.player_sprite.center_y -= change_x
                        self.player_sprite.right = \
                            min(item.left, self.player_sprite.right)

            elif change_x < 0:
                for item in hit_list:
                    # See if we can "run up" a ramp
                    self.player_sprite.center_y -= change_x
                    if check_for_collision(self.player_sprite, item):
                        self.player_sprite.center_y -= change_x
                        self.player_sprite.left = max(item.right,
                                                      self.player_sprite.left)
            else:
                print("Error, collision while player wasn't moving.")

        for platform in self.platforms:
            if platform.change_x != 0 or platform.change_y != 0:
                platform.center_x += platform.change_x

                if platform.boundary_left is not None \
                        and platform.left <= platform.boundary_left:
                    platform.left = platform.boundary_left
                    if platform.change_x < 0:
                        platform.change_x *= -1

                if platform.boundary_right is not None \
                        and platform.right >= platform.boundary_right:
                    platform.right = platform.boundary_right
                    if platform.change_x > 0:
                        platform.change_x *= -1

                if check_for_collision(self.player_sprite, platform):
                    if platform.change_x < 0:
                        self.player_sprite.right = platform.left
                    if platform.change_x > 0:
                        self.player_sprite.left = platform.right

                platform.center_y += platform.change_y

                if platform.boundary_top is not None \
                        and platform.top >= platform.boundary_top:
                    platform.top = platform.boundary_top
                    if platform.change_y > 0:
                        platform.change_y *= -1

                if platform.boundary_bottom is not None \
                        and platform.bottom <= platform.boundary_bottom:
                    platform.bottom = platform.boundary_bottom
                    if platform.change_y < 0:
                        platform.change_y *= -1