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)
def test_blocks_in_full_line_are_marked_to_be_cleared(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) self.assertEqual(p.counter_to_clear_blocks, 0) # Fill some points along the bottom of the grid with an opening # in the middle that will be filled by the current piece. prefilled_points = [(18, 0), (18, 1), (18, 2), (18, 3), (18, 6), (18, 7), (18, 8), (18, 9), (19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 # Drop the current piece to the floor, which should fill in two # complete lines, so every block on the grid will be cleared out. p.handle_event(Event(KEYDOWN, { 'key': K_UP })) for row in (18, 19): for col in range(PlayerArea.GRID_COLUMNS): self.assertEqual(p.value_at(row, col), PlayerArea.BLOCK_TO_BE_CLEARED, "block should be marked at (%s, %s)" % (row, col)) self.assertEqual(p.counter_to_clear_blocks, PlayerArea.TICKS_TO_CLEAR_BLOCKS)
def test_clearing_full_lines_after_attaching_piece(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) # Fill some points along the bottom of the grid with an opening # in the middle that will be filled by the current piece. prefilled_points = [(18, 0), (18, 1), (18, 2), (18, 3), (18, 6), (18, 7), (18, 8), (18, 9), (19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 # Drop the current piece to the floor, which should fill in two # complete lines, so every block on the grid will be cleared out. p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) for row in range(PlayerArea.GRID_ROWS): for col in range(PlayerArea.GRID_COLUMNS): self.assertEqual(p.value_at(row, col), 0, "should be clear at point (%s, %s)" % (row, col)) self.assertEqual(p.lines_cleared, 2)
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)
def test_handle_attempt_to_rotate_non_existent_current_piece(self): """ At various points in the game the current piece will be set to None such as when it was just attached to the grid. In these situations, a rotate command would attempt to call a method on a non-existent object, so include a check for not None before attempting to rotate the piece. """ p = PlayerArea() p.current_piece = None p.handle_event(Event(KEYDOWN, { 'key': K_SPACE })) self.assertIsNone(p.current_piece)
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)
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)
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]))
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]))
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)
def test_clearing_the_top_line(self): p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) for row in range(PlayerArea.GRID_ROWS): p.grid[row * PlayerArea.GRID_COLUMNS] = 1 prefilled_points = [(19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) self.assertEqual(p.value_at(0, 0), 0) self.assertTrue(p.value_at(1, 0) > 0)
def test_attempt_to_move_nonexistent_piece(self): """ At various points in the game, the current piece is set to None. Verify that attempting to move the current piece when it doesn't exist does not throw an exception. """ p = PlayerArea() p.current_piece = None p.handle_event(Event(KEYDOWN, { 'key': K_LEFT })) p.handle_event(Event(KEYDOWN, { 'key': K_RIGHT })) p.handle_event(Event(KEYDOWN, { 'key': K_DOWN })) p.handle_event(Event(KEYDOWN, { 'key': K_UP }))
def test_clearing_line_should_drop_lines_above(self): """ When clearing a line, all of the lines above it should be shifted down a row. """ p = PlayerArea() p.current_piece = Piece(Piece.BLOCK_SHAPE) prefilled_points = [(19, 0), (19, 1), (19, 2), (19, 3), (19, 6), (19, 7), (19, 8), (19, 9)] for point in prefilled_points: p.grid[(point[0] * PlayerArea.GRID_COLUMNS) + point[1]] = 1 p.handle_event(Event(KEYDOWN, { 'key': K_UP })) tick_til_lines_cleared(p) self.assertEqual(p.value_at(18, 4), 0, "block should not exist at (18, 4)") self.assertEqual(p.value_at(18, 5), 0, "block should not exist at (18, 5)") self.assertTrue(p.value_at(19, 4) > 0, "block should exist at (19, 4)") self.assertTrue(p.value_at(19, 5) > 0, "block should exist at (19, 5)") self.assertEqual(p.lines_cleared, 1)