/
board_ui.py
279 lines (228 loc) · 9.75 KB
/
board_ui.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import random, pygame, sys
from pygame.locals import *
from chessEngine import calcAllPositions, enemySide, determineCheck, determineCheckmate
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 640
BOXSIZE = 69
BOARDWIDTH = 8
BOARDHEIGHT = 8
XMARGIN = 45
YMARGIN = 45
PIECE_SIZE = 65
BORDER_WIDTH = 48
#convert chess board coodinates to screen pixels
def coordToPixels( a,b ):
x = (BOXSIZE * a) + XMARGIN
y = (BOXSIZE * b) + YMARGIN
return x,y
#I just realized that this probably does the same thing
def leftTopCoordsOfBox(boxx, boxy):
left = boxx * (BOXSIZE) + XMARGIN
top = boxy * (BOXSIZE) + YMARGIN
return (left, top)
#returns chess baord coordinate from pixels given
def getTileAtPixel(x,y):
for boxx in range(BOARDWIDTH):
for boxy in range(BOARDHEIGHT):
left, top = leftTopCoordsOfBox(boxx, boxy)
boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
if boxRect.collidepoint(x, y):
return (boxx, boxy)
return (None, None)
#Player class containing data on pieces, color, state
class Player:
def __init__(self, pieces, moves, color):
self.pieces = pieces
self.possibleNextMoves = moves
self.color = color
self.inCheck = False
#calculate next possible moves for player
def calcNextMoves(self):
moveList = []
for piece in self.pieces:
for move in piece.moveset:
moveList.append(move)
self.possibleNextMoves = moveList
#sets player status wrt check or not in check
def checkStatus(self, enemy):
self.inCheck = determineCheck(self, enemy)
#Piece class containing data on spot, color, type
class Piece:
def __init__(self, rank, color, pixel_pos, board_pos, moveset=None):
self.color = color
self.rank = rank
self.img = pygame.image.load('img/' + color + '_' + rank + '.png')
self.img = pygame.transform.scale(self.img, (PIECE_SIZE,PIECE_SIZE))
self.pixel_pos = pixel_pos
self.board_pos = board_pos
self.moveset = moveset
#draws piece sprite to screen
def draw(self, displaySurf):
displaySurf.blit(self.img, self.pixel_pos)
#calculates possible moves for piece
def setMoves(self, board, players):
self.moveset = calcAllPositions(self.rank,
self.color,
self.board_pos,
board,
players)
#sets up the board with pieces from each side
def initBoard(board, displaySurf):
for i in range(8):
board[i][1] = Piece("pawn","black", coordToPixels(i,1), (i,1))
board[i][6] = Piece("pawn","white", coordToPixels(i,6), (i,6))
for rank,i in zip(["rook","knight","bishop"], range(3)):
board[i][0] = Piece(rank,"black", coordToPixels(i,0), (i,0))
board[i][7] = Piece(rank,"white", coordToPixels(i,7), (i,7))
for rank,i in zip(["bishop","knight","rook"], range(5,8)):
board[i][0] = Piece(rank,"black", coordToPixels(i,0), (i,0))
board[i][7] = Piece(rank,"white", coordToPixels(i,7), (i,7))
for side,i in zip(["black","white"], [0,7]):
board[4][i] = Piece("king",side, coordToPixels(4,i), (4,i))
board[3][i] = Piece("queen",side, coordToPixels(3,i), (3,i))
return board
#draw possible moves to screen
def drawMoves(moves, displaySurf):
for move in moves:
left, top = leftTopCoordsOfBox(move[0],move[1])
pygame.draw.rect(displaySurf,
(0,255,102),
pygame.Rect(left, top, BOXSIZE, BOXSIZE),
3)
#reinitializes the moves for each player, piece
def reInitMoves(board):
for i in range(BOARDWIDTH):
for j in range(BOARDHEIGHT):
if board[i][j] != None:
board[i][j].setMoves(board, players)
#draws the board and piece sprites
def drawBoard(board, displaySurf):
background = pygame.image.load('img/board.png')
displaySurf.blit(background, (0,0))
for i in range(BOARDWIDTH):
for j in range(BOARDHEIGHT):
if board[i][j] != None:
board[i][j].draw(displaySurf)
board[i][j].setMoves(board, players)
#draw piece at mouse position for "holding" effect
def drawPiece(piece, mousePos, displaySurf):
pygame.mouse.set_visible(False)
drawPos = (mousePos[0] - BOXSIZE/2, mousePos[1] - BOXSIZE/2)
displaySurf.blit(piece.img, drawPos)
#setup pygame library
pygame.init()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Chess')
pickup_sound = pygame.mixer.Sound("sounds/pickup.wav")
putdown_sound = pygame.mixer.Sound("sounds/putdown.wav")
checkmateTextFont = pygame.font.SysFont('didot.ttc',72)
checkmateTextImg = checkmateTextFont.render('CHECKMATE', True, (255,0,0))
checkmateTextRect = checkmateTextImg.get_rect()
checkmateTextRect.center = ( WINDOWWIDTH // 2 , WINDOWWIDTH // 2 )
#set up players
white = Player([], [], "white")
black = Player([], [], "black")
players = [white, black]
white.calcNextMoves()
black.calcNextMoves()
#set up board matrix
board = []
for i in range(BOARDHEIGHT):
row = []
for j in range(BOARDWIDTH):
row.append(None)
board.append(row)
board = initBoard(board,DISPLAYSURF)
drawBoard(board, DISPLAYSURF)
for i in range(BOARDHEIGHT):
for j in range(BOARDWIDTH):
if board[i][j] != None:
if board[i][j].color == "white":
white.pieces.append(board[i][j])
else:
black.pieces.append(board[i][j])
mousex = 0
mousey = 0
pieceBeingHeld = False
#first player is white
player = "white"
gameOver = False
#game loop starts
while True:
if gameOver:
DISPLAYSURF.blit(checkmateTextImg,checkmateTextRect)
else:
mouseClicked = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mousex, mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
mouseClicked = True
#if the chess piece is held, draw it on the board
if pieceBeingHeld == True:
drawBoard(board, DISPLAYSURF)
drawMoves(controlledPiece.moveset, DISPLAYSURF)
drawPiece(controlledPiece, (mousex,mousey), DISPLAYSURF)
#get board tile where mouse is at
tile_x, tile_y = getTileAtPixel(mousex, mousey)
if tile_x != None and tile_y != None:
if mouseClicked:
#if a piece is not being held and player clicked on a piece that belongs to them
if pieceBeingHeld == False and board[tile_x][tile_y] != None and board[tile_x][tile_y].color == player:
#piece is now picked up
controlledPiece = board[tile_x][tile_y]
board[tile_x][tile_y] = None
pieceBeingHeld = True
pygame.mixer.Sound.play(pickup_sound)
#if a piece is held and player clicked on a valid board spot
elif pieceBeingHeld and (tile_x,tile_y) in controlledPiece.moveset:
#place piece down
pygame.mixer.Sound.play(putdown_sound)
board[tile_x][tile_y] = controlledPiece
board[tile_x][tile_y].board_pos = (tile_x,tile_y)
board[tile_x][tile_y].pixel_pos = coordToPixels(tile_x,tile_y)
pieceBeingHeld = False
#recalc all the moves
reInitMoves(board)
white.calcNextMoves()
black.calcNextMoves()
#determine check and checkmate status
white.checkStatus(black)
black.checkStatus(white)
if determineCheckmate(black,white,board):
DISPLAYSURF.blit(checkmateTextImg, (0,0))
print("BLACK HAS BEEN CHECKMATED")
gameOver = True
if determineCheckmate(white,black,board):
DISPLAYSURF.blit(checkmateTextImg, (0,0))
print("WHITE HAS BEEN CHECKMATED")
gameOver = True
if white.inCheck:
print("WHITE IS IN CHECK")
if black.inCheck:
print("BLACK IS IN CHECK")
#flip player
player = enemySide(player)
print("it is now " + player + "'s turn")
pygame.mouse.set_visible(True)
#if a piece is held and player tries to click on invalid spot
elif pieceBeingHeld and (tile_x,tile_y) not in controlledPiece.moveset and (tile_x,tile_y) != controlledPiece.board_pos:
print("Invalid move")
#if piece is held and player clicks on original spot
elif pieceBeingHeld and (tile_x,tile_y) == controlledPiece.board_pos:
#place piece back at original position
pygame.mixer.Sound.play(putdown_sound)
pygame.mouse.set_visible(True)
board[tile_x][tile_y] = controlledPiece
board[tile_x][tile_y].board_pos = (tile_x,tile_y)
board[tile_x][tile_y].pixel_pos = coordToPixels(tile_x,tile_y)
pieceBeingHeld = False
#draw board with new updates
drawBoard(board, DISPLAYSURF)
pygame.display.update()
print("Checkmate! Game over...")