You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1 KiB
Python

import logging
import os
import time
from threading import Thread
import numpy as np
import cv2
from airtest.core.cv import Template
import pyautogui
from pynput import mouse
from mouse.logitech import Logitech
from screen_shot import screen_shot_mss, show, save_image
logger = logging.getLogger("airtest")
logger.setLevel(logging.NOTSET)
ENEMY_X = 5
ENEMY_Y = 15
CHAMPION_H = 190
CHAMPION_W = 130
GLOBAL_POS_LIS = []
def is_some_color(s_array):
# 查找是否有30个像素是暗红色
# 检查颜色条件
condition = ((s_array[:, :, 2] > 50) & (s_array[:, :, 2] < 70) &
(s_array[:, :, 1] > 0) & (s_array[:, :, 1] < 20) &
(s_array[:, :, 0] > 0) & (s_array[:, :, 0] < 20))
# 计算满足条件的像素数量
num_matching_pixels = np.sum(condition)
# 判断是否有30个像素满足条件
return num_matching_pixels >= 30
left_button_pressed = False
def mouse_handle():
while True:
time.sleep(0.2)
if left_button_pressed:
now_x, now_y = pyautogui.position()
if GLOBAL_POS_LIS:
x, y = GLOBAL_POS_LIS[0]
move_x = x - now_x
move_y = y - now_y
print(move_x, move_y)
Logitech.mouse.move(move_x, move_y)
def listen():
def on_click(x, y, button, pressed):
global left_button_pressed
if button == mouse.Button.left:
left_button_pressed = pressed
listener = mouse.Listener(on_click=on_click)
listener.start()
if __name__ == '__main__':
Thread(target=mouse_handle).start()
Thread(target=listen).start()
base_t = Template("./base.png")
lower_gray = np.array([70, 70, 70])
upper_gray = np.array([120, 120, 120])
while True:
tmp_pos_lis = []
now_screen = screen_shot_mss(2200, 900, 180, 90)
mask = cv2.inRange(now_screen, lower_gray, upper_gray)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if w > 20 and h < 5:
s = now_screen[y - 30:y + 30, x - 30:x + 30]
if s.any():
cv2.rectangle(now_screen, (x - 30, y - 30), (x + 30, y + 30), (0, 255, 0), thickness=2)
pos = base_t.match_in(s)
if pos:
if is_some_color(s):
# cv2.rectangle(now_screen, (x, y), (x + 80, y + 150), (0, 255, 0), thickness=2)
# cv2.rectangle(now_screen, (x-30, y-30), (x+50, y+50), (0, 255, 0), thickness=2)
pos_x, pos_y = pos
# 计算pos在屏幕的 坐标
s_x = 180 + x - 30 + pos_x
s_y = 90 + y - 30 + pos_y
# 加上一些坐标得到人物的主体大概部位
p_x = s_x + 55
p_y = s_y + 115
tmp_pos_lis.append([p_x, p_y])
# show(now_screen)
GLOBAL_POS_LIS = tmp_pos_lis