#๐ Automating a rhythm game (Repost 2)
25 messages ยท Page 1 of 1 (latest)
@storm wolf
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.
for referance this is the old post
import time
import mss
import numpy as np
import pyautogui
# Define the region to capture the specific area based on the given coordinates
region = {"top": 766, "left": 1503, "width": 59, "height": 65}
# Define the color to action mapping
color_to_action = {
(4, 0, 255): 'left_click', # Color to left mouse click
(255, 0, 157): 'right_click' # Color to right mouse click
}
# Initialize screen capture
sct = mss.mss()
def get_pixel_color(region):
# Capture the screen region
sct_img = sct.grab(region)
# Convert to a numpy array
img = np.array(sct_img)
# Get the color of the center pixel
center_pixel = img[region['height']//2, region['width']//2, :3]
return tuple(center_pixel)
while True:
try:
# Get the color of the pixel
color = get_pixel_color(region)
# Check if the color is in our mapping
if color in color_to_action:
# Perform the corresponding action
action = color_to_action[color]
if action == 'left_click':
pyautogui.click(button='left')
print(f"Detected color: {color}, Performed action: {action}")
elif action == 'right_click':
pyautogui.click(button='right')
print(f"Detected color: {color}, Performed action: {action}")
# Small delay to avoid overloading the CPU
time.sleep(0.01)
except KeyboardInterrupt:
break
import pyautogui
import time
import numpy as np
import cv2
# Define the coordinates and expected colors for 'E' and 'Q'
pixels_e = [
((1504, 900), (65, 65, 65)),
((1504, 910), (74, 74, 74)),
((1504, 922), (65, 65, 65)),
((1495, 910), (64, 64, 64)),
]
pixels_q = [
((1504, 904), (66, 66, 66)),
((1508, 924), (68, 68, 68)),
((1486, 911), (69, 69, 69)),
((1504, 900), (76, 76, 76)),
]
def match_pixels(pixels, screenshot):
for (x, y), color in pixels:
if not np.array_equal(screenshot[y, x], color):
return False
return True
def press_key(key):
pyautogui.press(key)
print(f"Pressed key: {key}")
def main():
time.sleep(2) # Delay to switch to the target application
while True:
# Capture the screen
screenshot = np.array(pyautogui.screenshot())
# Convert the screenshot to BGR format for OpenCV compatibility
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
# Check if 'E' is detected
if match_pixels(pixels_e, screenshot):
press_key('e')
# Check if 'Q' is detected
if match_pixels(pixels_q, screenshot):
press_key('q')
# Add a small delay to avoid excessive CPU usage
time.sleep(0.01)
if __name__ == "__main__":
main()
import pyautogui
import time
import numpy as np
import cv2
# Define the coordinates and expected colors for 'E' and 'Q'
pixels_e = [
((1504, 900), (65, 65, 65)),
((1504, 910), (74, 74, 74)),
((1504, 922), (65, 65, 65)),
((1495, 910), (64, 64, 64)),
]
pixels_q = [
((1504, 904), (66, 66, 66)),
((1508, 924), (68, 68, 68)),
((1486, 911), (69, 69, 69)),
((1504, 900), (76, 76, 76)),
]
def match_pixels(pixels, screenshot):
for (x, y), color in pixels:
if not np.array_equal(screenshot[y, x], color):
print(f"Pixel mismatch at ({x}, {y})")
print(f"Expected color: {color}, Actual color: {screenshot[y, x]}")
return False
return True
def press_key(key):
pyautogui.press(key)
print(f"Pressed key: {key}")
def main():
time.sleep(2) # Delay to switch to the target application
while True:
# Capture the screen
screenshot = np.array(pyautogui.screenshot())
# Convert the screenshot to BGR format for OpenCV compatibility
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
# Check if 'E' is detected
if match_pixels(pixels_e, screenshot):
press_key('e')
print("Detected 'E'")
# Check if 'Q' is detected
if match_pixels(pixels_q, screenshot):
press_key('q')
print("Detected 'Q'")
# Add a small delay to avoid excessive CPU usage
time.sleep(0.01)
if __name__ == "__main__":
main()
the colors don't seem consistent
so that strat is out the window
that's the point
its about finding a threshold for colors that allows detection
from dataclasses import dataclass
@dataclass
class Color:
red: int
green: int
blue: int
def is_similar_to(self, other: "Color", tolerance: int = 40) -> bool:
return (
abs(self.red - other.red) <= tolerance
and abs(self.green - other.green) <= tolerance
and abs(self.blue - other.blue) <= tolerance
)
something like that
could help
mhm so if the color is in the range of 40 it counts it as good?
yeah well you can modify the tolerance
its about finding the right threshold
my mistake
are you familiar with object oriented programming?
no?
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.