from icecream import ic from graphics import Graphics class Draw: BOARD_SIZE = 64 SQUARE_SIZE = 8 PIECES = { "p": [ " . . . . . . ", " . . ■ ■ . . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " . . ■ ■ . . ", " . . . . . . "], "r": [ " ■ ■ ■ ■ ■ ■ ", " ■ - - - - ■ ", " . ■ - - ■ . ", " . ■ - - ■ . ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "n": [ " . . ■ ■ ■ . ", " . ■ - - - ■ ", " ■ - - - - ■ ", " . ■ - - - ■ ", " . . ■ - - ■ ", " . . ■ ■ ■ ■ "], "b": [ " . . ■ ■ . . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " . ■ - - ■ . ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "q": [ " ■ . . . . ■ ", " ■ ■ . . ■ ■ ", " ■ - ■ ■ - ■ ", " ■ - - - - ■ ", " ■ - - - - ■ ", " ■ ■ ■ ■ ■ ■ "], "k": [ " . . ■ ■ . . ", " ■ ■ - - ■ ■ ", " ■ - - - - ■ ", " ■ ■ - - ■ ■ ", " . ■ - - ■ . ", " . ■ ■ ■ ■ . "], } MENU = [ "New (W)", "New (B)", "Quit"] def __init__(self, graphics: Graphics): self._graphics = graphics def _draw_piece(self, piece: list[str], white: bool, sqx, sqy, sqc): c = 0 if sqc == 1 else 1 filled = (white and sqc == 0) or (not white and sqc == 1) for row in range(len(piece)): pixels = piece[row].replace(" ", "") for col in range(len(pixels)): x = (sqx * self.SQUARE_SIZE) + col + 1 y = (sqy * self.SQUARE_SIZE) + row + 1 pixel = pixels[col] if pixel == '■': self._graphics.pixel(x, y, c) elif pixel =='-': self._graphics.pixel(x, y, c if filled else sqc) def _clear_info(self): self._graphics.fill_rect( self.BOARD_SIZE, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) def draw_board(self, board: str): self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0) nb = "".join(board.split()) c = True for row in range(8): c = not c for col in range(8): if c: x = col * self.SQUARE_SIZE y = row * self.SQUARE_SIZE self._graphics.fill_rect(x, y, self.SQUARE_SIZE, self.SQUARE_SIZE, 1) p = nb[(row * 8) + col] if p.lower() in self.PIECES: self._draw_piece(self.PIECES[p.lower()], p.isupper(), col, row, c) c = not c self._graphics.show() def draw_menu(self, menu_index: int): self._clear_info() self._graphics.text("Menu:", 12, 0, 1) for i in range(len(self.MENU)): marker = "> " if i == menu_index else " " self._graphics.text(marker + self.MENU[i], 12, i + 2, 1) self._graphics.show()