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)
def check_hit_brick(self, group_brick: pygame.sprite.RenderPlain) -> int: """ Check if the ball hits bricks in the `group_brick`. The hit bricks will be removed from `group_brick`, but the alive hard brick will not. However, if the ball speed is high, the hard brick will be removed with only one hit. @param group_brick The sprite group containing bricks @return The number of destroyed bricks """ hit_bricks = pygame.sprite.spritecollide(self, group_brick, 1, physics.collide_or_contact) num_of_destroyed_brick = len(hit_bricks) if num_of_destroyed_brick > 0: # XXX: Bad multiple collision bouncing handling if (num_of_destroyed_brick == 2 and (hit_bricks[0].rect.y == hit_bricks[1].rect.y or hit_bricks[0].rect.x == hit_bricks[1].rect.x)): combined_rect = hit_bricks[0].rect.union(hit_bricks[1].rect) physics.bounce_off_ip(self.rect, self._speed, combined_rect, (0, 0)) else: physics.bounce_off_ip(self.rect, self._speed, hit_bricks[0].rect, (0, 0)) if abs(self._speed[0]) == 7: for brick in hit_bricks: if isinstance(brick, HardBrick) and brick.hit(): group_brick.add((brick,)) num_of_destroyed_brick -= 1 return num_of_destroyed_brick
def check_hit_brick(self, group_brick: pygame.sprite.RenderPlain) -> int: hit_bricks = pygame.sprite.spritecollide(self, group_brick, 1, \ physics.collide_or_tangent) if len(hit_bricks) > 0: # XXX: Bad multiple collision bouncing handling if len(hit_bricks) == 2 and \ hit_bricks[0].rect.y == hit_bricks[1].rect.y: combined_rect = hit_bricks[0].rect.union(hit_bricks[1].rect) physics.bounce_off_ip(self.rect, self._speed, combined_rect, (0, 0)) else: physics.bounce_off_ip(self.rect, self._speed, hit_bricks[0].rect, (0, 0)) return len(hit_bricks)
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)