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


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