MFC_Install_Create

1. 安装MFC

编写MFC窗口应用程序需要用到Visual Studiohttps://visualstudio.microsoft.com/zh-hans/,然后安装,要选择使用C++的桌面开发,再点击右边安装详细信息中的使用C++的桌面开发,往下滑,有一个适用于最新的v143生成工具的C++MFC(x86和x64)

2. 创建MFC

安装好后,点击Create a new project 

找到MFC,如果没有找到可以再Search for templates里输入MFC App

随便

import cv2 import numpy as np import win32gui import win32ui import win32con import time import pyautogui # 用于模拟鼠标点击 # 假设已经训练好YOLO模型,这里使用YOLOv8,需要安装ultralytics库 from ultralytics import YOLO # 加载自定义训练好的模型 model = YOLO("C:\\GAMG5\\yolov5_7.0-pyqt5\\runs\\train\\exp30\\weights\\best.pt") # 获取游戏窗口的句柄 hwnd = win32gui.FindWindow(None, "游戏窗口标题") if hwnd == 0: raise Exception("未找到游戏窗口") # 获取窗口位置大小 left, top, right, bottom = win32gui.GetWindowRect(hwnd) width = right - left height = bottom - top # 设置截图区域 region = (left, top, width, height) # 截图函数 def capture_screen(region=None): # 使用win32api截图 hwnd_dc = win32gui.GetWindowDC(hwnd) mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc) save_dc = mfc_dc.CreateCompatibleDC() save_bitmap = win32ui.CreateBitmap() save_bitmap.CreateCompatibleBitmap(mfc_dc, width, height) save_dc.SelectObject(save_bitmap) save_dc.BitBlt((0, 0), (width, height), mfc_dc, (0, 0), win32con.SRCCOPY) bmpinfo = save_bitmap.GetInfo() bmpstr = save_bitmap.GetBitmapBits(True) img = np.frombuffer(bmpstr, dtype='uint8').reshape((bmpinfo['bmHeight'], bmpinfo['bmWidth'], 4)) # 释放资源 win32gui.DeleteObject(save_bitmap.GetHandle()) save_dc.DeleteDC() mfc_dc.DeleteDC() win32gui.ReleaseDC(hwnd, hwnd_dc) # 转换为RGB,去掉alpha通道 img = img[:, :, :3] return img # 循环进行打怪 while True: # 1. 捕获屏幕 img = capture_screen(region) # 2. 使用YOLO模型推理 results = model(img) # 3. 解析结果,获取怪物位置(这里假设我们只取第一个结果,且第一个类别为怪物) # 注意:results[0]是一个Results对象,里面包含检测到的框等信息 boxes = results[0].boxes # 如果有检测到怪物 if len(boxes) > 0: # 这里简单取第一个怪物 box = boxes[0] # 获取中心点坐标(注意:box.xyxy返回左上角和右下角坐标,我们取中心) x1, y1, x2, y2 = box.xyxy[0].tolist() center_x = int((x1 + x2) / 2) center_y = int((y1 + y2) / 2) # 将坐标转换成屏幕绝对坐标(因为截图区域是游戏窗口,所以需要加上窗口偏移) target_x = left + center_x target_y = top + center_y # 移动鼠标到目标位置并点击(模拟攻击) pyautogui.moveTo(target_x, target_y) pyautogui.click() # 为了避免连续点击太快,可以加一个延迟 time.sleep(0.5) else: # 没有检测到怪物,可以执行其他操作或者等待 time.sleep(1) 給我修改後優化後完整代碼
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值