老板布置的工作:改写C++yolo 并随便写个FPS游戏锁头的算法
已经解决了yoloC++化。但在鼠标应用过程出了问题。
明天要开工作组会了,捉鸡,随便一个FPS全屏游戏都行,对游戏不要求 ,你玩cs1.6我都没意见
方法是不断截屏识别,获取坐标。
问题如下: 在yoloV5导出的坐标中,已换算为导出坐标(POSx,POSy)已经是对应屏幕的坐标了
灵敏度系数调整的也还可以··
但在实际调用过程中出现了鼠标跑飞问题
#include <windows.h>
#include <iostream>
#include <cmath>
#include <random>
// 全局变量,记录最后一次移动的坐标
int lastMovedX = 0;
int lastMovedY = 0;
const LPCWSTR windowClass = L"类名"; // 或者具体的类名,如L"ConsoleWindowClass"
const LPCWSTR windowTitle = L"窗口标题";
bool isMoving = false; // 全局标志,用于标记是否正在进行鼠标移动
double SmoothFunction(double t, float smoothnessFactor)
{
return std::pow(t, 1.0f / (1.0f + smoothnessFactor));
}
// 更新中心视角到目标坐标
void UpdateCenterView(int targetX, int targetY, int step, float sensitivity, float smoothnessFactor)
{
// 获取当前鼠标位置
POINT currentMousePos;
GetCursorPos(¤tMousePos);
// 确保目标坐标在屏幕范围内
targetX = std::clamp(targetX, 0, GetSystemMetrics(SM_CXSCREEN) - 1);
targetY = std::clamp(targetY, 0, GetSystemMetrics(SM_CYSCREEN) - 1);
// 计算目标位置与上次移动位置之间的增量
int deltaX = targetX - lastMovedX;
int deltaY = targetY - lastMovedY;
// 当目标坐标与上一次移动位置的距离小于等于步长时,无需移动
if (std::hypot(deltaX, deltaY) <= step)
{
return;
}
isMoving = true; // 设置标志,表示开始新的鼠标移动操作
// 计算移动方向(以弧度表示)
double angle = atan2(deltaY, deltaX);
// 计算所需移动次数(向上取整)
double distance = std::hypot(deltaX, deltaY);
int numMoves = static_cast<int>(std::ceil(distance / step));
for (int i = 0; i < numMoves; ++i)
{
double t = static_cast<double>(i) / numMoves;
double t_smoothed = t; // 默认无平滑
if (smoothnessFactor > 0.0f)
{
// 使用平滑曲线(如指数或余弦函数)平滑t
t_smoothed = SmoothFunction(t, smoothnessFactor);
}
int newX = static_cast<int>(lastMovedX + step * sensitivity * cos(angle) * t_smoothed);
int newY = static_cast<int>(lastMovedY + step * sensitivity * sin(angle) * t_smoothed);
INPUT input = {};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_MOVE;
input.mi.dx = newX - currentMousePos.x;
input.mi.dy = newY - currentMousePos.y;
SendInput(1, &input, sizeof(INPUT));
Sleep(3);
// 检查是否有新的指令打断当前移动
if (!isMoving)
{
break; // 中断当前移动操作
}
}
lastMovedX = currentMousePos.x;
lastMovedY = currentMousePos.y;
isMoving = false; // 移动操作完成,重置标志
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd = FindWindowW(windowClass, windowTitle);
if (hwnd == NULL)
{
std::cerr << "Failed to find the game window.\n";
return -1;
}
SetForegroundWindow(hwnd);
Sleep(500);
int step = 20;
float sensitivity = 0.1f; // 假设默认视角灵敏度为1.0,根据实际游戏调整
float smoothnessFactor = 0.8f; // 平滑度因子,根据需要调整
//假定屏幕分辨率是1600*900
UpdateCenterView(POSx, POSy, step, sensitivity, smoothnessFactor);
return 0;
}
请修改我的代码 要求任意一个全屏FPS游戏,能交任务就行
效率方法无所谓。C++