• FPS_AI编程


    声明

    本文仅仅用于学习交流,请勿进行真人游戏或者商用。 目前也在根据代码进行AI打FPS的反击程序的测试,有兴趣的可以一起讨论。我们以APEX为列子进行说明。

    技术层面需要掌握的东西

    win32gui.FindWindow

    win32gui.Findwindow(param1,param2):
    param1需要传入窗口的类名,param2需要传入窗口的标题
    一般情况下,参数一填写none即可,参数二是游戏名字

    PyQt截图

    这个依赖组件很多大家可以依次输入以下命令安装,也可以自己写个记事本批量安装

    pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple/
    pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
    pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/
    pip install pypiwin32 -i https://pypi.tuna.tsinghua.edu.cn/simple/
    
    • 1
    • 2
    • 3
    • 4

    使用pyautogui方法实现截屏

    import pyautogui
    import cv2
    import numpy as np
    
    img = pyautogui.screenshot(region=[300,50, 200, 100])  
    # 分别代表:左上角坐标,宽高
    #imshow,默认是BGR,pyautogui默认是RGB,因此要手动转换
    img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    cv2.imshow("截屏",img)
    cv2.waitKey(0)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    不使用的理由:不能指定获取程序的窗口,因此窗口也不能遮挡

    使用win32gui方法实现截屏

    from PyQt5.QtWidgets import QApplication
    import win32gui
    import sys
    #这个是截取全屏的
    hwnd = win32gui.FindWindow(None, 'C:/Windows/system32/cmd.exe')
    app = QApplication(sys.argv)
    screen = QApplication.primaryScreen()
    img = screen.grabWindow(hwnd).toImage()
    img.save("screenshot.bmp")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    win32api.mouse_event

    win32api.mouse_event(param1,param2,param3)
    第一个参数,设定鼠标的行为:相对移动或者绝对移动
    第二个参数,x轴相对移动的距离
    第三个参数,y轴相对移动的距离

    完整代码

    import math
    import sys
    import torch
    import win32api
    import win32con
    import win32gui
    from PyQt5.QtWidgets import QApplication
    from pynput.mouse import Controller
    
    
    class Point:
        """
        定义点,便于后面计算距离
        """
    
        def __init__(self, x1, y1, x2, y2):
            self.x1 = x1
            self.y1 = y1
            self.x2 = x2
            self.y2 = y2
    
    
    class Line(Point):
        """
        定义线,便于后面计算距离
        """
    
        def __init__(self, x1, y1, x2, y2):
            super().__init__(x1, y1, x2, y2)
    
        def get_len(self):
            """
            获取笛卡尔距离
            :return: 笛卡尔距离
            """
            length = math.sqrt(math.pow((self.x1 - self.x2), 2) +
                               math.pow((self.y1 - self.y2), 2))
            return length
    
    
    def main():
        # GPU处理
        device = torch.device("cuda")
        model = torch.hub.load('E:\Project\Python_Project\yolov5-master', 'custom',
                               "E:\Project\Python_Project\yolov5--weights\yolov5x.pt",
                               source='local', force_reload=False)  # 加载本地模型
    
        def screen_record():
            """
            截图
            """
            hwnd = win32gui.FindWindow(None, "Apex Legends")
            app = QApplication(sys.argv)
            screen = QApplication.primaryScreen()
            img = screen.grabWindow(hwnd).toImage()
            img.save("ApexLegendsbg.bmp")
    
        while True:
            # 截取屏幕
            screen_record()
            # 使用模型
            model = model.to(device)
            img = 'ApexLegendsbg.bmp'
            # 开始推理
            results = model(img)
            # 过滤模型
            xmins = results.pandas().xyxy[0]['xmin']
            ymins = results.pandas().xyxy[0]['ymin']
            xmaxs = results.pandas().xyxy[0]['xmax']
            ymaxs = results.pandas().xyxy[0]['ymax']
            class_list = results.pandas().xyxy[0]['class']
            confidences = results.pandas().xyxy[0]['confidence']
            new_list = []
            for xmin, ymin, xmax, ymax, classitem, conf in zip(xmins, ymins, xmaxs, ymaxs, class_list, confidences):
                if classitem == 0 and conf > 0.5:
                    new_list.append([int(xmin), int(ymin), int(xmax), int(ymax), conf])
            # 循环遍历每个敌人的坐标信息传入距离计算方法获取每个敌人距离鼠标的距离
            if len(new_list) > 0:
                # 存放距离数据
                cdList = []
                xyList = []
                for listItem in new_list:
                    # 当前遍历的人物中心坐标
                    x_index = int(listItem[2] - (listItem[2] - listItem[0]) / 2)
                    y_index = int(listItem[3] - (listItem[3] - listItem[1]) / 2)
                    mouseModal = Controller()
                    x, y = mouseModal.position
                    L1 = Line(x, y, x_index, y_index)
                    # 获取到距离并且存放在cdList集合中
                    cdList.append(int(L1.get_len()))
                    xyList.append([x_index, y_index, listItem[0], listItem[1], listItem[2], listItem[3]])
                # 这里就得到了距离最近的敌人位置了
                minCD = min(cdList)
                # 分辨率
                game_width = 1920
                game_height = 1080
                # 如果敌人距离鼠标坐标小于150则自动进行瞄准,这里可以改大改小,小的话跟枪会显得自然些
                if minCD < 100:
                    for cdItem, xyItem in zip(cdList, xyList):
                        if cdItem == minCD:
                            # 锁头算法:使用win32api获取左键按下状态,如果按下则开始自动跟枪
                            if win32api.GetAsyncKeyState(0x01):
                                win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(xyItem[0] - game_width // 4),
                                                     int(xyItem[1] - (game_height - (xyItem[3] - xyItem[5])) // 4), 0, 0)
                            break
    
    
    if __name__ == '__main__':
        main()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
  • 相关阅读:
    springboot配置文件
    利用Python进行数据分析——函数部分
    基于Java实现的离散数学测试实验
    奇迹mu 架设过程中可能会出现的问题及解决办法
    如何获取一个目录下的子文件和子目录
    Java后端底座从无到有的搭建(随笔)
    OpenGL ES之深入解析PBO、UBO与TBO的功能和使用
    分布式文件服务器——初识MinIO
    第一行代码Android 第九章9.4-9.5(解析JSON格式,网络编程最佳实践:发送HTTP请求的代码)
    单链表详解
  • 原文地址:https://blog.csdn.net/GodGump/article/details/127751054