#๐Ÿ”’ My code is running but not working as intended

4 messages ยท Page 1 of 1 (latest)

dusky oakBOT
#

@shadow schooner

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @shadow schooner!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
shadow schooner
#
import cv2
import numpy as np
import pyscreenshot as ImageGrab
import keyboard
import pydirectinput
import time


# Function to check if a certain color is present in the image
def check_for_color(image, hex_color):
    # Convert hex color to BGR
    bgr_color = tuple(int(hex_color[i:i + 2], 16) for i in (1, 3, 5))
    np_color = np.array(bgr_color[::-1])

    # Check if color is present in the image
    if np.any(np.all(image == np_color, axis=-1)):
        return True
    else:
        return False


# Hex color code to monitor
hex_color_to_monitor = '#E99636'

# Flag to indicate whether monitoring is active
monitoring = False


def monitor_screen():
    global monitoring
    monitoring = True

    while monitoring:
        # Capture the screen
        screen = np.array(ImageGrab.grab())

        # Convert captured screen to BGR (OpenCV uses BGR by default)
        bgr_screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)

        # Check if the desired color is present
        if check_for_color(bgr_screen, hex_color_to_monitor):
            print("Color detected!")

            # Continuous monitoring until color is not present
            while check_for_color(bgr_screen, hex_color_to_monitor):
                screen = np.array(ImageGrab.grab())
                bgr_screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
                time.sleep(0.1)  # Optional: Add delay to reduce CPU usage

            # Simulate left click
            pydirectinput.click(button="left")
            print("Left click simulated.")

            # Stop monitoring after left click
            monitoring = False


# Start monitoring when 'l' key is pressed
keyboard.on_press_key('l', monitor_screen)

# Stop monitoring when 'l' key is released
keyboard.on_release_key('l', lambda event: setattr(monitoring, False))

# Keep the script running
keyboard.wait('esc')
dusky oakBOT
#

@shadow schooner

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.