More chess stuff
This commit is contained in:
parent
eba9ea62bb
commit
b6fdd93850
|
@ -4,6 +4,8 @@ import io
|
|||
from PIL import Image, ImageDraw, ImageTk, ImageFont
|
||||
import sys
|
||||
from random import randint as rand
|
||||
import chess
|
||||
import chess.pgn
|
||||
|
||||
button_names = ('close', 'cookbook', 'cpu', 'github', 'pysimplegui', 'run', 'storage', 'timer')
|
||||
|
||||
|
@ -34,38 +36,62 @@ initial_board = [[ROOKB, KNIGHTB, BISHOPB, KINGB, QUEENB, BISHOPB, KNIGHTB, ROO
|
|||
[PAWNW for _ in range(8)],
|
||||
[ROOKW, KNIGHTW, BISHOPW, KINGW, QUEENW, BISHOPW, KNIGHTW, ROOKW]]
|
||||
|
||||
def get_image_bytes(image64):
|
||||
image_file = io.BytesIO(base64.b64decode(image64))
|
||||
img = Image.open(image_file)
|
||||
bio = io.BytesIO()
|
||||
img.save(bio, format='PNG')
|
||||
imgbytes = bio.getvalue()
|
||||
return imgbytes
|
||||
blank = os.path.join(CHESS_PATH, 'blank.png')
|
||||
bishopB = os.path.join(CHESS_PATH, 'bishopb.png')
|
||||
bishopW = os.path.join(CHESS_PATH, 'bishopw.png')
|
||||
pawnB = os.path.join(CHESS_PATH, 'pawnb.png')
|
||||
pawnW = os.path.join(CHESS_PATH, 'pawnw.png')
|
||||
knightB = os.path.join(CHESS_PATH, 'knightb.png')
|
||||
knightW = os.path.join(CHESS_PATH, 'knightw.png')
|
||||
rookB = os.path.join(CHESS_PATH, 'rookb.png')
|
||||
rookW = os.path.join(CHESS_PATH, 'rookw.png')
|
||||
queenB = os.path.join(CHESS_PATH, 'queenB.png')
|
||||
queenW = os.path.join(CHESS_PATH, 'queenW.png')
|
||||
kingB = os.path.join(CHESS_PATH, 'kingb.png')
|
||||
kingW = os.path.join(CHESS_PATH, 'kingw.png')
|
||||
|
||||
|
||||
images = {BISHOPB: bishopB, BISHOPW: bishopW, PAWNB: pawnB, PAWNW: pawnW, KNIGHTB: knightB, KNIGHTW: knightW,
|
||||
ROOKB: rookB, ROOKW: rookW, KINGB: kingB, KINGW: kingW, QUEENB: queenB, QUEENW: queenW, BLANK: blank}
|
||||
|
||||
def get_a_move():
|
||||
board = chess.Board()
|
||||
pgn = open('C:/Python/PycharmProjects/GooeyGUI/Chess/game.pgn')
|
||||
first_game = chess.pgn.read_game(pgn)
|
||||
for move in first_game.main_line():
|
||||
print(move)
|
||||
yield move
|
||||
|
||||
|
||||
def render_square(image, key, location):
|
||||
if (location[0] + location[1]) % 2:
|
||||
color = '#B58863'
|
||||
else:
|
||||
color = '#F0D9B5'
|
||||
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', color), pad=(0, 0), key=key)
|
||||
|
||||
|
||||
def redraw_board(window, board):
|
||||
for i in range(8):
|
||||
for j in range(8):
|
||||
if (i+j) % 2:
|
||||
color = '#B58863'
|
||||
else:
|
||||
color = '#F0D9B5'
|
||||
piece_image = images[board[i][j]]
|
||||
elem = window.FindElement(key=(i,j))
|
||||
elem.Update(button_color = ('white', color),
|
||||
image_filename=piece_image,)
|
||||
|
||||
def update_board(board):
|
||||
# board[5][5] = KINGB
|
||||
return board
|
||||
|
||||
def DrawBoard():
|
||||
blank = os.path.join(CHESS_PATH, 'blank.png')
|
||||
bishopB = os.path.join(CHESS_PATH, 'bishopb.png')
|
||||
bishopW = os.path.join(CHESS_PATH, 'bishopw.png')
|
||||
pawnB = os.path.join(CHESS_PATH, 'pawnb.png')
|
||||
pawnW = os.path.join(CHESS_PATH, 'pawnw.png')
|
||||
knightB = os.path.join(CHESS_PATH, 'knightb.png')
|
||||
knightW = os.path.join(CHESS_PATH, 'knightw.png')
|
||||
rookB = os.path.join(CHESS_PATH, 'rookb.png')
|
||||
rookW = os.path.join(CHESS_PATH, 'rookw.png')
|
||||
queenB = os.path.join(CHESS_PATH, 'queenB.png')
|
||||
queenW = os.path.join(CHESS_PATH, 'queenW.png')
|
||||
kingB = os.path.join(CHESS_PATH, 'kingb.png')
|
||||
kingW = os.path.join(CHESS_PATH, 'kingw.png')
|
||||
|
||||
images = { BISHOPB:bishopB, BISHOPW:bishopW, PAWNB:pawnB, PAWNW:pawnW, KNIGHTB : knightB, KNIGHTW: knightW, ROOKB:rookB, ROOKW:rookW, KINGB:kingB, KINGW:kingW, QUEENB:queenB, QUEENW:queenW, BLANK:blank}
|
||||
|
||||
sg.SetOptions(auto_size_buttons=True, margins=(0,0), button_color=sg.COLOR_SYSTEM_DEFAULT)
|
||||
|
||||
def render_tan(image):
|
||||
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', '#B58863'), pad=(0, 0), key='_close_')
|
||||
def render_brn(image):
|
||||
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', '#F0D9B5'), pad=(0, 0), key='_close_')
|
||||
|
||||
brn = sg.RButton('',image_filename=blank, size=(1,1), button_color=('white','#B58863') ,pad=(0,0), key='_close_')
|
||||
tan = sg.RButton('', image_filename=blank, size=(4,3), button_color=('white','#F0D9B5') ,pad=(0,0))
|
||||
|
||||
|
@ -77,12 +103,16 @@ def DrawBoard():
|
|||
row = [sg.T(str(8-i)+' ', font='Any 13')]
|
||||
for j in range(8):
|
||||
piece_image = images[board[i][j]]
|
||||
if (i+j) % 2:
|
||||
row.append(render_tan(piece_image))
|
||||
else:
|
||||
row.append(render_brn(piece_image))
|
||||
row.append(render_square(piece_image, key=(i,j), location=(i,j)))
|
||||
# if (i+j) % 2:
|
||||
# row.append(render_tan(piece_image, key=(i,j)))
|
||||
# else:
|
||||
# row.append(render_brn(piece_image, key=(i,j)))
|
||||
row.append(sg.T(str(8-i)+' ', font='Any 13'))
|
||||
board_layout.append(row)
|
||||
|
||||
board_layout.append([sg.T(' ')] + [sg.T(f'{a}', pad=((23,27),0), font='Any 13') for a in 'abcdefgh'])
|
||||
|
||||
controls_layout = [[sg.Text('Performance Parameters', font='Any 20')]]
|
||||
statistics_layout = [[sg.Text('Statistics', font=('Any 20'))]]
|
||||
|
||||
|
@ -96,10 +126,29 @@ def DrawBoard():
|
|||
keep_on_top=True,
|
||||
icon='kingb.ico').Layout(layout)
|
||||
|
||||
pgn = open('C:/Python/PycharmProjects/GooeyGUI/Chess/game.pgn')
|
||||
first_game = chess.pgn.read_game(pgn)
|
||||
moves = [move for move in first_game.main_line()]
|
||||
# ---===--- Loop taking in user input --- #
|
||||
i = 0
|
||||
while True:
|
||||
board = update_board(board)
|
||||
button, value = window.Read()
|
||||
if button is None:
|
||||
break
|
||||
|
||||
move = moves[i]
|
||||
move_from = move.from_square
|
||||
move_to = move.to_square
|
||||
print(move_from, move_to)
|
||||
row = move_from//8
|
||||
col = move_from%8
|
||||
print(row, col)
|
||||
piece = board[row][col]
|
||||
board[row][col] = BLANK
|
||||
row = move_to//8
|
||||
col = move_to % 8
|
||||
board[row][col] = piece
|
||||
print(piece)
|
||||
redraw_board(window, board)
|
||||
i += 1
|
||||
DrawBoard()
|
Loading…
Reference in New Issue