星穹铁道bto小游戏自动射击程序Python

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()


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值