-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
69 lines (67 loc) · 2.04 KB
/
ui.py
File metadata and controls
69 lines (67 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import chess
import os.path
from errors import *
from tkinter import *
root = Tk()
root.title("Chess Game")
root.geometry("800x600")
def square_pic(b: chess.board, i: int, j: int):
square = b.board_state[i][j]
color = (i + j) %2
if square.s_piece !=0:
return "resources/{0}_{1}_{2}.PNG".format(square.s_piece, square.s_color, color)
else:
return "resources/0_{0}.PNG".format(color)
def error_popup(root: Tk, error: str):
popup = Toplevel(root)
popup.geometry("300x60")
popup.title("Невозможный ход")
Label(popup, text = error).pack()
X = Button(popup, text="OK", command = popup.destroy).pack()
board_gfx=[]
pics=[]
moving_piece = None
ready_to_move = False
def press(b: chess.board, t: Button):
global moving_piece
global ready_to_move
x = t.grid_info()["column"]
y = 7-t.grid_info()["row"]
if ready_to_move:
try:
moving_piece.move(y, x)
except IllegalMove as error:
error_popup(root, error)
else:
b.turn += 1
board_update(b)
finally:
moving_piece = None
ready_to_move = False
else:
try:
if b.board_state[y][x].s_piece == 0:
raise NoPiece
elif b.board_state[y][x].s_color != b.turn % 2:
raise WrongPlayer
else:
moving_piece = b.board_state[y][x]
except IllegalMove as error:
error_popup(root, error)
else:
ready_to_move = True
def board_update(b: chess.board):
for i in range(len(board_gfx)):
board_gfx[i].destroy()
board_gfx.clear()
for i in range(7, -1, -1):
for j in range(8):
photo = PhotoImage(file=square_pic(b, i, j))
pics.append(photo)
t=Button(root, height=40, width=40, image=photo)
t.grid(row=7-i,column=j, sticky=W)
t.configure(command = lambda x=b, y=t: press (x, y ))
board_gfx.append(t)
b=chess.board()
board_update(b)
root.mainloop()