Skip to content

Commit e26a772

Browse files
committed
PlayerUI promotion screen
1 parent fa9a676 commit e26a772

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

Chess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def makeMove(game, oldCell, newCell, promoteTo:str='Q', _testMove=False) -> "Che
421421

422422
def isAttackMove(game, oldCell, newCell):
423423
# TODO add en passant
424-
if game.pieceAt(newCell) == None: return False
424+
if game.pieceAt(newCell) == None or game.pieceAt(oldCell) == None: return False
425425
return game.pieceAt(oldCell)[0] != game.pieceAt(newCell)[0]
426426

427427
def save(game, filename:str=None, comments=None):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pe2-e4 Pd7-d5 Pe4xPd5 Pc7-c6 Pd5xPc6 Ph7-h5 Pc6xPb7 Ph5-h4 Pg2-g3 Ph4xPg3 Pf2-f3 Pg3xPh2 Pf3-f4 Ph2xNg1=Q Pb7xRa8=N Nb8-c6 Pf4-f5 Pg7-g5 Pf5-f6 Pg5-g4 Pf6xPe7 Pg4-g3 Na8-c7 Qd8xNc7 Pd2-d3 Pg3-g2

playChess.py

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def blitText(msg, center=CENTER, col=BLACK, bgcol=GREY, font=FONTSMALL, onclick=
7171
def drawBoard(game:Chess, activeCell=False, center=CENTER, boardSide=BOARDSIDE, options=[], move=[None, None]):
7272
'''Draws the Board, the pieces, available moves, etc.'''
7373

74+
DISPLAY.fill(BLACK)
7475
blitText('Save Game', onclick=game.save, center=((WINDIM[0]-BOARDSIDE)/4, WINDIM[1]/2))
7576

7677
for x in range(4, -4, -1):
@@ -87,22 +88,22 @@ def drawBoard(game:Chess, activeCell=False, center=CENTER, boardSide=BOARDSIDE,
8788

8889
# display Piece
8990
# exec('piece=%s'%game.pieceAt(bcell).upper()) # doesn't work
90-
if game.pieceAt(bcell):
91-
if game.pieceAt(bcell) == 'wP': piece = WP
92-
elif game.pieceAt(bcell) == 'bP': piece = BP
93-
elif game.pieceAt(bcell) == 'wK': piece = WK
94-
elif game.pieceAt(bcell) == 'bK': piece = BK
95-
elif game.pieceAt(bcell) == 'wQ': piece = WQ
96-
elif game.pieceAt(bcell) == 'bQ': piece = BQ
97-
elif game.pieceAt(bcell) == 'wR': piece = WR
98-
elif game.pieceAt(bcell) == 'bR': piece = BR
99-
elif game.pieceAt(bcell) == 'wB': piece = WB
100-
elif game.pieceAt(bcell) == 'bB': piece = BB
101-
elif game.pieceAt(bcell) == 'wN': piece = WN
102-
elif game.pieceAt(bcell) == 'bN': piece = BN
103-
pr = piece.get_rect()
104-
pr.center = (center[0]-x*CELLSIDE+CELLSIDE/2, center[1]-y*CELLSIDE+CELLSIDE/2)
105-
DISPLAY.blit(piece, pr)
91+
if not game.pieceAt(bcell): continue
92+
if game.pieceAt(bcell) == 'wP': piece = WP
93+
elif game.pieceAt(bcell) == 'bP': piece = BP
94+
elif game.pieceAt(bcell) == 'wK': piece = WK
95+
elif game.pieceAt(bcell) == 'bK': piece = BK
96+
elif game.pieceAt(bcell) == 'wQ': piece = WQ
97+
elif game.pieceAt(bcell) == 'bQ': piece = BQ
98+
elif game.pieceAt(bcell) == 'wR': piece = WR
99+
elif game.pieceAt(bcell) == 'bR': piece = BR
100+
elif game.pieceAt(bcell) == 'wB': piece = WB
101+
elif game.pieceAt(bcell) == 'bB': piece = BB
102+
elif game.pieceAt(bcell) == 'wN': piece = WN
103+
elif game.pieceAt(bcell) == 'bN': piece = BN
104+
pr = piece.get_rect()
105+
pr.center = (center[0]-x*CELLSIDE+CELLSIDE/2, center[1]-y*CELLSIDE+CELLSIDE/2)
106+
DISPLAY.blit(piece, pr)
106107

107108
pygame.draw.rect(DISPLAY, GREY,
108109
(center[0]-boardSide//2-BORDER3, center[1]-boardSide//2-BORDER3, boardSide+BORDER3*2, boardSide+BORDER3*2),
@@ -127,6 +128,25 @@ def drawBoard(game:Chess, activeCell=False, center=CENTER, boardSide=BOARDSIDE,
127128
if game.isAttackMove(activeCell, option):
128129
pygame.draw.circle(DISPLAY, color, (x, y), CELLSIDE/6, BORDER2)
129130

131+
def drawPromotionMenu(game:Chess, activeOption:int=None, center=CENTER, boardSide=BOARDSIDE, pieceOrder:str='RQBN'):
132+
promoStart = center[0] - boardSide // 2
133+
bgColor = BLACK if game.isWhitesMove else WHITE
134+
fgColor = WHITE if game.isWhitesMove else BLACK
135+
pieceMap = {'R':[BR, WR], 'Q':[BQ, WQ], 'B':[BB, WB], 'N':[BN, WN] }
136+
pieceWidth = boardSide // 4
137+
138+
DISPLAY.fill(bgColor)
139+
pygame.draw.rect(DISPLAY, bgColor, (promoStart, center[1]-boardSide//2, boardSide, boardSide))
140+
for i, piece in enumerate(pieceOrder):
141+
icon = pieceMap[piece][game.isWhitesMove]
142+
rect = icon.get_rect()
143+
rect.center = (promoStart + ((i+.5) * pieceWidth), center[1])
144+
DISPLAY.blit(icon, rect)
145+
146+
if activeOption != None:
147+
rectParams = (promoStart + (activeOption * pieceWidth), center[1]-boardSide//8, pieceWidth, pieceWidth)
148+
pygame.draw.rect(DISPLAY, fgColor, rectParams, BORDER1)
149+
130150
class PlayerUI(Player):
131151

132152
def chooseMove(self, game:Chess) -> list:
@@ -135,15 +155,14 @@ def chooseMove(self, game:Chess) -> list:
135155
activeCell = [4, 4]
136156

137157
while True:
138-
DISPLAY.fill(BLACK)
139158
events = pygame.event.get()
140159
for event in events:
141160

142161
# mouse response
143162
if event.type == pygame.MOUSEMOTION:
144163
x, y = ((event.pos[0]-(CENTER[0]-BOARDSIDE//2))//CELLSIDE, (event.pos[1]-(CENTER[1]-BOARDSIDE//2))//CELLSIDE)
145164
if 0<=x<=7 and 0<=y<=7 and activeCell!=[x, y]:
146-
activeCell=[x, y]
165+
activeCell = [x, y]
147166
log(celllogs, 'activeCell : %s'%activeCell)
148167

149168
# click handles
@@ -207,20 +226,34 @@ def chooseMove(self, game:Chess) -> list:
207226
CLOCK.tick(FPS)
208227

209228
def choosePromotion(self, game: Chess) -> str:
210-
# return 'Q' #TODO: under construction
229+
activeOption = None
230+
pieceOrder = 'NQBR'
231+
211232
while True:
212-
pygame.draw.rect(DISPLAY, BLACK, (CENTER[0]-100, CENTER[1]-100, 200, 200))
213-
events = pygame.event.get()
214-
for event in events:
215-
if event.type==pygame.MOUSEBUTTONDOWN:
216-
return 'Q'
217-
233+
drawPromotionMenu(game=game, activeOption=activeOption, pieceOrder=pieceOrder)
218234
pygame.display.update()
219235
CLOCK.tick(FPS)
220236

237+
events = pygame.event.get()
238+
for event in events:
239+
if event.type == pygame.MOUSEMOTION:
240+
x = (event.pos[0]-(CENTER[0]-BOARDSIDE//2))//(CELLSIDE*2)
241+
if activeOption != x and 0 <= x < 4:
242+
activeOption = x
243+
elif event.type == pygame.MOUSEBUTTONDOWN: return pieceOrder[activeOption]
244+
elif event.type == pygame.KEYDOWN:
245+
if event.key == pygame.K_RIGHT:
246+
if activeOption == None: activeOption = 0
247+
else: activeOption = (activeOption + 1) % 4
248+
elif event.key == pygame.K_LEFT:
249+
if activeOption == None: activeOption = 3
250+
else: activeOption = (activeOption - 1) % 4
251+
elif event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
252+
return pieceOrder[activeOption]
253+
elif event.type == pygame.QUIT: safe_quit()
254+
221255
def gameOverScreen(game:Chess):
222256
result = GAME_RESULT[game.result]
223-
DISPLAY.fill(BLACK)
224257
drawBoard(game)
225258
pygame.display.update()
226259
pygame.time.wait(500)
@@ -242,7 +275,6 @@ def loadGame():
242275
bgcol, col = BLACK, GREY
243276
txt = 'Drop your saved File here'
244277
while True:
245-
DISPLAY.fill(BLACK)
246278
drawBoard(Chess())
247279
blitText(txt, (CENTER[0], CENTER[1]*0.8), col, bgcol, FONTBIG)
248280
blitText('press escape to go back', (CENTER[0], CENTER[1]*1.2), col, bgcol)

0 commit comments

Comments
 (0)