IrisRainbowNeko / genshin_auto_fish

基于深度强化学习的原神自动钓鱼AI
4.78k stars 718 forks source link

buger repoter :Solution to incorrect mouse position/鼠标位置不正确解决办法 #329

Open lin12058 opened 1 year ago

lin12058 commented 1 year ago

问题: 放缩布局不是为100%时会导致鱼饵选择失败 image 下面可以看到操作范围上只有720p,放大1.5倍后才能一致 image 而分辨率是写死的,在获取图片的时候,直接获取长和宽的 image 并且存在分辨率不一致的问题如上面的1924x1120,实际上是1920x1080 明显1120比1080多了40像素,其原因是因为窗口化带来的问题(就是上面那个白条,如下图) image 最终导致屏幕异常,没办法找到正确的饵料

lin12058 commented 1 year ago

以下修改的代码位置在utils/utils.py 主要修改内容是针对scale进行修改,并且移除上面的白边的限制

####新增代码

def get_screen_scaling():
    gvars.genshin_window_rect = win32gui.GetWindowRect(hwnd)# 这里强制刷新,拖动游戏框会更新位置
    '''获取屏幕的缩放比例'''
    hDC = win32gui.GetDC(0)
    """获取真实的分辨率"""
    real_wide = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
    """获取缩放后的分辨率"""
    screen_wide = win32api.GetSystemMetrics(0)
    return round(real_wide / screen_wide, 4)

def get_y_offset():
    """上面的白边"""
    w = gvars.genshin_window_rect[2]-gvars.genshin_window_rect[0]
    h = gvars.genshin_window_rect[3]-gvars.genshin_window_rect[1]
    now_ratio = h/w
    if now_ratio>0.572:# 有白边
        real_ratio = 1080/1920.0
        return int(h-w*real_ratio)  # 这里假设 宽度是绝对正确的(反正两边没白边)
    return 0

####修改代码

def mouse_down(x, y, button=MOUSE_LEFT):
    x=int(x/get_screen_scaling())
    y=int(y/get_screen_scaling())+get_y_offset()
    time.sleep(0.1)
    xx,yy=x+gvars.genshin_window_rect[0], y+gvars.genshin_window_rect[1]
    win32api.SetCursorPos((xx,yy))
    win32api.mouse_event(mouse_list_down[button], xx, yy, 0, 0)

def mouse_move(dx, dy):
    dx=int(dx/get_screen_scaling())
    dy=int(dy/get_screen_scaling())#这里是移动,是相对量,不需要偏移量get_y_offset()
    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, dx, dy, 0, 0)

def mouse_up(x, y, button=MOUSE_LEFT):
    x=int(x/get_screen_scaling())
    y=int(y/get_screen_scaling())+get_y_offset()
    time.sleep(0.1)
    xx, yy = x + gvars.genshin_window_rect[0], y + gvars.genshin_window_rect[1]
    win32api.SetCursorPos((xx, yy))
    win32api.mouse_event(mouse_list_up[button], xx, yy, 0, 0)

def mouse_click(x, y, button=MOUSE_LEFT):
    mouse_down(x, y, button)
    mouse_up(x, y, button)

def mouse_down_raw(x, y, button=MOUSE_LEFT):
    x=int(x/get_screen_scaling())
    y=int(y/get_screen_scaling())+get_y_offset()
    xx, yy = x + gvars.genshin_window_rect[0], y + gvars.genshin_window_rect[1]
    win32api.mouse_event(mouse_list_down[button], xx, yy, 0, 0)

def mouse_up_raw(x, y, button=MOUSE_LEFT):
    x=int(x/get_screen_scaling())
    y=int(y/get_screen_scaling())+get_y_offset()
    xx, yy = x + gvars.genshin_window_rect[0], y + gvars.genshin_window_rect[1]
    win32api.mouse_event(mouse_list_up[button], xx, yy, 0, 0)

def mouse_click_raw(x, y, button=MOUSE_LEFT):
    mouse_down_raw(x, y, button)
    mouse_up_raw(x, y, button)
lin12058 commented 1 year ago

@7eu7d7