Rt � � � 2023-05-29 16:59 采纳率: 25%
浏览 57
已结题

pyautogui.写法改进,需要修改.


import pyautogui
import keyboard

def fixed_loop():
    print(' 循环选择待命')
    while True:

        if keyboard.is_pressed('4'):
            Royal = ['Royal.png', 'Royal1.png', 'Royal2.png', 'Royal3.png', 'Royal4.png', 'Royal5.png', 'Royal6.png']
            x = 0
            num = 0

            for i in Royal:
                pos = pyautogui.locateCenterOnScreen(Royal[num], grayscale=False, confidence=0.6, region=(50, 55, 310, 230))
                num += 1

                if pos is not None:
                    x = x + 1
            print(x)
            if x >= 1:
                cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=False, confidence=0.6, region=(50, 55, 310, 230))

                if cloak_image is None:
                    pyautogui.sleep(0.3)
                    print(' 有披风有红衣')
                    pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                    pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                    pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
                    pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                    pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                    print(' 固定起手结束')
                    break

                if cloak_image is not None:
                    pyautogui.sleep(0.3)
                    print(' 无披风有红衣')
                    pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                    pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                    pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                    pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                    print(' 固定起手结束')
                    break

            if x == 0:
                cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=False, confidence=0.6, region=(50, 55, 310, 230))

                if cloak_image is not None:
                    pyautogui.sleep(0.3)
                    print(' 无披风无红衣')
                    pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                    pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
                    pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                    pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                    pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                    print(' 固定起手结束')
                    break

                if cloak_image is None:
                    pyautogui.sleep(0.3)
                    print(' 有披风无红衣')
                    pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                    pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
                    pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                    pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
                    pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                    pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                    print(' 固定起手结束')
                    break

        if keyboard.is_pressed('5'):
            print(' 进入智能循环')
            break

        else:
            pyautogui.sleep(0.1)

if __name__ == '__main__':
    fixed_loop()


def smart_loop():

    while True:
        FrostD = ['D.png', 'D1.png', 'D2.png', 'D3.png']
        FrostF = ['F.png', 'F1.png', 'F2.png', 'F3.png']
        FrostR = ['R.png', 'R1.png', 'R2.png', 'R3.png']
        d = 0
        f = 0
        r = 0
        num = 0

        for i in FrostR:
            pos = pyautogui.locateCenterOnScreen(FrostR[num], grayscale=False, confidence=0.8, region=(880, 1000, 80, 80))
            num += 1
            if pos is not None:
                r = r + 1
        num = 0

        for i in FrostD:
            pos = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=False, confidence=0.8, region=(970, 1000, 80, 80))
            num += 1
            if pos is not None:
                d = d + 1
        num = 0

        for i in FrostF:
            pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=False, confidence=0.8, region=(1050, 1000, 80, 80))
            num += 1
            if pos is not None:
                f = f + 1

        print(d, f, r)

        if d >= 1:
            pyautogui.sleep(0.4)
            pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)

        if f >= 1:
            pyautogui.sleep(0.4)
            pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
            pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)

        if r >= 1:
            pyautogui.sleep(0.4)
            pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)

        else:
            pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.01)


        if keyboard.is_pressed('3'):
            print(" 智能循环结束")
            fixed_loop()

        if keyboard.is_pressed('ctrl'):
            print(' 暂停智能循环')
            pyautogui.sleep(1)

            while True:
                if keyboard.is_pressed('ctrl'):
                    pyautogui.sleep(0.3)

                else:
                    print(' 继续智能循环')
                    smart_loop()

if __name__ == '__main__':
    smart_loop()

百度自学能力有限,需求就是上面的功能,简化高效的写法有没有.可加100

  • 写回答

5条回答 默认 最新

  • LNX_EN 2023-05-29 17:34
    关注

    将D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))修改为D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))

    将pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))修改为pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))

    
    import pyautogui
    import keyboard
    
    # 固定循环函数
    def fixed_loop():
        print('循环选择待命')
        while True:
            # 检测键盘按键
            if keyboard.is_pressed('4'):
                Royal = ['Royal.png', 'Royal1.png', 'Royal2.png', 'Royal3.png', 'Royal4.png', 'Royal5.png', 'Royal6.png']
                x = 0
                y = 0
                num = 0
    
                # 在Royal列表中逐个查找图片位置
                for i in Royal:
                    pos = pyautogui.locateCenterOnScreen(Royal[num], grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
                    num += 1
    
                    if pos is None:
                        y = y + 1
    
                    if pos is not None:
                        x = x + 1
                print(x, y)
                if x >= 1 and y <= 6:
                    cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
    
                    if cloak_image is None:
                        pyautogui.sleep(0.3)
                        print('有披风有红衣')
                        pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                        pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                        pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
                        pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                        pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                        print('固定起手结束')
                        break
    
                    if cloak_image is not None:
                        pyautogui.sleep(0.3)
                        print('无披风有红衣')
                        pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                        pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                        pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                        pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                        print('固定起手结束')
                        break
    
                if x == 0 and y == 7:
                    cloak_image = pyautogui.locateCenterOnScreen('Cloak.png', grayscale=True, confidence=0.6, region=(50, 55, 310, 230))
    
                    if cloak_image is not None:
                        pyautogui.sleep(0.3)
                        print('无披风无红衣')
                        pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                        pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
                        pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                        pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                        pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                        print('固定起手结束')
                        break
    
                    if cloak_image is None:
                        pyautogui.sleep(0.3)
                        print('有披风无红衣')
                        pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                        pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
                        pyautogui.typewrite(['w', 'w', 'w', 'w'], 0.1)
                        pyautogui.typewrite(['e', 'e', 'e', 'e'], 0.1)
                        pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                        pyautogui.typewrite(['q', 'q', 'q', 'q'], 0.1)
                        print('固定起手结束')
                        break
    
            if keyboard.is_pressed('5'):
                print('进入智能循环')
                break
    
            else:
                pyautogui.sleep(0.1)
    
    if __name__ == '__main__':
        fixed_loop()
    
    
    # 智能循环函数
    def smart_loop():
        while True:
            FrostD = ['D.png', 'D1.png', 'D2.png', 'D3.png']
            FrostF = ['F.png', 'F1.png', 'F2.png', 'F3.png']
            FrostR = ['R.png', 'R1.png', 'R2.png', 'R3.png']
            d = 0
            dd = 0
            f = 0
            ff = 0
            r = 0
            rr = 0
            num = 0
    
            # 在FrostD列表中逐个查找图片位置
            for i in FrostD:
                D_use = pyautogui.locateCenterOnScreen(FrostD[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
                num += 1
                if D_use is None:
                    d = d + 1
                if D_use is not None:
                    dd = dd + 1
    
            # 在FrostF列表中逐个查找图片位置
            for i in FrostF:
                pos = pyautogui.locateCenterOnScreen(FrostF[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
                num += 1
                if pos is None:
                    f = f + 1
                if pos is not None:
                    ff = ff + 1
    
            # 在FrostR列表中逐个查找图片位置
            for i in FrostR:
                pos = pyautogui.locateCenterOnScreen(FrostR[num], grayscale=True, confidence=0.9, region=(970, 1000, 80, 80))
                num += 1
                if pos is None:
                    r = r + 1
                if pos is not None:
                    rr = rr + 1
    
                print(d, dd, f, ff, r, rr)
    
            if d >= 4 and dd <= 0:
                pyautogui.sleep(0.4)
                pyautogui.typewrite(['d', 'd', 'd', 'd'], 0.1)
                print('发现D技能,准备按下')
    
            if f >= 4 and ff <= 0:
                pyautogui.sleep(0.4)
                pyautogui.typewrite(['f', 'f', 'f', 'f'], 0.1)
                print('发现F技能,准备按下')
    
            if r >= 4 and rr <= 0:
                pyautogui.sleep(0.4)
                pyautogui.typewrite(['r', 'r', 'r', 'r'], 0.1)
                print('发现R技能,准备按下')
    
            pyautogui.sleep(0.1)
    
    
    if __name__ == '__main__':
        smart_loop()
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 修改了问题 5月29日
  • 修改了问题 5月29日
  • 修改了问题 5月29日
  • 展开全部