def redraw_piece(piece_status): global UI_PIECE pc, px, py, pdir = piece_status opc, opx, opy, opdir = UI_PIECE old_shape = pieces.get_piece_shape(opc, opdir) new_shape = pieces.get_piece_shape(pc, pdir) old_region = set([(i + opx, j + opy) for i, j in old_shape]) new_region = set([(i + px, j + py) for i, j in new_shape]) if pc == opc: change_region = old_region ^ new_region else: change_region = old_region | new_region for i, j in change_region: if i < 0: continue if j < 0: continue if (i, j) in new_region: color = PIECE_COLOR[pc] else: color = PIECE_COLOR.get(UI_BOARD[j][i], BACKGROUND_COLOR) ui_change_rect_color(i, j, color) UI_PIECE = [pc, px, py, pdir]
def redraw_board(board): pc, px, py, pdir = UI_PIECE p_shape = pieces.get_piece_shape(pc, pdir) piece_region = [(i + px, j + py) for i, j in p_shape] for i, j in [(i, j) for i in range(boards.BOARD_WIDTH) for j in range(boards.BOARD_HEIGHT)]: if board[j][i] == UI_BOARD[j][i]: # board (i, j) not change continue UI_BOARD[j][i] = board[j][i] if (i, j) in piece_region: # ignore piece region continue color = PIECE_COLOR.get(board[j][i], BACKGROUND_COLOR) ui_change_rect_color(i, j, color)
def place_piece(piece, board): """ for i, j in piece: board[j + py][i + px] = pc """ pc, px, py, pdir = piece.get_status() p_shape = pieces.get_piece_shape(pc, pdir) for i, j in p_shape: x = px + i y = py + j if not (0 <= x < boards.BOARD_WIDTH): continue if not (0 <= y < boards.BOARD_HEIGHT): continue board.change_piece(x, y, pc) board.commit_status()
def collide(piece, board): """ collide = lambda piece, px, py: [1 for (i, j) in piece if board[j + py][i + px]] #是否碰撞 """ pc, px, py, pdir = piece p_shape = pieces.get_piece_shape(pc, pdir) for (i, j) in p_shape: x = px + i y = py + j if not (0 <= x < boards.BOARD_WIDTH): return True if y >= boards.BOARD_HEIGHT: return True if y < 0: continue if not board.is_piece_on_board(x, y, pieces.EMPTY): return True return False
def test_get_piece_shape_J0(self): piece_id = pieces.J_PIECE direction = 0 expect = [(1, 1), (1, 2), (1, 3), (2, 1)] result = pieces.get_piece_shape(piece_id, direction) self.assertEqual(result, expect)