def main(): p.init() screen = p.display.set_mode((WIDTH, HEIGHT)) clock = p.time.Clock() screen.fill(p.Color('white')) gs = ChessEngine.gameState() validMoves = gs.getValidMoves() moveMade = False # Flag variable for when a move is made animate = False # Flag for when to animate move loadImages() # Load Images only once, before while loop running = True square_selected = () # Keep track of last click of user, tuple:(row, col) player_clicks = [ ] # Keep track of player clicks, two tuples: [(r, c), (r, c)] gameOver = False while running: # ---------------------------------------------------------- MAIN LOOP ------------- # for e in p.event.get(): if e.type == p.QUIT: running = False if not gameOver: if e.type == p.MOUSEBUTTONDOWN: # -------------------------------Mouse Handler ---------- # location = p.mouse.get_pos() # (x, y) location of mouse col = location[ 0] // SQUARE_SIZE # These will need to be changed when more panels are added row = location[ 1] // SQUARE_SIZE # Gets mouse pos based on click location on screen if square_selected == ( row, col): # User clicked same square twice / Deselect square_selected = () # Deselect player_clicks = [] # Clear Player clicks else: square_selected = (row, col) player_clicks.append( square_selected) # Append first and second clicks if len(player_clicks ) == 2: # Check if seconds click, if so make move move = ChessEngine.Move(player_clicks[0], player_clicks[1], gs.board) for i in range(len(validMoves)): if move == validMoves[i]: gs.makeMove(validMoves[i]) moveMade = True animate = True square_selected = () # Reset User Clicks player_clicks = [] if not moveMade: player_clicks = [square_selected] elif e.type == p.KEYDOWN: # -----------------------------------------Key Handler------------ # if e.key == p.K_z: # Undo -- 'z' gs.undoMove() moveMade = True animate = False if e.key == p.K_r: # Reset Board -- 'r' gs = ChessEngine.gameState() validMoves = gs.getValidMoves() square_selected = () player_clicks = [] moveMade = False animate = False if moveMade: if animate: animateMove(gs.moveLog[-1], screen, gs.board, clock) validMoves = gs.getValidMoves( ) # Generate valid move after a move is made moveMade = False animate = False drawGameState(screen, gs, validMoves, square_selected) if gs.checkMate: gameOver = True if gs.whiteToMove: drawText(screen, 'Checkmate.\nBlack Wins.') else: drawText(screen, 'Checkmate.\nWhite Wins.') elif gs.staleMate: gameOver = True drawText(screen, 'Stalemate.') clock.tick(MAX_FPS) p.display.flip()
def main(): p.init() p.display.set_caption('Chess -B Nayak') screen = p.display.set_mode((WIDTH, HEIGHT)) clock = p.time.Clock() screen.fill(p.Color("white")) gs = ChessEngine.gameState() validMoves = gs.getValidMoves() moveMade = False # Flag variable for when move is made animate = False # Flag variable for when we should animate a move loadImages() running = True sqSelected = ( ) # no squareis selected , keep track of the last click of the user (tuple(row,column)) playerClicks = [] # keep track of player clicks [two tuple: [(6,4),(6,8)]] gameOver = False while running: for event in p.event.get(): if event.type == p.QUIT: running = False # Mouse Button Down elif event.type == p.MOUSEBUTTONDOWN: if not gameOver: location = p.mouse.get_pos( ) #get the mouse position as a tuple (x,y) col = location[0] // SQ_SIZE row = location[1] // SQ_SIZE if sqSelected == (row, col): # The user clicked twice sqSelected = () # deselected the square playerClicks = [] # clear player clicks else: sqSelected = (row, col) playerClicks.append( sqSelected ) # append for both first and second click if len(playerClicks) == 2: move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board) print(move.getChessNotaion()) for select_move in validMoves: if move == select_move: gs.makeMove(select_move) moveMade = True animate = True sqSelected = () # Reset user clicks playerClicks = [] if not moveMade: playerClicks = [sqSelected] # Key Button Down elif event.type == p.KEYDOWN: if event.key == p.K_z: #Undo when 'z' is pressed gs.undoMove() moveMade = True animate = False if event.key == p.K_r: #Reset the board when 'r' is pressed gs = ChessEngine.gameState() validMoves = gs.getValidMoves() sqSelected = () playerClicks = [] moveMade = False animate = False if moveMade: if animate: animateMove(gs.moveLog[-1], screen, gs.board, clock) validMoves = gs.getValidMoves() moveMade = False animate = False drawGameState(screen, gs, validMoves, sqSelected) if gs.checkMate: gameOver = True if gs.whiteToMove: drawText(screen, ' Black Wins CHECKMATE') else: drawText(screen, ' White Wins CHECKMATE') elif gs.staleMate: gameOver = True drawText(screen, ' Stalemate') clock.tick(MAX_FPS) p.display.flip() # Update the full display Surface to the screen