import cv2 import numpy as np import pyautogui import os import logging import time…
- User
import cv2 import numpy as np import pyautogui import os import logging import time
TEMPLATE_FOLDER = r'C:\Bot_Hearthstone\Skreen' TEMPLATES = []
Загрузка эталонных шаблонов
for template_name in os.listdir(TEMPLATE_FOLDER): if template_name.endswith('.bmp'): template = cv2.imread(os.path.join(TEMPLATE_FOLDER, template_name), 0) TEMPLATES.append(template)
def find_game_window(): windows = pyautogui.getWindowsWithTitle('Hearthstone') if len(windows) > 0: return windows[0]
def main(): while True: game_wnd = find_game_window() if game_wnd is None: continue
# Делаем скриншот окна x1, y1, x2, y2 = game_wnd.left, game_wnd.top, game_wnd.right, game_wnd.bottom screenshot = np.array(ImageGrab.grab(bbox=(x1, y1, x2, y2))) # Работаем с шаблонами screenshot_gray = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY) for template in TEMPLATES: res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED) loc = np.where(res >= 0.9) if len(loc[0]) > 0: # Кликаем по найденному шаблону x, y = pyautogui.center(loc) pyautogui.click(x, y) # Ждем исчезновения while True: if find_template(template, screenshot_gray) is None: breaklog_dir = r'C:\Bot_Hearthstone' if not os.path.exists(log_dir): os.makedirs(log_dir)
logging.basicConfig(filename=os.path.join(log_dir, 'Logging.log'), level=logging.INFO, format='%(asctime)s: %(message)s')
def move_mouse_smoothly(x, y, duration, comment): start_x, start_y = pyautogui.position() pyautogui.moveTo(x, y, duration=duration) pyautogui.click() logging.info(f"{comment}")
def find_and_interact_with_image(image_path, region, sleep_duration, pre_click_coords=None, optional=False, duration=1): image_found = False while not image_found: image_location = pyautogui.locateOnScreen(image_path, region=region, confidence=0.6) if image_location is not None: if pre_click_coords: for coords in pre_click_coords: move_mouse_smoothly(coords[0], coords[1], duration, f"Нажал на координатах {coords}") else: image_center = pyautogui.center(image_location) move_mouse_smoothly(image_center[0], image_center[1], duration, f"Нажал на картинку {os.path.basename(image_path)}") logging.info(f"Картинка {os.path.basename(image_path)} найдена и нажата") image_found = True # Проверяем исчезновение изображения после нажатия while True: image_location_after_click = pyautogui.locateOnScreen(image_path, region=region, confidence=0.6) if image_location_after_click is None: logging.info(f"Картинка {os.path.basename(image_path)} исчезла") break else: logging.info(f"Картинка {os.path.basename(image_path)} не исчезла, нажимаем еще раз") if pre_click_coords: for coords in pre_click_coords: move_mouse_smoothly(coords[0], coords[1], duration, f"Нажал на координатах {coords}") else: image_center = pyautogui.center(image_location_after_click) move_mouse_smoothly(image_center[0], image_center[1], duration, f"Нажал на картинку {os.path.basename(image_path)}") time.sleep(1) # Даем немного времени для реакции на нажатие elif optional: break logging.info(f"Ждем картинку {os.path.basename(image_path)}") time.sleep(sleep_duration) time.sleep(1)
- Assistant
It looks like you're trying to automate a series of actions for the game "Hearthstone" using Python libraries such as , (OpenCV), and . The script you shared is supposed to find specific images on-screen (templates) and interact with them by moving the mouse and clicking.