Ejemplo n.º 1
0
def main():
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gamestate = GameState()
    loadImages()
    running = True
    selected_square = ()  # Stores previously-clicked square
    clicks = []  # [(6,4),(4,4)] moves white pawn forward two
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            if e.type == p.MOUSEBUTTONDOWN:
                mouse_position = p.mouse.get_pos(
                )  # Gives (x, y) location of mouse
                col = mouse_position[0] // SQUARE_SIZE
                row = mouse_position[1] // SQUARE_SIZE
                if (selected_square == (row, col)):
                    selected_square = ()  # If already selected, deselect
                    clicks = []  # Clear clicks
                else:
                    selected_square = (row, col)
                    clicks.append(selected_square)
                if (len(clicks) == 2):
                    move = Move(clicks[0], clicks[1], gamestate.board)
                    print(move.getChessNotation())
                    gamestate.makeMove(move)
                    selected_square = ()
                    clicks = []

        drawGameState(screen, gamestate,
                      selected_square)  # Draws screen every tick
        clock.tick(FPS)
        p.display.flip()  # Updates screen every tick
Ejemplo n.º 2
0
def main():
  # Variables needed for pygame
  run = True
  
  pygame.init()
  clock = pygame.time.Clock()
  screen = pygame.display.set_mode((WIDTH, HEIGHT))
  pygame.display.set_caption('Chess')
  
  selectedSquare = () # (x, y)
  playerClicks = [] # Has 2 tuples Start, End
  validMoves = board.getValidMoves()
  
  madeMove = False # flag to only check valid moves once a move is made boosting performance
  
  while run:
    for event in pygame.event.get():
      # if user closes the program
      if event.type == pygame.QUIT:
        run = False
      elif event.type == pygame.MOUSEBUTTONDOWN:
        # if the user clicks the left mouse button
        if event.button == 1:
          # Gets the mouse location and divides by the square size this integer value is the index of the square
          location = pygame.mouse.get_pos()
          col = location[0] // SQUARE_SIZE
          row = location[1] // SQUARE_SIZE
          
          if selectedSquare == (row, col):
            selectedSquare = () # Deselect as user clicked twice
            playerClicks = [] # Reset Clicks
          else:  
            selectedSquare = (row, col)
            playerClicks.append(selectedSquare)

          if len(playerClicks) == 2:
            # deselects the clicks on empty squares
            if board.board[playerClicks[0][0]][playerClicks[0][1]] == 0:
              playerClicks = []
            else:
              # creates the Move object and executes the move if its in the Valid moves
              move = Move(playerClicks[0], playerClicks[1], board)
              print(move.getChessNotation())
              if move in validMoves:
                board.makeMove(move)
                madeMove = True
              # resets the variables for the next move
              selectedSquare = ()
              playerClicks = []
              
        elif event.button == 3:
          # if the user clicks the right mouse button
          board.undoMove()
          madeMove = True
      
      # if a move is made or unmade check for the valid moves for this position             
      if madeMove:
          validMoves = board.getValidMoves()
          madeMove = False
          
      clock.tick(FPS)
      drawGame(screen, validMoves, selectedSquare)      
      pygame.display.flip()
      
  pygame.quit()