python3 游戏挂机脚本

import pyautogui
import numpy as np
import PIL
import pytesseract
from PIL import Image
import time
import cv2
import os
import keyboard

# find_list = ['img/wait.jpg', 'img/end.jpg']
genting_list = ['img/Genting/start.bmp', 'img/Genting/commit.bmp', 'img/Genting/exit.bmp', 'img/Genting/next.bmp']
equip_list = ['img/Genting/equip1.bmp', 'img/Genting/equip2.bmp']


def find_on_template(target, template):
    theight, twidth = target.shape[:2]

    result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    # 如果匹配度小于99%,就认为没有找到。
    if min_val > 0.1:
        return None
    strmin_val = str(min_val)
    # print(strmin_val)

    x = min_loc[0] + twidth // 2
    y = min_loc[1] + theight // 2
    return x, y


def get_position(image):
    # target = cv2.imread("target.jpg")
    target = pyautogui.screenshot()
    target = np.array(target)

    template = cv2.imread(image, dtype=np.uint8)

    # 获得模板图片的高宽尺寸
    theight, twidth = template.shape[:2]
    # 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
    result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
    # 归一化处理
    cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
    # 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    return min_loc[0] + twidth / 2, min_loc[1] + theight / 2


def find_and_click(target_dir, left_or_right):
    screen_img = pyautogui.screenshot()

    screen_img = cv2.cvtColor(np.asarray(screen_img), cv2.COLOR_RGB2BGR)

    target = cv2.imread(target_dir)

    position = find_on_template(target, screen_img)

    if position is not None:
        if left_or_right:
            pyautogui.moveTo(position[0], position[1])
            pyautogui.mouseDown()
            time.sleep(0.1)
            pyautogui.mouseUp()
        else:
            pyautogui.rightClick(x=position[0], y=position[1])

        # pyautogui.moveTo(position[0], position[1])
        # pyautogui.click()
        return True

    return False


def auto_genting():
    switch_on = False

    cards_list = make_img_list('img/Genting/cards')

    screenWidth, screenHeight = pyautogui.size()

    while True:
        if keyboard.is_pressed('f5'):
            print('开始!')
            switch_on = True
        elif keyboard.is_pressed('f6'):
            print('结束!')
            switch_on = False
        if switch_on:
            pyautogui.moveTo(screenWidth/2, screenHeight/2)
            # 开始or结束
            for each in genting_list:
                find_and_click(each, True)
            # 装备
            for each in equip_list:
                find_and_click(each, False)
            # 搜牌
            for each in cards_list:
                find_and_click(each, True)
            # 时间节点


def make_img_list(img_dir):
    res = []
    for root, dirs, files in os.walk(img_dir):
        for file in files:
            res.append(os.path.join(root, file))
    return res


if __name__ == '__main__':

    auto_genting()

    # main()

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值