Example #1
0
    def test_reflect_vertical(
        self,
        ball_top_left: Point,
        ball_angle: float,
        ball_result_top_left: Point,
        ball_result_angle: float,
        hit_objects_count: int,
    ):
        ball = Ball(
            top_left=ball_top_left,
            width=2.5,
            height=3.0,
            velocity=1.0,
            angle=Angle(deg2rad(ball_angle)),
        )

        bricks = [
            Brick(top_left=Point(1.0, 3.0), width=4.0, height=4.0),
            Brick(top_left=Point(5.0, 3.0), width=4.0, height=4.0),
            Brick(top_left=Point(9.0, 3.0), width=4.0, height=4.0),
        ]

        hit_objects = reflect_from_game_objects(ball, bricks)

        assert len(hit_objects) == hit_objects_count
        assert ball.top_left.x == ball_result_top_left.x
        assert ball.top_left.y == ball_result_top_left.y
        assert ball.angle.value == approx(deg2rad(ball_result_angle))
Example #2
0
    def test_reflect_from_three_objects_in_corner(
        self,
        brick1_top_left: Point,
        brick2_top_left: Point,
        brick3_top_left: Point,
        ball_top_left: Point,
        ball_angle: float,
        ball_result_top_left: Point,
        ball_result_angle: float,
    ):
        ball = Ball(
            top_left=ball_top_left,
            width=2.0,
            height=2.0,
            velocity=1.0,
            angle=Angle(deg2rad(ball_angle)),
        )

        bricks = [
            Brick(top_left=brick1_top_left, width=3.0, height=3.0),
            Brick(top_left=brick2_top_left, width=3.0, height=3.0),
            Brick(top_left=brick3_top_left, width=3.0, height=3.0),
        ]

        hit_objects = reflect_from_game_objects(ball, bricks)

        assert len(hit_objects) == 3
        assert ball.top_left.x == ball_result_top_left.x
        assert ball.top_left.y == ball_result_top_left.y
        assert ball.angle.value == approx(deg2rad(ball_result_angle))
Example #3
0
    def test_decrease_hitpoints(self):
        obj = Brick(top_left=Point(8.2, 1.3),
                    width=3.1,
                    height=4.2,
                    hitpoints=5)
        assert obj.start_hitpoints == 5
        assert obj.hitpoints == 5

        obj.decrease_hitpoints()
        assert obj.start_hitpoints == 5
        assert obj.hitpoints == 4
Example #4
0
 def test_init_throws_ValueError(self, hitpoints):
     with pytest.raises(ValueError):
         Brick(
             top_left=Point(8.2, 1.3),
             width=3.1,
             height=4.2,
             hitpoints=hitpoints,
         )
Example #5
0
    def test_init_default(self):
        obj = Brick()

        assert obj.top_left.x == 0.0
        assert obj.top_left.y == 0.0
        assert obj.width == 0.0
        assert obj.height == 0.0
        assert obj.start_hitpoints == 1
        assert obj.hitpoints == 1
Example #6
0
    def test_init(self):
        obj = Brick(top_left=Point(8.2, 1.3),
                    width=3.1,
                    height=4.2,
                    hitpoints=5)

        assert obj.top_left.x == 8.2
        assert obj.top_left.y == 1.3
        assert obj.width == 3.1
        assert obj.height == 4.2
        assert obj.start_hitpoints == 5
        assert obj.hitpoints == 5
Example #7
0
    def test_is_destroyed(self):
        obj = Brick(top_left=Point(8.2, 1.3),
                    width=3.1,
                    height=4.2,
                    hitpoints=1)
        assert obj.is_destroyed() == False

        obj.decrease_hitpoints()
        assert obj.is_destroyed() == True
Example #8
0
def _read_bricks_from_json_data(data: Dict) -> List[Brick]:
    bricks: List[Brick] = []
    if "bricks" in data:
        for brick_data in data["bricks"]:
            x: float = brick_data["top_left_x"]
            y: float = brick_data["top_left_y"]
            w: float = brick_data["width"]
            h: float = brick_data["height"]
            hp: int = brick_data["hitpoints"]
            brick = Brick(
                top_left=Point(x, y),
                width=w,
                height=h,
                hitpoints=hp,
            )
            bricks.append(brick)
    return bricks
Example #9
0
 def test_hitpoints(self):
     obj = Brick(top_left=Point(8.2, 1.3),
                 width=3.1,
                 height=4.2,
                 hitpoints=5)
     assert obj.hitpoints == 5
Example #10
0
 def _render_brick(self, brick: Brick):
     if brick.is_destroyed():
         return
     color = _get_brick_draw_color(brick)
     self._render_game_object(brick, color)