Пример #1
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)
Пример #2
0
    def test_dropping_a_piece_to_the_floor_should_attach_to_grid(self):
        p = PlayerArea()
        p.current_piece = Piece(Piece.BLOCK_SHAPE)
        p.current_y = 0

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

        self.assertIsNone(p.current_piece, "should have attached the piece to the grid")

        filled_points = [(18, 4), (18, 5), (19, 4), (19, 5)]
        for point in filled_points:
            self.assertTrue(p.value_at(point[0], point[1]) > 0,
                            "(%s, %s) block should be filled" % (point[0], point[1]))
Пример #3
0
    def test_that_pieces_stack_on_each_other(self):
        p = PlayerArea()

        # Drop two pieces consecutively.
        for i in range(2):
            p.current_piece = Piece(Piece.BLOCK_SHAPE)
            p.current_y = 0
            p.handle_event(Event(KEYDOWN, { 'key': K_UP }))

        filled_points = [(18, 4), (18, 5), (19, 4), (19, 5),
                         (16, 4), (16, 5), (17, 4), (17, 5)]
        for point in filled_points:
            self.assertTrue(p.value_at(point[0], point[1]) > 0,
                            "(%s, %s) block should be filled" % (point[0], point[1]))
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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]))