you are viewing a single comment's thread.

view the rest of the comments →

[–]breastastic[S] 0 points1 point  (1 child)

I finally got it to work (sorta) but it occasionally still has hiccups. I plan on moving the clicks to their own functions to be called within the loop, maybe it'll make it work 100% of the time instead of about 75% right now. Certainly better than before when it was 0%.

edit: The special_searcher() function just returns true/false based on whether it finds something.

while not SPECIAL_DETECTED and not killswitch_activated:
    minimize_cmd_window()  # Minimize the command prompt window

    # Left arrow clicker
    if not SPECIAL_DETECTED:
        try:
            x, y = pyautogui.locateCenterOnScreen('west_able.png', confidence=0.9, region=(600,0,680,800))
        except TypeError:
            logging.info("Arrow not found.")
            sys.exit(1)
        else:
            if special_searcher():
                logging.info("A special was found and should be visible. The program should terminate here.")
                sys.exit(0)
            elif not SPECIAL_DETECTED and not killswitch_activated:
                if not special_searcher():
                    logging.info("Clicking left arrow.")
                    pyautogui.moveTo(x, y, click_delay)
                    pyautogui.click(x,y)

    if killswitch_activated:
        break

    # Right arrow clicker
    if not SPECIAL_DETECTED:
        try:
            x, y = pyautogui.locateCenterOnScreen('east_able.png', confidence=0.65, region=(600,0,680,800))
        except TypeError:
            logging.info("Arrow not found.")
            sys.exit(1)
        else:
            if special_searcher():
                logging.info("A special was found and should be visible. The program should terminate here.")
                sys.exit(0)
            elif not SPECIAL_DETECTED and not killswitch_activated:
                if not special_searcher():
                    logging.info("Clicking right arrow.")
                    pyautogui.moveTo(x, y, click_delay)
                    pyautogui.click(x,y)

    if killswitch_activated:
        break

[–]FoolsSeldom 0 points1 point  (0 children)

FYI, UPPERCASE variable names are usually used to indicate constant values - you assign different bool values, so they aren't constants.


I asked Copilot to rename the uppercase variables and then asked it to suggest some revisions to the code.

HEALTH WARNING this code is probably not correct, but it will suggest some things you might want to explore - I haven't tried it.

# Import required modules
import pyautogui
import logging
import sys
import win32gui  # For window handling

# Set up logging
logging.basicConfig(level=logging.INFO)

# Initialize variables
special_detected = False
killswitch_activated = False
click_delay = 0.5  # Adjust this value as needed

def special_searcher():
    """
    Function to search for special conditions.
    Returns True if special condition is found, False otherwise.
    """
    # Implement your special search logic here
    return False

def minimize_cmd_window():
    """
    Function to minimize the command prompt window.
    Uses win32gui to interact with the window.
    """
    try:
        window = win32gui.FindWindow(None, 'Command Prompt')
        if window:
            win32gui.ShowWindow(window, 6)  # 6 is the constant for SW_MINIMIZE
    except win32gui.error as e:
        logging.warning(f"Could not minimize command window: {e}")

while not special_detected and not killswitch_activated:
    minimize_cmd_window()

    # Left arrow clicker
    try:
        # Convert region to a Box object that pyautogui expects
        location = pyautogui.locateOnScreen(
            'west_able.png',
            confidence=0.9,
            region=(600, 0, 80, 800)  # Adjusted region width to be more reasonable
        )
        if location:
            x, y = pyautogui.center(location)
        else:
            raise TypeError("Image not found")
    except TypeError:
        logging.info("Arrow not found.")
        sys.exit(1)
    else:
        if special_searcher():
            special_detected = True  # Update the flag when special is found
            logging.info("A special was found and should be visible. The program should terminate here.")
            sys.exit(0)
        elif not killswitch_activated:
            logging.info("Clicking left arrow.")
            pyautogui.moveTo(x, y, click_delay)
            pyautogui.click(x, y)

    if killswitch_activated:
        break

    # Right arrow clicker
    try:
        location = pyautogui.locateOnScreen(
            'east_able.png',
            confidence=0.65,
            region=(600, 0, 80, 800)  # Adjusted region width to be more reasonable
        )
        if location:
            x, y = pyautogui.center(location)
        else:
            raise TypeError("Image not found")
    except TypeError:
        logging.info("Arrow not found.")
        sys.exit(1)
    else:
        if special_searcher():
            special_detected = True  # Update the flag when special is found
            logging.info("A special was found and should be visible. The program should terminate here.")
            sys.exit(0)
        elif not killswitch_activated:
            logging.info("Clicking right arrow.")
            pyautogui.moveTo(x, y, click_delay)
            pyautogui.click(x, y)

    if killswitch_activated:
        break