我试图让我的宏程序一次执行多个功能。我设置它的方式是我定义函数,然后在大多数函数上调用一个线程,并让一些函数仍在主 While 循环上运行。
好的,所以问题是:当主 While 循环函数正在运行时,我打开的不同线程将不会执行,直到主 While 循环函数正在执行。
我需要的是一种使其成为可能的方法,以便所有函数同时执行,无论它是否等待主要的 While 循环函数完成。
这是我的代码:谢谢
import cv2 as cv
import numpy as np
import pyautogui
import time
from threading import Thread
healHk = 'F2'
healHk2 = 'F5'
manaHk = 'F3'
hasteHk = 'F7'
doHeal = 'yes'
doMana = 'yes'
doHaste = 'yes'
spellHk1 = 'F1'
spellHk2 = 'F5'
doMove = True
didMove = True
doFollow = False
time.sleep(5)
def healer():
manaPixel = (83, 80, 218)
healthPixel = (241, 97, 97)
hastePixel = (249, 249, 248)
im = pyautogui.screenshot()
monsterPixel = (187, 214, 40)
monsterLoc = (10, 49)
matchMonster = im.getpixel(monsterLoc)
matchMana = im.getpixel((1175, 160))
matchHp = im.getpixel((1211, 148))
matchHp2 = im.getpixel((1175, 148))
matchHaste = im.getpixel((597, 63))
global doMove
if matchHp2 != healthPixel and doHeal == 'yes':
time.sleep(.6)
pyautogui.press(healHk2)
time.sleep(.6)
pyautogui.press(healHk2)
time.sleep(.6)
else:
doMove = True
if matchMana != manaPixel and doMana == 'yes' and matchHp2 == healthPixel:
pyautogui.press(manaHk)
time.sleep(.6)
doMove = False
else:
doMove = True
if matchHp != healthPixel and matchHp2 == healthPixel and doHeal == 'yes':
time.sleep(.6)
pyautogui.press(healHk)
doMove = False
else:
doMove = True
if matchHaste != hastePixel and doHaste == 'yes' and matchMonster != monsterPixel:
time.sleep(.6)
pyautogui.press(hasteHk)
doMove = False
else:
doMove = True
def attacker():
greenFollow = (92, 255, 92)
monsterPixel = (69, 69, 69)
monsterLoc = (14, 50)
attackBorder = (255, 0, 0)
healthPixel = (241, 97, 97)
im = pyautogui.screenshot()
matchHp = im.getpixel((1206, 148))
matchFollow = im.getpixel((1262, 202))
matchMonster = im.getpixel(monsterLoc)
matchAttackRight = im.getpixel((23, 48))
matchHp2 = im.getpixel((1175, 148))
global doMove
if matchFollow != greenFollow and doFollow == True:
time.sleep(.6)
pyautogui.leftClick(1262, 204)
time.sleep(1)
pyautogui.moveTo(1090, 204, duration=.3)
if matchMonster != monsterPixel and matchAttackRight != attackBorder:
pyautogui.press('space')
time.sleep(.6)
doMove = False
else:
doMove = True
if matchAttackRight == attackBorder:
doMove = False
if matchHp2 == healthPixel:
time.sleep(.6)
pyautogui.press(spellHk1)
else:
doMove = True
sleepTime = 5.5
def charMove(delay, imgSrc):
monsterPixel = (187, 214, 40)
monsterLoc = (10, 49)
attackBorder = (255, 0, 0)
screenshot = pyautogui.screenshot()
healthPixel = (241, 97, 97)
matchHp = screenshot.getpixel((1211, 148))
matchMonster = screenshot.getpixel(monsterLoc)
matchAttackRight = screenshot.getpixel((23, 48))
cb1 = cv.imread(imgSrc, cv.IMREAD_UNCHANGED)
w, h = cb1.shape[: -1]
screenshot = np.array(screenshot)
screenshot = screenshot[:, :, ::-1].copy()
resultLife = cv.matchTemplate(cb1, screenshot, cv.TM_CCOEFF_NORMED)
threshold = 0.80
locations = np.where(resultLife >= threshold)
locations = list(zip(*locations[::-1]))
for pt in locations:
cv.rectangle(cb1, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
global didMove
if locations and matchHp == healthPixel and matchMonster != monsterPixel and matchAttackRight != attackBorder and doMove == True:
didMove = True
print('moving')
time.sleep(.3)
pyautogui.moveTo(pt[0] + w/2, pt[1] + h/2, duration=0.1)
pyautogui.leftClick()
pyautogui.moveTo(1090, 204, duration=.2)
pyautogui.press('F10')
time.sleep(delay)
else:
didMove = False
print('dontmove')
def doMove2():
screenshot = pyautogui.screenshot()
monsterPixel = (187, 214, 40)
monsterLoc = (10, 49)
attackBorder = (255, 0, 0)
matchMonster = screenshot.getpixel(monsterLoc)
matchAttackRight = screenshot.getpixel((23, 48))
if matchMonster == monsterPixel or matchAttackRight == attackBorder:
global doMove
doMove = False
else:
doMove = True
while True:
x = Thread(target=healer)
x.start()
y = Thread(target=attacker)
y.start()
z = Thread(target=doMove2)
z.start()
moveTime = 5
charMove(moveTime, 'cb1.jpg')
charMove(moveTime, 'cb2.jpg')
if cv.waitKey(1) == ord('q'):
cv.destroyAllWindows()
break
print('Done.')
请注意 charMove 函数,它有一个 time.sleep,并且线程不会评估,直到 charMove 函数结束,这是我的问题.. 任何解决方案?