import cv2
import numpy as np
import pyautogui
import time
import keyboard
import random
lower_red1 = np.array([0, 100, 100])
upper_red1 = np.array([10, 255, 255])
lower_red2 = np.array([160, 100, 100])
upper_red2 = np.array([180, 255, 255])
def detect_red_circles(image):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask1 = cv2.inRange(hsv_image, lower_red1, upper_red1)
mask2 = cv2.inRange(hsv_image, lower_red2, upper_red2)
mask = cv2.bitwise_or(mask1, mask2)
# blurred = cv2.GaussianBlur(mask, (3, 3), 2)
circles = cv2.HoughCircles(
mask, cv2.HOUGH_GRADIENT, dp=1.2, minDist=40, param1=100, param2=30, minRadius=8, maxRadius=17
)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
return circles[0: min(len(circles), 1)]
return None
running = False
def toggle_running():
global running
running = not running
print(f"Running: {running}")
keyboard.add_hotkey('u', toggle_running)
try:
while True:
if running:
screenshot = pyautogui.screenshot()
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
height, width = frame.shape[:2]
left = width // 3
right = 2 * width // 3
up = height // 6
roi = frame[up:, left:right]
roi_height, roi_width = roi.shape[:2]
subregion_height = np.round(roi_height / 1.7).astype("int")
subregion_width = np.round(roi_width / 1.2).astype("int")
top_left_y = int(np.random.exponential(scale=roi_height / 6))
top_left_y = min(top_left_y, roi_height - subregion_height)
top_left_x = random.randint(0, roi_width - subregion_width)
subregion = roi[top_left_y:top_left_y + subregion_height, top_left_x:top_left_x + subregion_width]
circles = detect_red_circles(subregion)
if circles is not None:
for (x, y, r) in circles:
x += left + top_left_x
y += up + top_left_y
pyautogui.click(x, y)
# print("Shoot")
# Add a small delay to avoid high CPU usage
# time.sleep(0.00005)
except KeyboardInterrupt:
pass
cv2.destroyAllWindows()
星穹铁道bto小游戏自动射击程序Python
最新推荐文章于 2025-05-03 23:16:11 发布