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]
def check_bouncing(self, platform: Platform): if (physics.collide_or_contact(self, platform) or self._platform_additional_check(platform)): self.hit_platform_times += 1 rect_after_bounce, speed_after_bounce = physics.bounce_off( self.rect, self._speed, platform.rect, platform._speed) # Check slicing ball when the ball goes up after bouncing (not game over) if self._do_slide_ball and speed_after_bounce[1] < 0: speed_after_bounce[0] = self._slice_ball(self._speed[0], platform._speed[0]) self.rect = rect_after_bounce self._speed = speed_after_bounce if physics.rect_break_or_contact_box(self.rect, self._play_area_rect): physics.bounce_in_box_ip(self.rect, self._speed, self._play_area_rect)