Esempio n. 1
0
def pygame_mainloop():

    chess_board = ChessBoard()
    player = 0
    chess_board.set_allowed_color(player)
    screen = pygame.display.set_mode((640, 480))
    board_image, chess_image = load_images()
    selected = surface()

    is_selected_cell = False
    selected_cell = Point(None, None)
    offset = 120

    print ("player's", player, "turn")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return

            elif event.type == pygame.MOUSEBUTTONUP:
                new_position = make_a_point_from_mouse(event, offset)
                if is_into_board(new_position):
                    new_position.normalize_point()
                    if is_selected_cell:
                        is_selected_cell = False
                        can_play = chess_board.play(selected_cell, new_position)
                        if can_play:
                            player = change_player(player)
                            chess_board.set_allowed_color(player)
                            print ("player's", player, "turn")
                        else:
                            print("Invalid move!!! Try again")
                        selected_cell.set_point(None, None)
                    else:
                        selected_cell.set_point(
                            new_position.get_x(), new_position.get_y())
                        if chess_board.allowed_selection(selected_cell, player):
                            is_selected_cell = True

                        else:
                            selected_cell.set_point(None, None)

        screen.fill((109, 165, 165))
        screen.blit(board_image, (offset, offset))
        if selected_cell.get_x() is not None and \
                selected_cell.get_y() is not None:
            screen.blit(selected, (selected_cell.get_x()*32 + offset + 2,
                        selected_cell.get_y()*24 + offset + 2))
        prepare_board_to_update(chess_board, screen, chess_image, offset)
        pygame.display.update()
        time.sleep(0.04)
        if chess_board.is_end():
            print("The winner is Player {0}".format((player + 1) % 2))
            time.sleep(4)
            return
Esempio n. 2
0
    def test_allowed_selection_cell(self):

        # Test 1
        board = ChessBoard()
        selected_cell = Point(0, 0)
        player = 0
        self.assertFalse(board.allowed_selection(selected_cell, player))

        # Test 2
        selected_cell = Point(7, 7)
        player = 0
        self.assertTrue(board.allowed_selection(selected_cell, player))

        # Test 3
        selected_cell = Point(4, 4)
        player = 1
        self.assertFalse(board.allowed_selection(selected_cell, player))