Example #1
0
    def check_bouncing(self, platform_1p: Platform, platform_2p: Platform):
        physics.bounce_in_box(self.rect, self._speed, self._play_area_rect)

        # Check if the ball hits the platform or not
        target_platform = None
        cur_pos = Vector2(self.rect.x, self.rect.y)

        if physics.collide_or_tangent(self, platform_1p):
            target_platform = platform_1p
        elif physics.collide_or_tangent(self, platform_2p):
            target_platform = platform_2p
        # Additional checking for the ball passing through the corner of the platform
        # Determine if the routine of the ball intersects with the platform
        elif self.rect.bottom < platform_1p.rect.bottom:
            line_top_right = (cur_pos + Vector2(self.rect.width, 0), \
                self._last_pos + Vector2(self.rect.width, 0))
            line_top_left = (cur_pos, self._last_pos)

            if self._ball_routine_hit_platform( \
                platform_1p, line_top_right, line_top_left):
                target_platform = platform_1p

        elif self.rect.top > platform_2p.rect.top:
            line_bottom_right = (cur_pos + Vector2(self.rect.width, self.rect.height), \
                self._last_pos + Vector2(self.rect.width, self.rect.height))
            line_bottom_left = (cur_pos + Vector2(0, self.rect.height), \
                self._last_pos + Vector2(0, self.rect.height))

            if self._ball_routine_hit_platform( \
                platform_2p, line_bottom_right, line_bottom_left):
                target_platform = platform_2p

        if target_platform:
            physics.bounce_off_ip(self.rect, self._speed, \
                target_platform.rect, target_platform._speed)
Example #2
0
    def check_bouncing(self, platform_1p: Platform, platform_2p: Platform,
                       blocker: Blocker):
        # If the ball hits the play_area, adjust the position first
        # and preserve the speed after bouncing.
        hit_box = physics.rect_break_or_contact_box(self.rect,
                                                    self._play_area_rect)
        if hit_box:
            self.rect, speed_after_hit_box = (physics.bounce_in_box(
                self.rect, self._speed, self._play_area_rect))

        # If the ball hits the specified sprites, adjust the position again
        # and preserve the speed after bouncing.
        hit_sprite = self._check_ball_hit_sprites(
            (platform_1p, platform_2p, blocker))
        if hit_sprite:
            self.rect, speed_after_bounce = physics.bounce_off(
                self.rect, self._speed, hit_sprite.rect, hit_sprite._speed)

            # Check slicing ball when the ball is caught by the platform
            if (self._do_slide_ball and
                ((hit_sprite is platform_1p and speed_after_bounce[1] < 0) or
                 (hit_sprite is platform_2p and speed_after_bounce[1] > 0))):
                speed_after_bounce[0] = self._slice_ball(
                    self._speed, hit_sprite._speed[0])

        # Decide the final speed
        if hit_box:
            self._speed[0] = speed_after_hit_box[0]
        if hit_sprite:
            self._speed[1] = speed_after_bounce[1]
            if not hit_box:
                self._speed[0] = speed_after_bounce[0]
Example #3
0
 def check_bouncing(self, platform: Platform):
     if physics.collide_or_tangent(self, platform):
         physics.bounce_off_ip(self.rect, self._speed, platform.rect,
                               platform._speed)
     physics.bounce_in_box(self.rect, self._speed, self._play_area_rect)