<>前言

* 利用目标检测算法实现对游戏界面目标位置的获取
* PyKeyboard、ctypes实现鼠标键盘的控制
* 实现特定操作
<>一、目标检测算法

吧游戏界面截图下来,对特定目标进行Labelimg标注,训练一个目标检测算法模型

<>二、操作游戏流程

<>1.获取显示屏界面图像
def grab_screen(region=None): hwin = win32gui.GetDesktopWindow() if region:
left, top, x2, y2 = region width = x2 - left + 1 height = y2 - top + 1 else:
width= win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN) height = win32api.
GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN) left = win32api.GetSystemMetrics(
win32con.SM_XVIRTUALSCREEN) top = win32api.GetSystemMetrics(win32con.
SM_YVIRTUALSCREEN) hwindc = win32gui.GetWindowDC(hwin) srcdc = win32ui.
CreateDCFromHandle(hwindc) memdc = srcdc.CreateCompatibleDC() bmp = win32ui.
CreateBitmap() bmp.CreateCompatibleBitmap(srcdc, width, height) memdc.
SelectObject(bmp) memdc.BitBlt((0, 0), (width, height), srcdc, (left, top),
win32con.SRCCOPY) signedIntsArray = bmp.GetBitmapBits(True) img = np.fromstring(
signedIntsArray, dtype='uint8') img.shape = (height, width, 4) srcdc.DeleteDC()
memdc.DeleteDC() win32gui.ReleaseDC(hwin, hwindc) win32gui.DeleteObject(bmp.
GetHandle()) return cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
<>2.获取游戏窗口界面图像
def get_screan(): # hwnd = win32gui.FindWindow(None,
'C:\Windows\system32\cmd.exe') app = QApplication(sys.argv) hwnd = win32gui.
FindWindow(None, 'window') # window 窗口名称 # win32gui.SetForegroundWindow(hwnd)
rect= win32gui.GetWindowRect(hwnd) # print('aaaaaaa', hwnd) screen =
QApplication.primaryScreen() img = screen.grabWindow(hwnd).toImage() # img.save(
"screenshot.jpg") arr = convertQImageToMat(img) return cv2.cvtColor(arr, cv2.
COLOR_BGRA2BGR), rect
<>3.鼠标键盘操作

试了很多种方法,这种方法是能成功对游戏界面进行鼠标键盘操作的
def mouse_click(x, y, n=1): # x, y鼠标在窗口中的坐标 # win32api.SetCursorPos([x, y])
ctypes.windll.user32.SetCursorPos(int(x), int(y)) for i in range(n): # win32api.
mouse_event(win32con.MOUSEEVENTF_MOVE, x, y) win32api.mouse_event(win32con.
MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.
MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
<>三、验证码识别

数字字母验证码识别,准确率很好,但是需要32位的python来调用E语言的库
class Ver_code_2: def __init__(self, path): # 载入识别库 self.dll = windll.
LoadLibrary(path + r"\OCRS.dll") # 载入字库与建立字库索引 with open(os.path.join(path, r
"zimushuziku.cnn"), "rb") as file: # 载入字库 self.word_bank = file.read() # 建立字库索引
self.word_index = self.dll.INIT(path, self.word_bank, len(self.word_bank), -1, 1
) def ocr(self, image): Str = create_string_buffer(100) # 创建文本缓冲区 self.dll.OCR(
self.word_index, image, len(image), Str) # 利用DLL中的识别函数进行识别 # print(Str) return
Str.raw.decode("utf-8").rstrip('\x00') # 对识别的返回值进行编码后返回,这里的\x00是删除缓冲区的空白符
<>总结

识别依赖于目标检测算法的准确率,操作过程中的逻辑处理需严密

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信