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
- 模块
- 调整模拟器窗体大小位置
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 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 thread_it(func, *args):
# 创建
t = threading.Thread(target=func, args=args)
# 守护 !!!
t.setDaemon(True)
# 启动
t.start()
- 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)
- 主要的代码就在这里的还有一些小东西,扒的大佬的:以下是完整的代码
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() #持续运行窗口