[Python] 纯文本查看 复制代码
import time
import win32gui, win32ui, win32con
import ctypes
import pyautogui
import numpy as np
import copy
class GameAutoGui:
def __init__(self, className):
print('欢迎使用游戏自动化类库')
self.hwnd = self.FindWin(className)
win32gui.SetForegroundWindow(self.hwnd)
# self.l, self.t, self.r, self.b = self.get_window_rect(self.hwnd)
self.setWinPos(self.hwnd, 0, 0)
# print(self.hwnd, self.l, self.t, self.r, self.b)
def FindWin(self, des):
return win32gui.FindWindow(des, None)
def get_appoint_hwnd(self, hwnd: str, num: int):
'''
获取所有指定句柄
:param hwnd: 类名或者窗口名字
:param num: 0对应类名,1对应窗口名字
:return: 数组
'''
hwnd_title = dict()
li = []
def get_all_hwnd(hwnd, mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
if num:
hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
else:
hwnd_title.update({hwnd: win32gui.GetClassName(hwnd)})
win32gui.EnumWindows(get_all_hwnd, 0)
for h, t in hwnd_title.items():
if str(t) == hwnd:
# print(h, t)
li.append(h)
return li
def get_window_rect(self, hwnd):
'''
获取窗口坐标
:param hwnd: 句柄所在指针
:return: 元组(rect.left, rect.top, rect.right, rect.bottom)
'''
import ctypes.wintypes
try:
f = ctypes.windll.dwmapi.DwmGetWindowAttribute
except WindowsError:
f = None
if f:
rect = ctypes.wintypes.RECT()
DWMWA_EXTENDED_FRAME_BOUNDS = 9
f(ctypes.wintypes.HWND(hwnd),
ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect),
ctypes.sizeof(rect)
)
return rect.left, rect.top, rect.right, rect.bottom
def click(self, x, y):
x = int(x)
y = int(y)
ctypes.windll.user32.SetCursorPos(x, y)
ctypes.windll.user32.mouse_event(2, 0, 0, 0, 0)
time.sleep(0.1)
ctypes.windll.user32.mouse_event(4, 0, 0, 0, 0)
def setWinPos(self, hwnd, x, y):
'''
设置窗口位置
:param hwnd: 句柄
:param x: X坐标
:param y: Y坐标
:return:
'''
return ctypes.windll.user32.SetWindowPos(hwnd, 0, x, y, 0, 0, 0X0001 | 0X0004)
# 截图
def getCapture(self, stax, stay, endx, endy):
w = endx - stax
h = endy - stay
# print(stax, stay, endx, endy,w,h)
im = pyautogui.screenshot(region=(stax, stay, w, h))
im.save("123.bmp")
# 定点比色
def cmpcolor(self, x, y, color):
# print(x, y)
color = self.Hex_to_RGB2(color)
im = pyautogui.screenshot(region=(0, 0, x + 10, y + 10))
res = im.getpixel((x, y))
# print(res)
if res[0] == color[0] and res[1] == color[1] and res[2] == color[2]:
return True
else:
return False
def RGB_to_Hex(self, rgb):
RGB = rgb.split(',') # 将RGB格式划分开来
color = '#'
for i in RGB:
num = int(i)
# 将R、G、B分别转化为16进制拼接转换并大写 hex() 函数用于将10进制整数转换成16进制,以字符串形式表示
color += str(hex(num))[-2:].replace('x', '0').upper()
print(color)
return color
def Hex_to_RGB(self, hex):
r = int(hex[1:3], 16)
g = int(hex[3:5], 16)
b = int(hex[5:7], 16)
return b, g, r
def Hex_to_RGB2(self, hex):
r = int(hex[1:3], 16)
g = int(hex[3:5], 16)
b = int(hex[5:7], 16)
return r, g, b
# 获取颜色数量
def getcolornum(self, stax: int, stay: int, endx: int, endy: int, des):
w = endx - stax
h = endy - stay
im = pyautogui.screenshot(region=(stax, stay, w, h))
im = np.array(im)
# print(im)
des = list(self.Hex_to_RGB2(des))
# print(des)
result = np.count_nonzero(np.all(im == des, axis=2))
print(result)
return result
# 找色
def FindColor(self, stax: int, stay: int, endx: int, endy: int, des):
w = endx - stax
h = endy - stay
im = pyautogui.screenshot(region=(stax, stay, w, h))
im = np.array(im)
des = list(self.Hex_to_RGB2(des))
result = np.argwhere(np.all(im == des, axis=2)).tolist()
print(result)
for i in result:
a = i[0]
i[0] = stax + i[1]
i[1] = stay + a
# print(result)
return result
# 多点找色
def FindFindColor(self, stax: int, stay: int, endx: int, endy: int, des):
w = endx - stax
h = endy - stay
im = pyautogui.screenshot(region=(stax, stay, w, h))
im = np.array(im)
global headCoordinate, matched, corrdinate
corrdinate = np.empty([0, 2])
for i in des:
if type(i) == str:
# print('我是头部', i)
headColor = self.Hex_to_RGB2(i)
# print('头部颜色', headColor)
headCoordinate = np.argwhere(np.all(im == headColor, axis=2))
# print('头部坐标', headCoordinate)
else:
# print('我是身体', i)
bodyColor = self.Hex_to_RGB2(i[2])
# print('身体颜色', bodyColor)
bodyCoordinate = np.argwhere(np.all(im == bodyColor, axis=2))
# print('身体坐标', bodyCoordinate)
headBodyCoordinate = copy.deepcopy(headCoordinate)
for index in headBodyCoordinate:
index[0] = int(index[0]) + i[1]
index[1] = int(index[1]) + i[0]
# print('头部坐标', headCoordinate)
# print('头部偏移后坐标', headBodyCoordinate)
q = set([tuple(t) for t in bodyCoordinate])
w = set([tuple(t) for t in headBodyCoordinate])
matched = np.array(list(q.intersection(w)))
# print('相同坐标', matched)
beforeMatched = copy.deepcopy(matched)
for index in beforeMatched:
index[0] = int(index[0]) - i[1]
index[1] = int(index[1]) - i[0]
# print('相同坐标的原坐标', beforeMatched)
corrdinate = np.append(corrdinate, beforeMatched, axis=0)
# print('中间结果', corrdinate)
# print('结果', corrdinate)
corrdinate = corrdinate.tolist()
for i in corrdinate:
# print(corrdinate.count(i))
if corrdinate.count(i) == len(des) - 1:
return i[1] + stax, i[0] + stay
return -1,-1
# a = GameAutoGui('LDPlayerMainFrame')
# b = a.FindFindColor(20,361,117,467,
# ['#a3906f', [29, 35, '#22a0e5'], [35, 41, '#d82c29'], [25, 3, '#887b65'], [14, 6, '#3c3734']])
# print(b)