-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (70 loc) · 3.51 KB
/
main.py
File metadata and controls
79 lines (70 loc) · 3.51 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
70
71
72
73
74
75
76
77
78
79
import os
from chess import *
from tkinter import *
from PIL import Image, ImageTk
def click(event):
global row, col, row1, col1
if row is None:
row, col = 7 - (root.winfo_pointery() - root.winfo_rooty() - 80) // 80, (root.winfo_pointerx() - root.winfo_rootx() - 80) // 80
cell = board.field[row][col]
if not cell:
row, col = None, None
elif cell.color != board.color:
row, col = None, None
else:
for d in cell.moves:
temp_row, temp_col = row, col
while True:
temp_row += d[0]
temp_col += d[1]
if not correct_coords(temp_row, temp_col):
break
if cell.can_move(board, row, col, temp_row, temp_col):
Label(root, image=images[board.cell(temp_row, temp_col)], borderwidth=2, relief="solid", bg="#bcefd0").place(
x=120 + 80 * temp_col,
y=680 - 80 * temp_row,
anchor="center")
if cell.char == "P":
if cell.can_attack(board, row, col, temp_row, temp_col):
Label(root, image=images[board.cell(temp_row, temp_col)], borderwidth=2, relief="solid",
bg="#bcefd0").place(
x=120 + 80 * temp_col,
y=680 - 80 * temp_row,
anchor="center")
if cell.one_move:
break
else:
row1, col1 = 7 - (root.winfo_pointery() - root.winfo_rooty() - 80) // 80, (
root.winfo_pointerx() - root.winfo_rootx() - 80) // 80
if board.move_piece(row, col, row1, col1):
root.title("Ход: "+ {WHITE: "белые", BLACK: "черные"}[board.color])
update_board()
if board.game_over:
root.unbind('<Button-1>')
root.title("Конец игры")
T = Text(root, height=2, width=20, font=("Courier", 35))
T.pack()
return T.insert(END, f" Игра завершена.\nПобедил {'БЕЛЫЙ' if board.game_over == WHITE else 'ЧЕРНЫЙ'} игрок!")
x, y = board.king_pos[board.color]
if board.is_under_attack(x, y):
Label(root, image=images[board.cell(x, y)], borderwidth=2, bg="#ff0000",
relief="solid").place(x=120 + 80 * y,
y=680 - 80 * x,
anchor="center")
row, col, row1, col1 = None, None, None, None
def update_board():
for x in range(8):
for y in range(8):
Label(root, image=images[board.cell(x, y)], borderwidth=2, bg=["#bf6d24", "#ffe4cd"][(x + y) % 2], relief="solid").place(x=120 + 80 * y,
y=680 - 80 * x,
anchor="center")
board = Board()
root = Tk()
row, col, row1, col1 = None, None, None, None
root.title("Ход: "+ {WHITE: "белые", BLACK: "черные"}[board.color])
root.minsize(width=800, height=800)
root.bind('<Button-1>', click)
images = {x[:2]: ImageTk.PhotoImage(Image.open("assets/" + x).resize((80, 80))) for x in os.listdir("assets")}
images[" "] = ImageTk.PhotoImage(Image.new('RGBA', (80, 80), (255, 0, 0, 0)))
update_board()
root.mainloop()