Ejemplo n.º 1
0
def reflect_from_game_objects(
        ball: Ball, game_objects: List[GameObject]) -> List[GameObject]:
    """
    Reflect from game objects if ball has hit them.

    Get intersections with game objects.
    Reflect from one or several game objects.
    Return game objects which were hit.
    If brick was hit decrease hitpoints.
    """
    object_intersection_pairs = _get_object_intersection_pairs(
        ball, game_objects)
    if len(object_intersection_pairs) == 1:
        _reflect_from_single_object(
            ball=ball,
            game_object=object_intersection_pairs[0][0],
            intersection=object_intersection_pairs[0][1],
        )
        ball.angle = _clamp_angle(ball.angle)

        return [object_intersection_pairs[0][0]]
    if len(object_intersection_pairs) > 1:
        _reflect_from_multiple_objects(
            ball=ball, object_intersection_pairs=object_intersection_pairs)
        ball.angle = _clamp_angle(ball.angle)

        hit_objects: List[GameObject] = []
        for object_intersection_pair in object_intersection_pairs:
            hit_objects.append(object_intersection_pair[0])
        return hit_objects
    return []
Ejemplo n.º 2
0
    def test_angle(self):
        obj = Ball(
            top_left=Point(8.2, 1.3),
            width=3.1,
            height=4.2,
            velocity=2.4,
            angle=Angle(deg2rad(30.0)),
            gravity=9.8,
        )
        assert obj.angle.value == approx(deg2rad(30.0))

        obj.angle = Angle(deg2rad(65))
        assert obj.angle.value == approx(deg2rad(65.0))
Ejemplo n.º 3
0
def reflect_from_platform(ball: Ball, platform: Platform) -> bool:
    """
    Reflect from platform if ball has hit it.
    
    Get intersections with platform.
    If intersections exists reflect from the platform.
    Return True to indicate reflection.
    """
    intersection = _get_intersection(ball, platform)
    if intersection == _Intersection.NONE:
        return False
    _reflect_from_single_object(ball, platform, intersection)
    ball.angle = _clamp_angle(ball.angle)
    return True