Python制作yys彻底解放双手(代码篇)

我看到好多人想要具体的代码,但是我希望你抱着学习的心态来做这件事情,写该脚本的意义是为了更好的学习python语言而不是进行游戏,千万不要本末倒置

文章相关的问题!!!!

1.qt界面如下:


在这里只要点击开始按键就可以自动进行三张图片的对比

开始

在这里插入图片描述

中间

在这里插入图片描述

结束

在这里插入图片描述

在主程序中主要进行qt界面的初始化,关于程序进行放在qt界面的槽中进行

在这里需要理解pyqt的信号与槽的关系,并且需要了解多线程的相关内容,因为我主要是做硬件方面py只是业余爱好,所以这里就不多进行介绍了,想要学习的自己进行网上了解

main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import yysqt

app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = yysqt.Ui_YYS()
ui.setupUi(MainWindow)
MainWindow.show()

sys.exit(app.exec_())

在setting中主要是相关变量的定义,起到文件与文件之间变量调用的作用

settings.py
class Settings():
    """储存所有阴阳师脚本的设置类"""
    start_x = None
    start_y = None
    # 结束点击范围
    end_x = None
    run = None
    text = None

    def set_x(x):
        Settings.start_x = x

    def set_y(y):
        Settings.start_y = y

    def end_x(x_y):
        Settings.end_x = x_y

    def get_x():
        return Settings.start_x

    def get_y():
        return Settings.start_y

    def get_end_x():
        return Settings.end_x


    def set_run(run):
        Settings.run = run

    def get_run():
        return Settings.run

    def set_text(text):
        Settings.run = str(text)

    def get_text():
        return Settings.text

yysqt中主要是设置pyqt的相关界面

在这里引用了多线程可能这个程序还是不够完美!需要密切注意,并且我在这里想要往qt界面传递一个动态参数到现在为止没有很好的解决!!!!

yysqt.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from settings import Settings
import yys
from PyQt5 import QtCore, QtGui, QtWidgets
import threading


class Ui_YYS(object):
    def setupUi(self, YYS):
        YYS.setObjectName("YYS")
        YYS.resize(533, 297)
        self.centralwidget = QtWidgets.QWidget(YYS)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(190, 110, 114, 51))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.pressed.connect(self.click_success)

        # self.pushButton.pressed.connect(self.print_text)
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(11, 11, 508, 90))
        self.textBrowser.setObjectName("textBrowser")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(101, 220, 291, 20))
        self.label.setObjectName("label")
        # self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        # self.lineEdit.setGeometry(QtCore.QRect(10, 180, 501, 31))
        # self.lineEdit.setObjectName("lineEdit")
        YYS.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(YYS)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 533, 26))
        self.menubar.setObjectName("menubar")
        YYS.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(YYS)
        self.statusbar.setObjectName("statusbar")
        # self.lineEdit.textChanged.connect(self.print_text)
        YYS.setStatusBar(self.statusbar)
        self.retranslateUi(YYS)
        QtCore.QMetaObject.connectSlotsByName(YYS)

    def retranslateUi(self, YYS):
        _translate = QtCore.QCoreApplication.translate
        YYS.setWindowTitle(_translate("YYS", "阴阳师脚本"))
        self.pushButton.setText(_translate("YYS", "点击开始"))
        self.textBrowser.setHtml(_translate("YYS", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">使用说明!!!</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">1:使用雷电模拟器打开阴阳师并且将模拟器设置分辨率为 </p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"> ***平板版 960x540</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">2:将游戏窗口放屏幕的左上角,并且不要遮挡游戏窗口</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">3: 完成上述操作以后点击开开始</p>\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">     ******作者:BingYiii  联系qq:981725514 ****</p></body></html>"))
        self.label.setText(_translate("YYS", "作者:BingYIII 联系方式:qq981725514"))


    def click_success(self):
        text = self.pushButton.text()
        if text == "点击开始":
            self.pushButton.setText("点击停止")
            Settings.set_run(1)
            t = threading.Thread(target=yys.yys_run)
            t.start()
        if text == "点击停止":
            self.pushButton.setText("点击开始")
            Settings.set_run(0)
            print(Settings.get_run())


y_functions文件中进行截取屏幕并匹配点击的相关操作

在截取屏幕的时候需要注意,并且需要着重注意么个cv匹配的算法,在计算坐时候可能有所出入注意!!!!

y_functions.py
import pyautogui
import cv2
import random
import time
import settings as Settings
from pymouse import PyMouse


def get_screen():
    # 截屏口令 保存图片
    img = pyautogui.screenshot(region=(0,0, 1280, 720))
    img.save('./img/screen_img.png')
    img = cv2.imread('./img/screen_img.png', 0)
    return img


def match(img1, template):
    """img1代表待匹配图像, img2代表模板"""

    res = cv2.matchTemplate(img1, template, cv2.TM_SQDIFF_NORMED)
    mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    tl = min_loc
    Settings.Settings.set_x(tl)
    t_start = cv2.imread('./img/start.png', 0)
    th, tw = t_start.shape[:2]
    y_setting = (int(tl[0]) + tw, int(tl[1]) + th)
    Settings.Settings.set_y(y_setting)
#   maxres = res.max()
    return mn_val


def get_randxy(x, y):
    """产生一个在x,y二维区域内的随机位置,x,y为两个元素的列表,变量范围"""
    xc = int((x[0]+y[0])/2)
    yc = int((x[1]+y[1])/2)

    # xc = random.randint(x[1], x[0])
    # yc = random.randint(y[1], y[0])
    return xc, yc


def get_randtime(a, b):
    """产生a,b间的随机时间延迟"""
    time.sleep(random.uniform(a, b))


def click(x, y):
    """输入两个二维列表,表示要点击的位置的x座标,y座标"""
    m = PyMouse()
#    cmd_click = 'adb shell input tap {} {}'.format(x, y)
    m.click(x, y, 1)

y_functions2文件中对cv模板和截取的图片进行对比

y_functions2.py
import y_functions as yf
import cv2
import settings
import yysqt



# 准备模块
# 导入模板,设置刷御魂次数模块
def begin():
    t_start = cv2.imread('./img/start.png', 0)
    t_end = cv2.imread('./img/end.png', 0)
    t_first_end = cv2.imread('./img/end0.png', 0)
    # n = input("请输入刷御魂次数:")
    # n = int(n)
    return t_start,t_end, t_first_end


# 匹配模块
# 检测模板,点击屏幕
def matchT(t,x):

    img1 = yf.get_screen()
    res = yf.match(img1, t)
    # settings.Settings.set_text(res)
    # yyqt = yysqt.Ui_YYS()
    # yyqt.print_text(res)
    print(res)
    if x == 1:
        value = 2e-05
    if x == 2:
        value = 0.1
    if x == 3:
        value = 0.1
    if res < value:
        # 匹配成功
        flog = True
    else:
        # yf.get_randtime(0.1, 0.2)
        flog = False
    if flog == True:
        x = settings.Settings.get_x()
        y = settings.Settings.get_y()
        print(x[0], x[1])
        print(y[0], y[1])
        sx, sy = yf.get_randxy(x, y)
        yf.get_randtime(0.5, 1)

        yf.click(sx,sy)

        print("敲击成功")
        yf.get_randtime(0.5, 1)


yys文件中进行界面和程序的交互

yys.py
import y_functions2 as yf2
from settings import Settings


def yys_run():
    val = Settings.get_run()
    print(val)
    if val == 1:
         while True:
            # 导入准备模板
            t_start, t_end,t_first_end= yf2.begin()
            # n为计划刷御魂次数,通过begin函数输入
            # 检测挑战模板
            yf2.matchT(t_start,1)
            yf2.matchT(t_first_end,2)
            yf2.matchT(t_end,3)
            val = Settings.get_run()
            if val == 0:
                break


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章