每当一个数字(健康值)减少时,执行一个代码



所以基本上我正在编写一个python脚本来读取游戏中的生命值。我使用以下库:

import numpy as np
import pytesseract
import cv2
from PIL import ImageGrab

我已经成功地读取了实时屏幕上的健康值,并在python中将其更改为int。然而,当我在游戏中的生命值下降时,或者当我的生命值下降时,我仍然想不出如何执行代码的想法。

conf = r'--oem 3 --psm 6 outputbase digits'
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
def screenToData():
while (True):
screen = np.array(ImageGrab.grab(bbox=(350, 150, 550, 300)))
cv2.imshow("window", cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
data = pytesseract.image_to_data(cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY), lang='eng', config=conf)
# print(data)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
for x, box in enumerate(data.splitlines()):
if x != 0:
box = box.split()
# print(box)
if len(box) == 12:
if int(box[11]) == 100:
print("Full health")
elif int(box[11]) <= 100:
print("Nope")

screenToData()

第一步,在你的代码中你应该有一个叫做health的变量,这使得代码更容易读。

要确定你的生命值何时下降,每次循环时你只需要将当前生命值与上次读数进行比较,所以可以添加一个变量new_health来比较生命值。

health = 100
while True:
new_health = read_health()
if new_health < health:
# execute code
health = new_health

相关内容

最新更新