@@ -71,6 +71,7 @@ def blitText(msg, center=CENTER, col=BLACK, bgcol=GREY, font=FONTSMALL, onclick=
7171def 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+
130150class 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+
221255def 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