Python写阴阳师脚本带GUI又是一个学习小技巧

Python写阴阳师脚本带GUI

  • 需要用到环境 Python3、 pycharm、天天模拟器、阴阳师。
  • 准备库
pip install pyautogui
pip install pywin32
  • 需要用到的全部库
import tkinter as tk
import pyautogui
import time
import random
import win32gui
import win32con
import threading
from tkinter import *
AP = True
Ma = 0
  • 模块
  1. 调整模拟器窗体大小位置
def chuangkou():
 titlename = "靠谱天天模拟器 3.2.9"   #  名字注意你的版本
#  获取句柄
 hwnd = win32gui.FindWindow(0, titlename)
#  获取窗口左上角和右下角坐标
 left, top, right, bottom = win32gui.GetWindowRect(hwnd)
 print("宽",right,"高", bottom)
# 调整目标窗口到坐标(0,0),大小设置为(700,500)
 win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST, 0, 0, 700, 500, win32con.SWP_SHOWWINDOW)
  1. 获取开始匹配按钮
def kaishi():
    if pyautogui.pixelMatchesColor(647,391,(241,213,141)) == True:  # 判断像素点是否为输入像素
        if pyautogui.pixelMatchesColor(684,404,(105,71,35)) == True:  # 判断像素点是否为输入像素
            a = random.randint(649, 678)  # 随机x轴坐标区间
            b = random.randint(391, 414)  # 随机y轴坐标区间

            pyautogui.moveTo(x=a, y=b, duration=0.25)  # 鼠标移动到该随机区域时间为随机c秒
            pyautogui.doubleClick()  # 单击该区域
            print("识别到按钮")
            time.sleep(2)
    time.sleep(random.uniform(0.05, 0.10))
  1. 获取结束画面
def jieshu():
    if pyautogui.pixelMatchesColor(254,119,(153,28,18)) == True:  # 判断像素点是否为输入像素
        if pyautogui.pixelMatchesColor(503,100,(231,214,170)) == True:  # 判断像素点是否为输入像素
            print("识别到")
            time.sleep(random.uniform(0.05, 0.2))  # 随机延迟时间
            a = random.randint(424, 676)  # 随机x轴坐标区间
            b = random.randint(286, 413)  # 随机y轴坐标区间
            for i in range(0, random.randint(7, 9)):  # 随机点击次数7到9次
                a = a + random.randint(2, 8)   # x轴随机位置偏移
                a = a - random.randint(2, 8)
                b = b + random.randint(2, 8)   # y轴随机位置偏移
                b = b - random.randint(2, 8)
                pyautogui.moveTo(x=a, y=b, duration=0.15)  # 随机移动鼠标到随机位置
                time.sleep(random.uniform(0.05, 0.10))  # 随机延迟点击时间间隔
                pyautogui.click()   # 模拟鼠标点击
                time.sleep(random.uniform(0.05, 0.10))  # 点击后延迟随机秒
            time.sleep(1)  # 执行完成后暂停一秒
  1. 因为用的死循环所以要搞个多线程
def thread_it(func, *args):
    # 创建
    t = threading.Thread(target=func, args=args)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
  1. GUI界面 用的tk
def __init__(self, master, masterl):
        fm1 = Frame(master)
        Label(topl, justify=tk.LEFT, image=photo, compound=tk.CENTER, fg="white").pack(side=TOP, anchor=W, fill=BOTH)
        fm1.pack(side=TOP,  fill=BOTH)

        fm2 = Frame(masterl)
        Button(topl, text='开始激情御魂(队长版)', command=lambda: thread_it(New), bg="pink").pack(side=LEFT, anchor=NW, fill=X)
        Button(topl, text='开始激情御魂(打手版)', command=lambda: thread_it(News), bg="DeepSkyBlue").pack(side=LEFT, anchor=N, fill=X, padx=12, )
        Button(topl, text='调整位置', command=lambda: thread_it(chuangkou), bg="red").pack(side=TOP, anchor=NE, fill=X)
        fm2.pack(side=LEFT, fill=NONE, padx=10)

        fm3 = Frame(masterl)
        Button(topl, text='停止', command=lambda: thread_it(tingzhi), bg="yellow").pack(side=LEFT, anchor=W, fill=X)
        Button(topl, text='开始', command=lambda: thread_it(dakai), bg="yellow").pack(side=LEFT, fill=X, padx=5)
        fm3.pack(side=LEFT, fill=NONE, ipadx=10)

  1. 主要的代码就在这里的还有一些小东西,扒的大佬的:以下是完整的代码
import tkinter as tk
import pyautogui
import time
import random
import win32gui
import win32con
import threading
from tkinter import *
AP = True
Ma = 0
def kaishi():
    if pyautogui.pixelMatchesColor(647,391,(241,213,141)) == True:  # 判断像素点是否为输入像素
        if pyautogui.pixelMatchesColor(684,404,(105,71,35)) == True:  # 判断像素点是否为输入像素
            a = random.randint(649, 678)  # 随机x轴坐标区间
            b = random.randint(391, 414)  # 随机y轴坐标区间

            pyautogui.moveTo(x=a, y=b, duration=0.25)  # 鼠标移动到该随机区域时间为随机c秒
            pyautogui.doubleClick()  # 单击该区域
            print("识别到按钮")
            time.sleep(2)
    time.sleep(random.uniform(0.05, 0.10))

def jieshu():
    if pyautogui.pixelMatchesColor(254,119,(153,28,18)) == True:  # 判断像素点是否为输入像素
        if pyautogui.pixelMatchesColor(503,100,(231,214,170)) == True:  # 判断像素点是否为输入像素
            print("识别到")
            time.sleep(random.uniform(0.05, 0.2))  # 随机延迟时间
            a = random.randint(424, 676)  # 随机x轴坐标区间
            b = random.randint(286, 413)  # 随机y轴坐标区间
            for i in range(0, random.randint(7, 9)):  # 随机点击次数7到9次
                a = a + random.randint(2, 8)   # x轴随机位置偏移
                a = a - random.randint(2, 8)
                b = b + random.randint(2, 8)   # y轴随机位置偏移
                b = b - random.randint(2, 8)
                pyautogui.moveTo(x=a, y=b, duration=0.15)  # 随机移动鼠标到随机位置
                time.sleep(random.uniform(0.05, 0.10))  # 随机延迟点击时间间隔
                pyautogui.click()   # 模拟鼠标点击
                time.sleep(random.uniform(0.05, 0.10))  # 点击后延迟随机秒
            time.sleep(1)  # 执行完成后暂停一秒
def chuangkou():
 titlename = "靠谱天天模拟器 3.2.9"
#  获取句柄
 hwnd = win32gui.FindWindow(0, titlename)
#  获取窗口左上角和右下角坐标
 left, top, right, bottom = win32gui.GetWindowRect(hwnd)
 print("宽",right,"高", bottom)
# 调整目标窗口到坐标(0,0),大小设置为(700,500)
 win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST, 0, 0, 700, 500, win32con.SWP_SHOWWINDOW)
def New():   # 御魂司机的方法
    while (AP):  #AP是一个全局变量用来暂停代码的
     print("御魂司机正在执行")
     kaishi()
     print("等待结束")
     time.sleep(1)
     jieshu()
def News():   #  御魂打手的方法
    while (AP):
     print("御魂打手正在执行")
     jieshu()
     time.sleep(1)
def tingzhi():  #停止按钮修改AP变量为False 停止循环
    global AP
    AP = False
    print(AP)
def dakai():   #修改变量AP为true 解锁循环
    global AP
    AP = True
    print(AP)
def thread_it(func, *args):
    # 创建
    t = threading.Thread(target=func, args=args)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    #  t.join()
def __init__(self, master, masterl):   #研究了好久的布局写的超级乱
        fm1 = Frame(master)
        Label(topl, justify=tk.LEFT, image=photo, compound=tk.CENTER, fg="white").pack(side=TOP, anchor=W, fill=BOTH)
        fm1.pack(side=TOP,  fill=BOTH)

        fm2 = Frame(masterl)
        Button(topl, text='开始激情御魂(队长版)', command=lambda: thread_it(New), bg="pink").pack(side=LEFT, anchor=NW, fill=X)
        Button(topl, text='开始激情御魂(打手版)', command=lambda: thread_it(News), bg="DeepSkyBlue").pack(side=LEFT, anchor=N, fill=X, padx=12, )
        Button(topl, text='调整位置', command=lambda: thread_it(chuangkou), bg="red").pack(side=TOP, anchor=NE, fill=X)
        fm2.pack(side=LEFT, fill=NONE, padx=10)

        fm3 = Frame(masterl)
        Button(topl, text='停止', command=lambda: thread_it(tingzhi), bg="yellow").pack(side=LEFT, anchor=W, fill=X)
        Button(topl, text='开始', command=lambda: thread_it(dakai), bg="yellow").pack(side=LEFT, fill=X, padx=5)
        fm3.pack(side=LEFT, fill=NONE, ipadx=10)

topl = tk.Tk()
# 创建top容器
photo = tk.PhotoImage(file="C:\\Users\\longqiang\\Desktop\\python\\实验\\yysimg.png")    #图片可以自己找喜欢的呕大小为400*200像素 注意图片名称
topl.geometry("420x280+851+484")
# 设置界面大小 和界面初始位置,默认的会和模拟器重合
topl.title("我爱你阴阳师")
# 导入图片
__init__(topl, master=topl, masterl=topl)  #调用界面
topl.resizable(0, 0)  #不允许用户改变窗口大小
topl.mainloop()  #持续运行窗口

  • 图片的路径位置记得改在这里插入图片描述

效果图在这里插入图片描述

  • 害萌新python玩家,关于脚本的用到是像素匹配的,天天模拟器的设置不同可能识别不到像素,模拟器设置如下
    在这里插入图片描述
  • 使用流程,进入游戏组队界面直接点开始激情御魂,由于太菜停止按钮的分开的, 后期还能加入其他功能
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值