def main(): screen = p.display.set_mode((W, H)) clock = p.time.Clock() screen.fill(p.Color("blue")) gs = Engine.State() moveMade = False print(gs.board) LOADIMG() running = True sqSelected = () playerClicks = [] while running: # Run game for e in p.event.get(): # get event if e.type == p.QUIT: # if exit, quit running = False return "True" #MOUSE elif (e.type == p.MOUSEBUTTONDOWN): # get which square is clicked location = p.mouse.get_pos() col = location[0] // sq_size row = location[1] // sq_size if sqSelected == (row, col): # if its the same square, ignore sqSelected = () playerClicks = [] else: # if its diffrent append clicks sqSelected = (row, col) playerClicks.append(sqSelected) if len(playerClicks ) == 2: # if the player cicks twice, do the operations move = Engine.Move(playerClicks[0], playerClicks[1], gs.board) print(move.getChessNotation) gs.makeMove(move) moveMade = True sqSelected = () playerClicks = [] #BUTTON if e.type == p.KEYDOWN: # if "z"is pressed, undo move if e.key == p.K_z: gs.UndoMove(move) moveMade = True if moveMade: # if a move has been made, draw screen moveMade = False drawState(screen, gs) clock.tick(MAX_FPS) p.display.flip() print(gs.board)
def main(): pg.init() screen = pg.display.set_mode((WIDTH, HEIGHT)) clock = pg.time.Clock() screen.fill(pg.Color("white")) game_state = Engine.GameState() load_images() running = True sq_selected = () # keep track of the user's last click (row,column) player_clicks = [ ] # Keep track of # of user clicks (two tuples: [(6,4),(4,4)]) while running: for e in pg.event.get(): if e.type == pg.QUIT: running = False elif e.type == pg.MOUSEBUTTONDOWN: location = pg.mouse.get_pos() col = location[0] // SQ_SIZE row = location[1] // SQ_SIZE if sq_selected == (row, col): # user selected the same square twice sq_selected = () player_clicks = [] else: sq_selected = (row, col) player_clicks.append(sq_selected) #Resets the click if it is on an empty space if len(player_clicks) == 1 and game_state.board[ sq_selected[0]][sq_selected[1]].id == '--': sq_selected = () player_clicks = [] elif len(player_clicks) == 2: move = Engine.Move(player_clicks[0], player_clicks[1], game_state.board) if game_state.is_valid_move(move): print(move.get_chess_notation()) game_state.makeMove(move) sq_selected = () player_clicks = [] draw_game_state(screen, game_state) clock.tick(MAX_FPS) pg.display.flip()
def main(): p.init() screen = p.display.set_mode((WIDTH, HEIGHT)) clock = p.time.Clock() screen.fill(p.Color("white")) gs = Engine.GameState() validMoves = gs.getValidMoves() moveMade = False animate = False LoadImages() #Images only loaded once running = True sqSelected = () currentMove = [] #Keep track of starting and ending square coordinates gameOver = False while running: for e in p.event.get(): if e.type == p.QUIT: running = False #Moving pieces elif e.type == p.MOUSEBUTTONDOWN: if not gameOver: location = p.mouse.get_pos() col = location[0]//SQ_SIZE row = location[1]//SQ_SIZE if sqSelected == (row, col): #Player clicked same square twice, reset move. sqSelected = () currentMove = [] else: sqSelected = (row, col) currentMove.append(sqSelected) #Checks to see if move is valid if len(currentMove) == 2: move = Engine.Move(currentMove[0], currentMove[1], gs.board) print(move.getChessNotation()) for i in range(len(validMoves)): if move == validMoves[i]: gs.makeMove(validMoves[i]) moveMade = True animate = True sqSelected = () currentMove = [] if not moveMade: currentMove = [sqSelected] elif e.type == p.KEYDOWN: #Undo move using "z" if e.key == p.K_z: gs.unndoMove() moveMade = True #Don't animate when undoing animate = False #Reset board using "r" if e.key == p.K_r: gs = Engine.GameState() validMoves = gs.getValidMoves() sqSelected = () playerClicks = [] moveMade = False animate = False if moveMade: if animate: moveAnimation(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.whiteTurn: drawText(screen, 'Black Wins By Checkmate') else: drawText(screen, 'White Wns By Checkmate') elif gs.staleMate: gameOver = True drawText(screen, 'Stalemate') clock.tick(MAX_FPS) p.display.flip()