def rotate_piece(self): """Rotate a piece as long as it won't cause a collision.""" if not self.gameover and not self.paused: new_piece = helpers.rotate_clockwise(self.piece) if not helpers.check_collision(self.board, new_piece, (self.piece_x, self.piece_y)): self.piece = new_piece
def _get_costs_of_moves(board, piece): """Get the costs of all the moves given a board and piece.""" cost_to_move = {} max_x = len(board[0]) rotation_index = constants.TETRIS_SHAPES.index(piece) for rand_rotation in range(0, constants.SHAPE_TO_ROTATION[rotation_index]): if rand_rotation: piece = helpers.rotate_clockwise(piece) for new_x in range(0, max_x - len(piece[0]) + 1): interm_piece_y = 0 while not helpers.check_collision(board, piece, (new_x, interm_piece_y)): interm_piece_y += 1 interm_board, removed_rows = helpers.get_interm_board( board, piece, (new_x, interm_piece_y)) if not USE_DELLACHERIES: interm_cost = _calculate_simple_cost(interm_board, removed_rows) else: interm_cost = _calculate_dellacheries_cost( interm_board, removed_rows, (new_x, interm_piece_y)) cost_to_move[interm_cost] = (new_x, interm_piece_y, piece) return cost_to_move
def random_player(board, piece, shape_x, shape_y): """Player which returns a random move for a given piece and board.""" shape_x = shape_x final_shape = piece # Get random rotation rand_rotation = random.randint(0, 3) for _ in range(0, rand_rotation): piece = helpers.rotate_clockwise(piece) if not helpers.check_collision(board, piece, (shape_x, shape_y)): final_shape = piece # Get random x_position position new_x = random.randint(0, constants.CONFIG['cols']) if new_x > constants.CONFIG['cols'] - len(piece[0]): new_x = constants.CONFIG['cols'] - len(piece[0]) if not helpers.check_collision(board, piece, (new_x, shape_y)): shape_x = new_x return shape_x, final_shape
def new_piece(self): """Randomly spawn a new piece.""" self.piece = constants.TETRIS_SHAPES[rand(len( constants.TETRIS_SHAPES))] self.piece_x = int(constants.CONFIG['cols'] / 2 - len(self.piece[0]) / 2) self.piece_y = 0 if helpers.check_collision(self.board, self.piece, (self.piece_x, self.piece_y)): self.gameover = True
def move(self, delta_x): """For manual play move the piece left or right.""" if not self.gameover and not self.paused: new_x = self.piece_x + delta_x if new_x < 0: new_x = 0 if new_x > constants.CONFIG['cols'] - len(self.piece[0]): new_x = constants.CONFIG['cols'] - len(self.piece[0]) if not helpers.check_collision(self.board, self.piece, (new_x, self.piece_y)): self.piece_x = new_x
def manual_drop(self): """The drop function when playing manually. This slowly drops a piece to animate a falling piece on the board. When using the automatic tetris players we don't care about that so the drop functions are different. """ if not self.gameover and not self.paused: self.piece_y += 1 if helpers.check_collision(self.board, self.piece, (self.piece_x, self.piece_y)): self.board = helpers.add_piece_to_board( self.board, self.piece, (self.piece_x, self.piece_y)) self.new_piece() while True: for i, row in enumerate(self.board[:-1]): if 0 not in row: self.board = helpers.remove_row(self.board, i) self.score += 1 break else: break