示例#1
0
    def test_dot_product(self):
        (v1, v2) = (3, 4.23)
        v = Vector2d(v1, v2)
        (w1, w2) = (8.65, 3.5)
        w = Vector2d(w1, w2)

        assert v.dotProduct(w) == v1 * w1 + v2 * w2
    def test_left_collision_with_object(self, collisionDetector, block):
        block.position = Vector2d(block.width/2, block.height/2)

        ball = collisionDetector.movingObject
        ball.position = Vector2d(block.position.x - block.width/2, block.position.y)

        collisionWithObject = collisionDetector.detectCollisionWithObject(block)
        assert collisionWithObject.happened == True
        assert collisionWithObject.hasVerticalIntersection == True
        assert collisionWithObject.hasHorizontalIntersection == True

        assert collisionWithObject.hasTopIntersection == False
        assert collisionWithObject.hasBottomIntersection == False
        assert collisionWithObject.hasRightIntersection == False
        assert collisionWithObject.hasLeftIntersection == True
示例#3
0
    def test_set_coordinates(self):
        v = Vector2d(1, 2)
        new_x, new_y = 3, 4
        v.x = new_x
        v.y = new_y

        assert v.x == new_x
        assert v.y == new_y
示例#4
0
    def __init__(self, engine, position=None):
        if engine is None:
            raise Exception("engine can not be None")
        self._engine = engine

        if position is None:
            self._position = Vector2d(0, 0)
        else:
            self._position = position
示例#5
0
    def initialize(self):
        self.__informationBarFont = Font(_INFORMATION_BAR_FONT_FILE,
                                         _INFORMATION_BAR_FONT_SIZE)
        self.__messageBoxFont = Font(_MESSAGE_BOX_FONT_FILE,
                                     _MESSAGE_BOX_FONT_SIZE)

        self.__paddle.position = Vector2d(
            self.__rectangle.right / 2.0,
            self.__rectangle.bottom + self.__paddle.height)

        self.__loadLevel(self.__levelFactory.buildLevel(1))
        self.__playBackgroundMusic()
        self.__state = GameState.PAUSE
示例#6
0
    def __buildBlocks(self):
        boundaries = self._engine.rectangle
        blockHeight = boundaries.height / float(_BLOCK_ROWS)
        blockWidth = boundaries.width / float(_BLOCK_COLUMNS)

        blocks = []
        blockColorIndex = 0
        for j in [
                27, 26, 25, 24, 23, 22, 21, 20, 15, 14, 13, 12, 11, 10, 9, 8
        ]:
            blockColor = BlockColor.selectInRainbowOrder(blockColorIndex)
            blockColorIndex += 1
            for i in xrange(3, _BLOCK_COLUMNS - 2):
                block = Block(self._engine,
                              blockColor,
                              width=blockWidth,
                              height=blockHeight)
                block.position = Vector2d(boundaries.left + i * blockWidth,
                                          boundaries.bottom + j * blockHeight)
                blocks.append(block)

        return blocks
示例#7
0
    def __buildBalls(self):
        boundaries = self._engine.rectangle

        ball1 = Ball(self._engine)
        ball1.position = Vector2d(boundaries.width / 2.0,
                                  boundaries.top - ball1.radius)
        ball1.speed = Vector2d(-_BALL_SPEED, _BALL_SPEED)

        ball2 = Ball(self._engine)
        ball2.position = Vector2d(boundaries.right - ball2.radius,
                                  boundaries.top - ball2.radius)
        ball2.speed = Vector2d(-_BALL_SPEED, -_BALL_SPEED)

        ball3 = Ball(self._engine)
        ball3.position = Vector2d(boundaries.left + ball3.radius,
                                  boundaries.top - ball3.radius)
        ball3.speed = Vector2d(_BALL_SPEED, -_BALL_SPEED)

        return [ball1, ball2, ball3]
示例#8
0
 def test_divide_by_scalar(self):
     v1 = Vector2d(3, 6)
     s = 3
     v2 = v1 / s
     assert v2.x == v1.x / s and v2.y == v1.y / s
示例#9
0
 def test_multiply_by_scalar_inplace(self):
     x, y = 3, 5
     s = 3
     v = Vector2d(x, y)
     v *= s
     assert v.x == x * s and v.y == y * s
示例#10
0
 def test_multiply_by_scalar(self):
     v1 = Vector2d(3, 5)
     s = 3
     v2 = v1 * s
     assert v2.x == v1.x * s and v2.y == v1.y * s
示例#11
0
 def test_subtract_scalar_inplace(self):
     x, y = 3, 5
     s = 3
     v = Vector2d(x, y)
     v += s
     assert v.x == x + s and v.y == y + s
示例#12
0
 def test_subtract_by_scalar(self):
     v1 = Vector2d(3, 5)
     s = 3
     v2 = v1 - s
     assert v2.x == v1.x - s and v2.y == v1.y - s
示例#13
0
 def test_add_scalar(self):
     v1 = Vector2d(3, 5)
     s = 3
     v2 = v1 + s
     assert v2.x == v1.x + s and v2.y == v1.y + s
示例#14
0
 def test_versor_from_non_zero_vector(self):
     v = Vector2d(10, 20)
     versor = v.versor()
     assert versor == v / abs(v)
示例#15
0
 def test_equal_if_coordinates_are_equal(self):
     v1 = Vector2d(2, 3.1)
     v2 = Vector2d(2, 3.1)
     assert v1 == v2
示例#16
0
 def test_not_equal_to_slightly_different_coordinates(self):
     v1 = Vector2d(2, 3)
     v2 = Vector2d(2, 3.1)
     assert not (v1 == v2)
示例#17
0
 def test_not_equal_to_none(self):
     v = Vector2d(2, 3)
     assert not v.__eq__(None)
示例#18
0
 def test_dot_product_between_two_vectors_90_degrees_away_is_zero(self):
     v1 = Vector2d(0, 50)
     v2 = Vector2d(100, 0)
     assert v1.dotProduct(v2) == 0
     assert v2.dotProduct(v1) == 0
def ball(gameEngine, originalSpeed):
    return Ball(gameEngine,
                radius=3,
                position=Vector2d(0, 0),
                speed=originalSpeed)
示例#20
0
 def test_norm(self):
     (v1, v2) = (3, 4)
     v = Vector2d(v1, v2)
     assert abs(v) == v.norm() == math.sqrt(v1 * v1 + v2 * v2)
示例#21
0
 def test_versor_from_zero_vector(self):
     v = Vector2d(0, 0)
     versor = v.versor()
     assert versor.x == 0 and versor.y == 0
示例#22
0
 def test_get_coordinates(self):
     x, y = 1, 2
     v = Vector2d(x, y)
     assert v.x == x == v[0] == v.coordinates[0]
     assert v.y == y == v[1] == v.coordinates[1]
示例#23
0
 def test_divide_by_scalar_inplace(self):
     x, y = 3, 6
     s = 3
     v = Vector2d(x, y)
     v /= s
     assert v.x == x / s and v.y == y / s
def originalSpeed():
    return Vector2d(-2.5, 3.8)
示例#25
0
 def test_not_equal_to_different_type(self):
     v = Vector2d(2, 3)
     assert not (v == (2, 3))
示例#26
0
 def test_projection(self):
     v1 = Vector2d(0, 50)
     v2 = Vector2d(1, 23)
示例#27
0
 def test_not_equal_to_very_different_coordinates(self):
     v1 = Vector2d(5, -35)
     v2 = Vector2d(2, 3.1)
     assert not (v1 == v2)
示例#28
0
 def test_to_string(self):
     v = Vector2d(1.5, 50.3)
     assert str(v) == "(1.5, 50.3)"
 def __init__(self, engine, position=None, speed=None):
     AbstractGameObject.__init__(self, engine, position)
     if speed is None:
         self._speed = Vector2d(0, 0)
     else:
         self._speed = speed
示例#30
0
 def test_negative(self):
     v1 = Vector2d(3, 5)
     v2 = -v1
     assert v2.x == -v1.x and v2.y == -v1.y