def add_test_data(self): # ..... # ....# # .#.## # .#### tetromino = Tetromino(3, self.board) util.control_tetromino(tetromino, "WWWDSS") self.board.add_tetromino(tetromino) tetromino = Tetromino(6, self.board) util.control_tetromino(tetromino, "WWDDWDS") self.board.add_tetromino(tetromino)
def test_remove_multiple_full_rows(self): self.add_test_data() self.board.remove_full_rows(0, 4) tetromino = Tetromino(6, self.board) util.control_tetromino(tetromino, "DS") self.board.add_tetromino(tetromino) tetromino = Tetromino(1, self.board) self.board.add_tetromino(tetromino) self.board.remove_full_rows(0, 4) util.check_board_state(self, self.board, [".....", ".....", ".....", "#...."])
def test_remove_one_full_row(self): self.add_test_data() self.board.remove_full_rows(0, 4) util.check_board_state(self, self.board, [".....", "....#", ".#.##", ".####"]) tetromino = Tetromino(1, self.board) self.board.add_tetromino(tetromino) util.check_board_state(self, self.board, ["#....", "#...#", "##.##", "#####"]) self.board.remove_full_rows(0, 4) util.check_board_state(self, self.board, [".....", "#....", "#...#", "##.##"])
def test_moving_tetromino_outside_board_box(self): tetromino = Tetromino(0, self.board) self.assertFalse(util.control_tetromino(tetromino, "A")) self.assertFalse(util.control_tetromino(tetromino, "DDDD")) self.assertFalse(util.control_tetromino(tetromino, "SSS"))
def test_invalid_tetromino_move_inside_used_board(self): self.add_test_data() tetromino = Tetromino(0, self.board) self.assertFalse(util.control_tetromino(tetromino, "S")) self.assertFalse(util.control_tetromino(tetromino, "DDD"))
def test_tetromino_type_T_grid(self): tetromino = Tetromino(6, self.board) util.check_tetromino_state(self, tetromino, ["###", ".#."])
def test_moving_tetromino_inside_empty_board(self): tetromino = Tetromino(0, self.board) self.assertTrue(util.control_tetromino(tetromino, "DADDDSAADSDAD"))
def test_tetromino_type_S_grid(self): tetromino = Tetromino(4, self.board) util.check_tetromino_state(self, tetromino, [".##", "##."])
def test_tetromino_type_Z_grid(self): tetromino = Tetromino(5, self.board) util.check_tetromino_state(self, tetromino, ["##.", ".##"])
def test_tetromino_type_I_grid(self): tetromino = Tetromino(1, self.board) util.check_tetromino_state(self, tetromino, ["#", "#", "#", "#"])
def create_tetromino_type_I(self): self.tetromino = Tetromino(1, self.board)
def create_tetromino_type_T(self): self.tetromino = Tetromino(6, self.board)
class TetrisGame: def __init__(self, size, falling_speed): self.falling_tetromino: Tetromino = None self.board = Board(size) self.is_running = False self.gravity = PeriodicTask(lambda: self.handle_command(Command.FALL), falling_speed) self.board_state_change = NotifyEvent() self.tetromino_state_change = NotifyEvent() self.game_over = NotifyEvent() def start(self): self.is_running = self.generate_next_tetromino() if self.is_running: self.gravity.start() self.board_state_change.on_change(self.board) self.tetromino_state_change.on_change(self.falling_tetromino) def stop(self): self.is_running = False self.gravity.stop() self.game_over.on_change() def handle_command(self, command): if not self.is_running: return state_changed = False if command == Command.ROTATE: state_changed = self._rotate_tetromino() elif command == Command.MOVE_LEFT: state_changed = self._move_left_tetromino() elif command == Command.MOVE_RIGHT: state_changed = self._move_right_tetromino() elif command == Command.MOVE_DOWN: self.gravity.reset() state_changed = self._move_down_tetromino() elif command == Command.FALL: state_changed = self._move_down_tetromino() elif command == Command.DROP: while self._move_down_tetromino(): pass if state_changed and self.is_running: self.tetromino_state_change.on_change(self.falling_tetromino) def tetromino_landed(self): self.board.add_tetromino(self.falling_tetromino) y = self.falling_tetromino.position.y height = self.falling_tetromino.bounding_box.height self.board.remove_full_rows(y, height) self.board_state_change.on_change(self.board) added = self.generate_next_tetromino() if added: self.gravity.reset() self.tetromino_state_change.on_change(self.falling_tetromino) else: self.stop() def generate_next_tetromino(self): shape_type = self.get_next_tetromino_type() self.falling_tetromino = Tetromino(shape_type, self.board) return self.falling_tetromino.move_to_initial_pos() def get_next_tetromino_type(self): return random.randint(0, 6) def _rotate_tetromino(self): return self.falling_tetromino.rotate_right() def _move_left_tetromino(self): return self.falling_tetromino.move_left() def _move_down_tetromino(self): moved = self.falling_tetromino.move_down() if not moved: self.tetromino_landed() return moved def _move_right_tetromino(self): return self.falling_tetromino.move_right()
def generate_next_tetromino(self): shape_type = self.get_next_tetromino_type() self.falling_tetromino = Tetromino(shape_type, self.board) return self.falling_tetromino.move_to_initial_pos()