This repository has been archived on 2022-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
zeropod/plugin/chess/draw.py

126 lines
4.2 KiB
Python

from icecream import ic
from graphics import Graphics
class Draw:
BOARD_SIZE = 64
SQUARE_SIZE = 8
PIECES = {
"p": [
" . . . . . . ",
" . . ■ ■ . . ",
" . ■ - - ■ . ",
" . ■ - - ■ . ",
" . . ■ ■ . . ",
" . . . . . . "],
"r": [
" ■ ■ ■ ■ ■ ■ ",
" ■ - - - - ■ ",
" . ■ - - ■ . ",
" . ■ - - ■ . ",
" ■ - - - - ■ ",
" ■ ■ ■ ■ ■ ■ "],
"n": [
" . . ■ ■ ■ . ",
" . ■ - - - ■ ",
" ■ - - - - ■ ",
" . ■ - - - ■ ",
" . . ■ - - ■ ",
" . . ■ ■ ■ ■ "],
"b": [
" . . ■ ■ . . ",
" . ■ - - ■ . ",
" . ■ - - ■ . ",
" . ■ - - ■ . ",
" ■ - - - - ■ ",
" ■ ■ ■ ■ ■ ■ "],
"q": [
" ■ . . . . ■ ",
" ■ ■ . . ■ ■ ",
" ■ - ■ ■ - ■ ",
" ■ - - - - ■ ",
" ■ - - - - ■ ",
" ■ ■ ■ ■ ■ ■ "],
"k": [
" . . ■ ■ . . ",
" ■ ■ - - ■ ■ ",
" ■ - - - - ■ ",
" ■ ■ - - ■ ■ ",
" . ■ - - ■ . ",
" . ■ ■ ■ ■ . "],
}
MENU = [
"Play",
"Quit",
"New (W)",
"New (B)"]
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):
ic(board)
self._graphics.fill_rect(0, 0, self.BOARD_SIZE, self.BOARD_SIZE, 0)
nb = "".join(board.split())
c = True
for row in range(8):
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
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()
def draw_thinking(self):
self._clear_info()
self._graphics.text("Shh, I'm", 12, 0, 1)
self._graphics.text("thinking!", 12, 1, 1)
self._graphics.show()
def draw_checkmate(self, move: str, player_won: bool):
self._clear_info()
self._graphics.text(move, 12, 0, 1)
self._graphics.text("Checkmate", 12, 1, 1)
self._graphics.text(
"you win!" if player_won else "you lose!", 12, 2, 1)
self._graphics.show()
def draw_rowsel(self, move: str):
self._clear_info()
self._graphics.text(move, 12, 0, 1)
self._graphics.show()