在这里需要理解pyqt的信号与槽的关系,并且需要了解多线程的相关内容,因为我主要是做硬件方面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_())
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
在这里引用了多线程可能这个程序还是不够完美!需要密切注意,并且我在这里想要往qt界面传递一个动态参数到现在为止没有很好的解决!!!!
# -*- 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", "\n"
"\n"
"使用说明!!!
\n"
"1:使用雷电模拟器打开阴阳师并且将模拟器设置分辨率为
\n"
" ***平板版 960x540
\n"
"2:将游戏窗口放屏幕的左上角,并且不要遮挡游戏窗口
\n"
"3: 完成上述操作以后点击开开始
\n"
"
\n"
" ******作者:BingYiii 联系qq:981725514 ****
"))
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())
在截取屏幕的时候需要注意,并且需要着重注意么个cv匹配的算法,在计算坐时候可能有所出入
注意!!!!
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)
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)
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