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

2024-04-24 12:10:35 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,基本上我正在编写一个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()

Tags: importbox游戏dataifconfnpcv2
1条回答
网友
1楼 · 发布于 2024-04-24 12:10:35

第一步,在代码中应该有一个名为health的变量,这意味着代码更具可读性

要确定您的健康状况何时下降,每次循环时,您只需将当前健康状况与上次读数进行比较,因此可能需要添加一个变量new_health来与健康状况进行比较

health = 100

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

相关问题 更多 >