(python)cf火线瞄准红名自动开枪

一. 前言

这个应该cf系列第四篇了,目前已经写了ai瞄准,罗技鼠标宏,这篇功能相比前俩个更简单一些,因为代码不多所以就不写类与功能函数了,直接直捣黄龙,我还写了一些辅助脚本,可以用来获取鼠标当前坐标和颜色的,改新的分辨率可以用得上

所需准备:罗技驱动的.dll文件与之对应的罗技驱动版本,链接我放下面了

链接:https://pan.baidu.com/s/1pd2RjNW6QoOSDRs_3gm1Tw?pwd=6666 
提取码:6666

注意事项:这里游戏内分辨率必须是1280*720,我的位置坐标参数就是根据这个分辨率调整的

二. 代码

2.1 驱动代码

这部分代码主要就是调用.dll文件,用里面封装好的方法,比如移动鼠标,点击鼠标,键盘等等,主要实现的功能就是进行射击操作,也就是自动鼠标点击左键

import ctypes
import os


try:
    # 获取当前绝对路径
    root = os.path.abspath(os.path.dirname(__file__))
    driver = ctypes.CDLL(f'{root}/logitech.driver.dll')
    ok = driver.device_open() == 1  # 该驱动每个进程可打开一个实例
    if not ok:
        print('错误, GHUB驱动没有找到')
except FileNotFoundError:
    print(f'错误, DLL 文件没有找到')



class Logitech:

    class mouse:

        """
        code: 1:左键, 2:中键, 3:右键
        """

        @staticmethod
        def press(code):
            if not ok:
                return
            driver.mouse_down(code)

        @staticmethod
        def release(code):
            if not ok:
                return
            driver.mouse_up(code)

        @staticmethod
        def click(code):
            if not ok:
                return
            driver.mouse_down(code)
            driver.mouse_up(code)

        @staticmethod
        def scroll(a):
            """
            鼠标滚轮
            """
            if not ok:
                return
            driver.scroll(a)

        @staticmethod
        def move(x, y):
            """
            相对移动, 绝对移动需配合 pywin32 的 win32gui 中的 GetCursorPos 计算位置
            pip install pywin32 -i https://pypi.tuna.tsinghua.edu.cn/simple
            x: 水平移动的方向和距离, 正数向右, 负数向左
            y: 垂直移动的方向和距离
            """
            if not ok:
                return
            if x == 0 and y == 0:
                return
            driver.moveR(x, y, True)

    class keyboard:

        """
        键盘按键函数中,传入的参数采用的是键盘按键对应的键码
        code: 'a'-'z':A键-Z键, '0'-'9':0-9
        """

        @staticmethod
        def press(code):

            if not ok:
                return
            driver.key_down(code)

        @staticmethod
        def release(code):
            if not ok:
                return
            driver.key_up(code)

        @staticmethod
        def click(code):
            if not ok:
                return
            driver.key_down(code)
            driver.key_up(code)



class RunLogitechTwo:
    def __init__(self):
        self.log_mouse = Logitech.mouse
        pass

    def quick_move(self):
        # time.sleep(random.randint(1, 3))
        self.log_mouse.click(1)
        # print('hahaha')


2.2 红名识别代码

代码思路:利用mss库的mss()功能函数,进行时对当前屏幕进行截图,设置了字典monitor参数,里面包含左上角坐标,宽高,用来控制截屏的范围,我这里对敌人红名出现的地方中央截取了16个像素,这16个像素中一定会有红色像素格出现,一旦其中一个符合rgb范围要求就跳出循环不执行剩下像素的判断,节省资源也防止一直开枪

for循环部分讲解:俩层for循环就是为了遍历截取的16个像素的RGB值进行判断,第二个for循环的bgr属性(分解的每个像素)是有含4个值的,分别是R,B,G,通透度,我们只取前三个就够了通透度不用管,np.array()会将16个像素值分成二维数组(4*4)类似于矩阵的形状,所以俩层for循环就够了

开枪延迟:这个开枪延迟一定要加上,要不然肯定会过快,进行检测,速度大家自行修改尝试就行了,我这个是喷子的延迟,大炮可以调的更长一些,火线还有个问题就是你可能在瞄准那个人附近的时候就开始出现红名了,所以也要通过设置这个开枪延迟来平衡自动开枪时机

吐槽:你在瞄准敌人的时候,他的名字是渐变的,从暗红色-红色-亮红色-橘色,这个RGB范围让我一顿好找

import logitech_test
from mss import mss
import numpy as np
import time
import random




if __name__ == "__main__":
    # 设置检测区域,这里截取整个屏幕
    monitor = {"top": 405, "left": 635, "width": 4, "height": 4}
    # monitor = {"top": 490, "left": 910, "width": 200, "height": 200}  # 反人类的设计,正常左上,他上左
    obj = logitech_test.RunLogitechTwo()
    # 设置目标颜色(示例:红色)
    target_color = np.array([163, 57, 49])  # RGB颜色值
    # 创建截屏对象
    sct = mss()
    while True:
        flag=False
        # 获取屏幕截图
        screen_shot = sct.grab(monitor)
        # 展示
        scr_img = np.array(screen_shot)

        for each_pic in scr_img:
            for bgr in each_pic:
                b, g, r = bgr[:3]
                # rgb参数145,245,47,80,20,57
                if r >= 140 and r <= 245 and g >= 45 and g <= 80 and b >= 20 and b <= 60:
                    time.sleep(random.randint(2, 5) * 0.01)
                    obj.quick_move()
                    flag = True
                    break
            if flag:
                break

三. 辅助工具

下面的脚本是用来时时获取鼠标当前位置的坐标x,y和rgb值的,用于改分辨率时候算参数用的

import pyautogui
from PIL import Image


def test_b():
    # obj = RunLogitechTwo()
    # 默认屏幕为1280 * 720,屏幕中心位置如下
    # x = 640   # 1.18
    # y = 360   # 32.66666
    # 屏幕检测区域
    # region_to_check = (590, 310, 690, 430)
    # x = 636
    # y = 410
    while True:
        x, y = pyautogui.position()  # 获取鼠标当前位置

        # region = (600, 400, 80, 20)
        im = pyautogui.screenshot()  # 返回屏幕的截图,是一个Pillow的image对象

        r,g,b = im.getpixel((x,y))
        print("x:", x, "y:", y, "r:", r,"-g:", g,"-b:", b)


        # img = Image.new('RGB', (300, 300), im.getpixel((x, y)))  # 用获取的颜色创建一张图片
        # img.show()  # 展示当前图片


if __name__ == '__main__':
    test_b()

四. 总结

重要的事情多说一遍,如果你想用我这个代码,游戏内分辨率一定要调整为1280*720,因为我取像素位置就是根据这个分辨率来的,如果你想用别的分辨率自己重新计算一下就行了,也不难,重新找一下新分辨率下的红名出现坐标位置,如果你找不到新的分辨率红名坐标位置,我建议你可以把电脑分辨率调整为和游戏一样的分辨率,然后在游戏中截屏,将截屏的照片设为全屏,再开启辅助工具取坐标和rgb,这样就能准确得知了(我就是这样做的)

最后,如果还有什么不懂的或者困难或者改进也可以邮箱联系我一起进步,874302104@qq.com

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年8月22日
下一篇 2023年8月22日

相关推荐