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))
def test_top_left(self): obj = DerrivedGameObject(top_left=Point(x=10.1, y=20.2), width=10.5, height=20.6) assert obj.top_left.x == 10.1 assert obj.top_left.y == 20.2 obj.top_left = Point(x=20.3, y=30.5) assert obj.top_left.x == 20.3 assert obj.top_left.y == 30.5
def test_init(self): obj = IndestructibleBrick(Point(8.2, 1.3), 3.1, 4.2) assert obj.top_left.x == 8.2 assert obj.top_left.y == 1.3 assert obj.width == 3.1 assert obj.height == 4.2
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, ): GameObject.__init__(self, top_left, width, height)
def test_init(self): obj = Wall(Point(8.2, 1.3), 3.1, 4.2) assert obj.top_left.x == 8.2 assert obj.top_left.y == 1.3 assert obj.width == 3.1 assert obj.height == 4.2
def test_init(self): obj = DerrivedGameObject(Point(8.2, 1.3), 3.1, 4.2) assert obj.top_left.x == 8.2 assert obj.top_left.y == 1.3 assert obj.width == 3.1 assert obj.height == 4.2
def _calc_new_position( point: Point, velocity: float, angle: Angle, elapsed_time_in_ms: float ) -> Point: traveld_way = _calc_traveld_way(elapsed_time_in_ms, velocity) delta = _calc_delta(angle, traveld_way) return Point(point.x + delta.x, point.y + delta.y)
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, )
def test_init(self): obj = Platform(Point(8.2, 1.3), 3.1, 4.2, 1.2) 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.velocity == 1.2
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, velocity: float = 0.0, ): MoveableGameObject.__init__(self, top_left, width, height, velocity)
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
def test_move(self, velocity, end_x): obj = Platform(top_left=Point(0, 0), width=3.1, height=4.2, velocity=velocity) time_in_ms = 1000 obj.move(time_in_ms) assert obj.top_left.x == approx(end_x) assert obj.top_left.y == obj.top_left.y
def test_velocity(self): obj = DerrivedGameObject( top_left=Point(x=10.1, y=20.2), width=10.5, height=20.6, velocity=1.2, ) assert obj.velocity == 1.2 obj.velocity = 3.5 assert obj.velocity == 3.5
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
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
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))
def test_gravity(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.gravity == 9.8 obj.gravity = 4.5 assert obj.gravity == 4.5
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, hitpoints: int = 1, ): """ Raises ValueError if hitpoints not in range between 1 and 9. """ GameObject.__init__(self, top_left, width, height) self._start_hitpoints = _check_args_hitpoints(hitpoints) self._hitpoints = self._start_hitpoints
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, velocity: float = 0.0, angle: Angle = Angle(0.0), gravity: float = 0.0, ): MoveableGameObject.__init__(self, top_left, width, height, velocity) self._angle = angle self._gravity = gravity self._is_active = False
def _read_indestructible_bricks_from_json_data( data: Dict, ) -> List[IndestructibleBrick]: ind_bricks: List[IndestructibleBrick] = [] if "indestructible bricks" in data: for ind_brick_data in data["indestructible bricks"]: x: float = ind_brick_data["top_left_x"] y: float = ind_brick_data["top_left_y"] w: float = ind_brick_data["width"] h: float = ind_brick_data["height"] ind_brick = IndestructibleBrick(top_left=Point(x, y), width=w, height=h) ind_bricks.append(ind_brick) return ind_bricks
def test_move(self, active, velocity, degree, gravity, end_point): obj = Ball( top_left=Point(0.0, 0.0), width=3.1, height=4.2, velocity=velocity, angle=Angle(deg2rad(degree)), gravity=gravity, ) obj.is_active = active time_in_ms = 1000 obj.move(time_in_ms) assert obj.top_left.x == approx(end_point.x) assert obj.top_left.y == approx(end_point.y)
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, velocity: float = 0.0, ): """ Raises Exception on direct use as MoveableGameObject. """ if type(self) is MoveableGameObject: raise Exception( "MoveableGameObject is an abstract class and cannot be " "instantiated directly") GameObject.__init__(self, top_left, width, height) self._velocity = velocity
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
def __init__( self, top_left: Point = Point(0.0, 0.0), width: float = 0.0, height: float = 0.0, ): """ Raises ValueError if height, width or top left contain negative values. Raises Exception on direct use as GameObject. """ if type(self) is GameObject: raise Exception( "GameObject is an abstract class and cannot be instantiated " "directly") self._top_left = _check_args_point(top_left) self._witdh = _check_args_width(width) self._height = _check_args_height(height)
def test_init(self): obj = Ball( top_left=Point(8.2, 1.3), width=3.1, height=4.2, velocity=2.4, angle=Angle(deg2rad(90.0)), gravity=9.8, ) 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.velocity == 2.4 assert obj.angle.value == approx(deg2rad(90.0)) assert obj.gravity == 9.8 assert obj.is_active == False
def _calc_delta(angle: Angle, side_c: float) -> Point: if isclose(side_c, 0.0): return Point(0.0, 0.0) side_a = sin(angle.quadrant_angle) * side_c side_b = cos(angle.quadrant_angle) * side_c if angle.quadrant == Quadrant.I: return Point(side_b, side_a) if angle.quadrant == Quadrant.II: return Point(-side_a, side_b) if angle.quadrant == Quadrant.III: return Point(-side_b, -side_a) if angle.quadrant == Quadrant.IV: return Point(side_a, -side_b) return Point(0.0, 0.0)
def _ball_init_position(grid_width: int, grid_height: int) -> Point: return Point(x=grid_width / 2.0, y=grid_height - 2.0)
def _platform_init_position(platform_width: float, grid_width: int, grid_height: int): return Point(x=grid_width / 2.0 - platform_width / 2.0, y=grid_height - 1.0)
def _make_top_wall(grid_width: int) -> Wall: return Wall( top_left=Point(x=WALL_THICKNESS, y=0.0), width=grid_width, height=WALL_THICKNESS, )
def _make_right_wall(grid_width: int, grid_height: int) -> Wall: return Wall( top_left=Point(x=grid_width + WALL_THICKNESS, y=0.0), width=WALL_THICKNESS, height=grid_height + WALL_THICKNESS, )