Beispiel #1
0
def main():
    # init GUI
    game_display, clock = pygame_init_function()

    # Create new game board object and test print it
    chess_board = ChessBoard()
    chess_board.test_print_board()  # Test function TODO: delete me in the end

    # All needed variables, TODO: Check if needed here
    quit_game = False
    all_tiles = []
    all_pieces = []
    selected_piece = None

    # This var is a list that holds the position in pixels of all tiles in relation to the user GUI
    all_squares_parameters = create_squares_parameters()

    # Show the menu to the user
    game_menu(game_display, chess_board)

    # Board drawing function
    draw_chess_pieces(game_display, chess_board, all_pieces, all_tiles)

    # Create toolbar
    toolbar = Toolbar(CONSTANT_PIXEL_SIZE * 9, CONSTANT_PIXEL_SIZE / 2,
                      (128, 128, 128))

    # GUI game loop until user quits the game
    while not quit_game:
        click = pygame.mouse.get_pressed()

        # Check's if the user quited the game
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game = True
                pygame.quit()
                quit()

            # Check's if the user selected a tile
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 2:
                    game_menu()
                    new_pieces = print_pieces_images(chess_board)
                    all_pieces = new_pieces
                if selected_piece is None:
                    mouse_x, mouse_y = pygame.mouse.get_pos()
                    for piece in range(len(all_pieces)):
                        if all_pieces[piece][1][0] < mouse_x < all_pieces[
                                piece][1][0] + CONSTANT_PIXEL_SIZE:
                            if all_pieces[piece][1][1] < mouse_y < all_pieces[
                                    piece][1][1] + CONSTANT_PIXEL_SIZE:
                                selected_piece = piece
                    if 0 < mouse_x < CONSTANT_PIXEL_SIZE * 9 and 0 < mouse_y < CONSTANT_PIXEL_SIZE / 2:
                        toolbar.click((mouse_x, mouse_y), chess_board)
                        all_pieces = print_pieces_images(chess_board)
                    print(selected_piece)

            # Fallow the user mouse drag
            if event.type == pygame.MOUSEMOTION and selected_piece is not None:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                all_pieces[selected_piece][1][
                    0] = mouse_x - CONSTANT_PIXEL_SIZE / 2
                all_pieces[selected_piece][1][
                    1] = mouse_y - CONSTANT_PIXEL_SIZE / 2

            # TODO: create drag and drop GUI
            # Check's if the user released the mouse
            if event.type == pygame.MOUSEBUTTONUP and selected_piece is not None:
                try:
                    print("Mouse button up")
                    print("Selected piece: " + str(selected_piece))
                    the_move = 0
                    for mov_destination in range(64):
                        if all_squares_parameters[mov_destination][0] < all_pieces[selected_piece][1][0] + \
                                CONSTANT_PIXEL_SIZE / 2 < all_squares_parameters[mov_destination][1]:
                            if all_squares_parameters[mov_destination][2] < all_pieces[selected_piece][1][1] + \
                                    CONSTANT_PIXEL_SIZE / 2 < all_squares_parameters[mov_destination][3]:
                                the_move = mov_destination
                    all_pieces[selected_piece][1][0] = all_squares_parameters[
                        the_move][0]
                    all_pieces[selected_piece][1][1] = all_squares_parameters[
                        the_move][2]

                    chess_board.move(all_pieces[selected_piece][2], the_move)
                    all_pieces = print_pieces_images(chess_board)
                    chess_board.test_print_board(
                    )  # Test function TODO: delete me in the end

                # TODO: to broad except catch
                except Exception as e:
                    print(e)
                selected_piece = None

        # reset board
        game_display.fill((127, 78, 40))
        toolbar.draw(game_display)
        notation_drawing(game_display)

        # redraw tiles
        for info in all_tiles:
            pygame.draw.rect(game_display, info[0], info[1])

        # redraw images
        for img in all_pieces:
            game_display.blit(img[0], img[1])

        pygame.display.update()
        clock.tick(60)