admin管理员组

文章数量:1320587

I did an oopsie and made an infinite loop at the match case in the end:

import pyautogui
import cv2
import numpy as np
import time

def find_image(image_path, confidence=0.8):
    screenshot = pyautogui.screenshot()
    screenshot = np.array(screenshot)
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
    template = cv2.imread(image_path)
    result = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(result >= confidence)

    if loc[0].size > 0:
        # calculate the position (x, y) of the first match 
        return loc[1][0], loc[0][0]
    else:
        return None

def click_image(image_path, confidence=0.8, max_retries=5, interval=3):
    counter = 0  # Counter for Repitition

    while counter < max_retries:
        screenshot = pyautogui.screenshot()
        screenshot = np.array(screenshot)
        screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
        position = find_image(image_path, confidence)

        if position:
            # Klicke auf die gefundene Position
            print(f" Image found at {position}, click...")
            pyautogui.click(position)
            counter += 1
            print(f"Repitition: {counter}/{max_retries}")
            time.sleep(interval)  # Warte vor der nächsten Suche
            return True
        else:
            print(f"Image {image_path} not found. Try in {interval} second again...")
            time.sleep(interval)  # Warte, bevor du es erneut versuchst

    print("5 Repititions completed, restarting search...")
    counter = 0  # Reset Counter and Repeat

Stamina = r'C:\Users\blabla\DokkanAutocklicker\Stamina.png'
Start = r'C:\Users\blabla\DokkanAutocklicker\StartStage.png'
Ok = r'C:\Users\blabla\DokkanAutocklicker\Ok.png'
Next = r'C:\Users\blabla\DokkanAutocklicker\GoToNextLevel.png'

image_detect = [Stamina, Start, Ok, Next]

while True:
    print("Searching for Image ...")
    match click_image:
        case click_image if click_image in image_detect:
            print(f"Image found...")
            click_image(Stamina, confidence=0.8, max_retries=5)
        case _:
            print(f"No Image found...")

My Goal: Create an autoclicker which clicks on the matching images of an array of 4 different images. The first 3 images are always the same. The 4th image has a chance of being either the 4th image or being the 3rd image again.

If it's the 4th image: loop back immediately.

If it's the 3rd image: click image, 4th image appears, click it then loop back.

I tried a bunch of things and I can't roll back since the infinite loop.

本文标签: pythonAccidentally created a infinite loop while trying Match CaseStack Overflow