-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess.py
113 lines (85 loc) · 3.08 KB
/
Chess.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pygame
import chess
import math
X = 800
Y = 800
screen = pygame.display.set_mode((X, Y))
pygame.init()
# Colors
WHITE = (255, 255, 255)
GREY = (128, 128, 128)
BLUE = (50, 255, 255)
BLACK = (0, 0, 0)
b = chess.Board()
pieces = {
'p': pygame.image.load('b_pawn.png'),
'n': pygame.image.load('b_knight.png'),
'b': pygame.image.load('b_bishop.png'),
'r': pygame.image.load('b_rook.png'),
'q': pygame.image.load('b_queen.png'),
'k': pygame.image.load('b_king.png'),
'P': pygame.image.load('w_pawn.png'),
'N': pygame.image.load('w_knight.png'),
'B': pygame.image.load('w_bishop.png'),
'R': pygame.image.load('w_rook.png'),
'Q': pygame.image.load('w_queen.png'),
'K': pygame.image.load('w_king.png'),
}
def update(screen_, board):
for i in range(64):
piece = board.piece_at(i)
if piece is not None:
screen_.blit(pieces[str(piece)], ((i % 8) * 100, 700 - (i // 8) * 100))
for i in range(1, 8):
pygame.draw.line(screen_, WHITE, (0, i * 100), (800, i * 100))
pygame.draw.line(screen_, WHITE, (i * 100, 0), (i * 100, 800))
pygame.display.flip()
def main(board_):
screen.fill(GREY)
pygame.display.set_caption('Chess')
index_moves = []
status = True
while status:
# update screen
update(screen, board_)
for event in pygame.event.get():
if event.type == pygame.QUIT:
status = False
if event.type == pygame.MOUSEBUTTONDOWN:
screen.fill(GREY)
pos = pygame.mouse.get_pos()
square = (math.floor(pos[0] / 100), math.floor(pos[1] / 100))
index = (7 - square[1]) * 8 + (square[0])
if index in index_moves:
# noinspection PyUnboundLocalVariable
move = moves[index_moves.index(index)]
board_.push(move)
# noinspection PyUnusedLocal
index = None
index_moves = []
else:
piece = board_.piece_at(index)
if piece is None:
pass
else:
all_moves = list(board_.legal_moves)
moves = []
for move in all_moves:
if move.from_square == index:
moves.append(move)
to = move.to_square
TX = 100 * (to % 8)
TY = 100 * (7 - to // 8)
pygame.draw.rect(screen, BLUE, pygame.Rect(TX, TY, 100, 100), 5)
index_moves = [a.to_square for a in moves]
if board_.outcome() is not None:
print(board_.outcome())
status = False
print(board_)
if "False" in str(board_.outcome()):
print("\nBLACK SIDE WON!\n")
else:
print("\nWHITE SIDE WON!\n")
pygame.quit()
if __name__ == "__main__":
main(b)