Python多重而真正的循环,寻找一个整洁的方式???

时间:2017-11-13 05:10:28

标签: python python-3.x

您好我正在寻找一种简洁的方法来拥有多个while循环。

我知道你可以在彼此之下有多个嵌套的while循环来保持连续流动。

在下面的代码中,第一个while循环遍历并点击士兵,第二个while循环执行相同的操作,但是将来不同级别(最多35个)的循环代码将会更改,但仍需要流动。

我不希望在循环中有35个 嵌套 ,我正在寻找一种方法来保持每个级别(循环)的连续流量作为选项每个级别的变化

 while True:  # loop because it because the Soldier is animated and displayed at the moment.
        soldier = pyautogui.locateCenterOnScreen(imPath('soldier_1.png'), region=GAME_REGION, confidence=.5,
                                                 grayscale=True)
        continueButton = pyautogui.locateCenterOnScreen(imPath('continue_button.png'), region=GAME_REGION,
                                                            confidence=.6, grayscale=True)

        if soldier != None:
            #soldierCount.append(soldier)
            x, y = soldier
            x, y = int(x), int(y)
            aim_mouse.thompsonAimer.handle_click(x, y)
        else:
            # Move Mouse to the Centre Screen
            centreScreen = int(screenWidth / 2 - 130), int(screenHeight / 2)
            x, y = centreScreen
            ctypes.windll.user32.SetCursorPos(x, y)
            #pass

            # Press the Continue Button If Avaliable
            if continueButton == None:
                continue
            else:
                pyautogui.click(continueButton, duration=0.25)
                logging.info('Clicked on Continue button.')


            """Level 2"""
            # Function for First and Second Level
            print(8 * "-", "Level 2", 8 * "-")

            while True:  # loop because it because the Soldier is animated and displayed at the moment.
                soldier = pyautogui.locateCenterOnScreen(imPath('soldier_1.png'), region=GAME_REGION, confidence=.5,
                                                             grayscale=True)
                continueButton = pyautogui.locateCenterOnScreen(imPath('continue_button.png'), region=GAME_REGION,
                                                        confidence=.6, grayscale=True)

                if soldier != None:
                    print("Soldier Located at:", soldier)
                    x, y = soldier
                    x, y = int(x), int(y)
                    aim_mouse.thompsonAimer.handle_click(x, y)
                else:
                    # Move Mouse to the Centre Screen
                    centreScreen = int(screenWidth / 2 - 130), int(screenHeight / 2)
                    x, y = centreScreen
                    ctypes.windll.user32.SetCursorPos(x, y)

                    # Press the Continue Button If Avaliable
                    if continueButton == None:
                        continue
                    else:
                        pyautogui.click(continueButton, duration=0.25)
                        logging.info('Clicked on Continue button.')

有没有办法打破一段时间的True循环,然后立即再次重新开始?或者有没有更好的方法来编写这个也许是每个级别或类的多个缩进while循环函数?

或许只是一吨if if else语句的水平?

感谢您的时间。

0 个答案:

没有答案