PIL.ImageGrab 线程中的 grab() 和 load() 函数

PIL.ImageGrab , grab() and load() functions in thread

我想创建一个带线程的游戏机器人。

当脚本发现游戏屏幕工作 1 次并停止并显示此消息“进程已完成,退出代码为 0”时。但是scrpit找不到游戏画面,确实有效...

我该如何解决这个问题?非常感谢。

class POT(Thread):
    global x, y


    def __init__(self):
        Thread.__init__(self)
        self.running = True

    def run(self):
        while self.running:
            try:
                x, y = locateCenterOnScreen('menu.PNG')
                text = grab().load()[x - 670, y - 730]
                text2 = grab().load()[x - 757, y - 730]
                if (text[0] < 150):
                    for z in range(3):
                        press('0')
                        sleep(0.05)
                        release('0')
                        sleep(0.05)
                    print('HP USED')

                    if text2[0] < 150:
                        for z in range(3):
                            press('6')
                            sleep(0.025)
                            release('6')
                            sleep(0.025)
                            press('5')
                            sleep(0.033)
                            release('5')
                            sleep(0.033)
                        print('INTUITION USED 6')
                        print('AURA USED 5')
                print(x, y)
                return x, y
            except:
                print("GAME SCREEN NOT FOUND...")



bot=POT()
bot.start()
bot.join()

你正在做 return x, y

这将打破 while self.running 循环,因此线程结束,您的程序也随之结束。

顺便说一句:

  • 你不需要线程,因为你的程序除了这个循环什么都不做。
  • 你永远不应该使用裸露的 try: except: 块。它还会捕获 SystemExits 和 KeyboardInterrupts。
    • 如果您使用 try: except Exception: 而不是 try: except:,您会想要使用 traceback.print_exc() 以便了解 actual 异常.