自动进化单元Bot逻辑问题:检测mon图片执行evo(),否则执行scrol()
问题:自动进化游戏Bot的图片判断逻辑失效
我正在开发一款自动进化游戏单元的Bot,核心需求为:检测屏幕上是否存在名为mon.png的图片,若存在则执行evo()函数,若不存在则执行scrol()函数。
我尝试了两种代码逻辑,但均未正常生效:
- 逻辑1(while循环):先执行
scrol(),检测到mon则执行evo()并终止循环,但实际evo()从未执行
while True: scrol() if mon is not None: evo() break
- 逻辑2(if-else判断):判断
mon是否存在,存在则执行evo(),否则执行scrol(),但实际scrol()从未执行
if mon is not None: evo() else: scrol()
当前移除判断条件后代码可正常运行,但会在屏幕存在目标单元时仍执行滚动操作,导致错过进化机会。以下是完整代码:
import time import pyautogui import win32api import win32con import numpy as np time.sleep(3) def click(): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(np.random.uniform(0.1, 0.4)) def evolve(): evolve = pyautogui.locateCenterOnScreen('evolve.png', region=(0, 0, 1920, 1080), confidence=0.8) if evolve is not None: pyautogui.moveTo(evolve) time.sleep(np.random.uniform(0.1, 0.4)) click() time.sleep(np.random.uniform(0.1, 0.4)) def mon(): mon = pyautogui.locateCenterOnScreen('mon.png', region=(0, 0, 1920, 1080), confidence=0.9) if mon is not None: pyautogui.moveTo(mon) click() time.sleep(np.random.uniform(0.1, 0.4)) def autofill(): autofill = pyautogui.locateCenterOnScreen('autofill.png', region=(0, 0, 1920, 1080), confidence=0.7) if autofill is not None: pyautogui.moveTo(autofill) time.sleep(np.random.uniform(0.1, 0.4)) click() time.sleep(np.random.uniform(0.1, 0.4)) def scroll(): scroll = pyautogui.locateCenterOnScreen('scroll.png', region=(0, 0, 1920, 1080), confidence=0.7) if scroll is not None: pyautogui.moveTo(scroll) time.sleep(0.07) pyautogui.scroll(-4) time.sleep(np.random.uniform(0.1, 0.4)) def scroll2(): scroll2 = pyautogui.locateCenterOnScreen('scroll2.png', region=(0, 0, 1920, 1080), confidence=0.7) if scroll2 is not None: pyautogui.moveTo(scroll2) time.sleep(0.07) pyautogui.scroll(-4) time.sleep(np.random.uniform(0.1, 0.4)) def yes(): yes = pyautogui.locateCenterOnScreen('Yes.png', region=(0, 0, 1920, 1080), confidence=0.8) if yes is not None: pyautogui.moveTo(yes) click() time.sleep(np.random.uniform(0.1, 0.4)) def skip(): skip = pyautogui.locateCenterOnScreen('skip.png', region=(0, 0, 1920, 1080), confidence=0.8) if skip is not None: pyautogui.moveTo(skip) click() time.sleep(0.1) def ok(): ok = pyautogui.locateCenterOnScreen('ok.png', region=(0, 0, 1920, 1080), confidence=0.8) if ok is not None: pyautogui.moveTo(ok) click() time.sleep(np.random.uniform(0.1, 0.4)) def exitt(): exitt = pyautogui.locateCenterOnScreen('exit.png', region=(0, 0, 1920, 1080), confidence=0.7) if exitt is not None: pyautogui.moveTo(exitt) click() time.sleep(np.random.uniform(0.1, 0.4)) def storage(): storage = pyautogui.locateCenterOnScreen('storage.png', region=(0, 0, 1920, 1080), confidence=0.6) if storage is not None: pyautogui.moveTo(storage) click() time.sleep(np.random.uniform(0.1, 0.4)) def store(): store = pyautogui.locateCenterOnScreen('store.png', region=(0, 0, 1920, 1080), confidence=0.7) if store is not None: pyautogui.moveTo(store) click() time.sleep(np.random.uniform(0.1, 0.4)) def tick(): tick = pyautogui.locateCenterOnScreen('tick.png', region=(0, 0, 1920, 1080), confidence=0.9) if tick is not None: pyautogui.moveTo(tick) click() time.sleep(np.random.uniform(0.1, 0.4)) def putin(): putin = pyautogui.locateCenterOnScreen('putin.png', region=(0, 0, 1920, 1080), confidence=0.93) if putin is not None: pyautogui.moveTo(putin) click() time.sleep(np.random.uniform(0.1, 0.4)) def mon2(): mon2 = pyautogui.locateCenterOnScreen('mon2.png', region=(0, 0, 1920, 1080), confidence=0.7) if mon2 is not None: pyautogui.moveTo(mon2) for a in range(1): time.sleep(np.random.uniform(0.1, 0.12)) click() def putout(): putout = pyautogui.locateCenterOnScreen('putout.png', region=(0, 0, 1920, 1080), confidence=0.93) if putout is not None: pyautogui.moveTo(putout) click() time.sleep(np.random.uniform(0.1, 0.4)) def circle(): circle = pyautogui.locateCenterOnScreen('circle.png', region=(0, 0, 1920, 1080), confidence=0.7) if circle is not None: pyautogui.moveTo(circle) click() time.sleep(np.random.uniform(0.1, 0.4)) def powerup(): powerup = pyautogui.locateCenterOnScreen('powerup.png', region=(0, 0, 1920, 1080), confidence=0.7) if powerup is not None: pyautogui.moveTo(powerup) click() time.sleep(np.random.uniform(0.1, 0.4)) def evo(): exit1() circle() powerup() mon() autofill() evolve() yes() skip() ok() def sort(): exitt() storage() store() time.sleep(0.1) tick() time.sleep(0.1) putin() mon() mon2() putout() exitt() time.sleep(1) click() circle() powerup() def exit1(): exit1 = pyautogui.locateCenterOnScreen('exit1.png', region=(0, 0, 1920, 1080), confidence=0.7) if exit1 is not None: pyautogui.moveTo(exit1) click() time.sleep(np.random.uniform(0.1, 0.4)) def scrol(): scrol2 = pyautogui.locateCenterOnScreen('scrol2.png', region=(0, 0, 1920, 1080), confidence=0.7) if scrol2 is not None: scroll2() else: scroll() # while not keyboard.is_pressed('q'): for count in range(40): unexistent = False if not unexistent: evo() scrol() tess = pyautogui.locateCenterOnScreen('tess.png', region=(0, 0, 1920, 1080), confidence=0.7) if tess is not None: unexistent = True if unexistent: sort() unexistent = False
内容的提问来源于stack exchange,提问作者XpYcTaJIb
相关产品推荐
相关产品推荐

