示例#1
0
    def test_do_not_move_right_if_against_wall(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_x = 7

        p.handle_event(Event(KEYDOWN, { 'key': K_RIGHT }))
        self.assertEqual(p.current_x, 7)
示例#2
0
    def test_move_the_current_piece_to_the_right(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_x = 3

        p.handle_event(Event(KEYDOWN, { 'key': K_RIGHT }))

        self.assertEqual(p.current_x, 4)
示例#3
0
    def test_move_current_piece_down_should_reset_tick_counter(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_y = 0
        p.current_x = p.INITIAL_X
        p.drop_counter = 5

        p.handle_event(Event(KEYDOWN, { 'key': K_DOWN }))
        self.assertEqual(p.current_x, p.INITIAL_X)
        self.assertEqual(p.current_y, 1)
        self.assertEqual(p.drop_counter, p.ticks_per_drop)
示例#4
0
    def test_do_not_rotate_piece_if_the_next_position_is_in_conflict(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.L_SHAPE)
        p.current_x = 7

        blocks_before_rotate = [(0, 8), (1, 8), (2, 8), (2, 9)]
        for point in blocks_before_rotate:
            self.assertTrue(p.value_at(point[0], point[1]) > 0)

        p.handle_event(Event(KEYDOWN, { 'key': K_SPACE }))

        for point in blocks_before_rotate:
            self.assertTrue(p.value_at(point[0], point[1]) > 0)
示例#5
0
    def test_resetting_the_current_x_and_y_on_new_piece(self):
        """
        When a new piece is activated, the initial position should be reset to the
        top-center of the grid.
        """
        p = PlayerArea()
        p.current_piece = None
        p.current_y = 15
        p.current_x = 7

        p.tick()

        self.assertEqual(p.current_x, p.INITIAL_X)
        self.assertEqual(p.current_y, p.INITIAL_Y)
示例#6
0
    def test_attaching_piece_to_bottom_of_grid(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_y = 18
        p.current_x = p.INITIAL_X
        p.drop_counter = 0

        p.tick()

        self.assertIsNone(p.current_piece)

        for row in (18, 19):
            for col in (4, 5):
                index = (row * PlayerArea.GRID_COLUMNS) + col
                self.assertNotEqual(p.grid[index], 0)
示例#7
0
    def test_rotating_the_piece_clockwise(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.L_SHAPE)
        p.current_x = 3
        p.current_y = 0

        blocks_before_rotate = [(0, 4), (1, 4), (2, 4), (2, 5)]
        for point in blocks_before_rotate:
            self.assertTrue(p.value_at(point[0], point[1]) > 0)

        p.handle_event(Event(KEYDOWN, { 'key': K_SPACE }))

        blocks_after_rotate = [(1, 4), (1, 5), (1, 6), (2, 4)]
        for point in blocks_after_rotate:
            self.assertTrue(p.value_at(point[0], point[1]) > 0)
示例#8
0
    def test_getting_the_value_on_the_grid(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_y = 0
        p.current_x = p.INITIAL_X

        # Assign some random values on the grid.
        for point in [(0, 9), (5, 8), (6, 2), (13, 0)]:
            index = (point[0] * p.GRID_COLUMNS) + point[1]
            p.grid[index] = 1

        occupied_points = [(0, 9), (5, 8), (6, 2), (13, 0),
                           (0, 4), (0, 5), (1, 4), (1, 5)]
        empty_points = [(0, 0), (0, 3), (0, 6), (1, 3), (1, 6),
                        (10, 7), (7, 8), (15, 2), (19, 9)]
        for point in occupied_points:
            self.assertTrue(p.value_at(point[0], point[1]) > 0,
                            "(%s, %s) does not contain a value" % (point[0], point[1]))
        for point in empty_points:
            self.assertEqual(p.value_at(point[0], point[1]), 0,
                             "(%s, %s) contains a value" % (point[0], point[1]))