[Python] 纯文本查看 复制代码
print("正在加载库")
import win32api
import win32con
import time
import random
import win32gui
import random
from PIL import Image, ImageGrab
# %matplotlib inline
print("加载完成")
print("正在定义函数")
def move_click(x, y, t=0): # 移动鼠标并点击左键
win32api.SetCursorPos((x, y)) # 设置鼠标位置(x, y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN |
win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # 点击鼠标左键
if t == 0:
time.sleep(random.random()*2+1) # sleep一下
else:
time.sleep(t)
return 0
def get_posx(x, window_size): # 返回x相对坐标
return (window_size[2] - window_size[0]) * x /870
def get_posy(y, window_size): # 返回y相对坐标
return (window_size[3] - window_size[1]) * y /520
def get_window_info(): # 获取阴阳师窗口信息
wdname = '阴阳师-网易游戏'
handle = win32gui.FindWindow(0, wdname) # 获取窗口句柄
if handle == 0:
print('请打开PC端阴阳师,并进入御魂界面,选定御魂层数')
return None
else:
return win32gui.GetWindowRect(handle)
def get_hash(img):
img = img.resize((16, 16), Image.ANTIALIAS).convert('L') # 抗锯齿 灰度
avg = sum(list(img.getdata())) / 256 # 计算像素平均值
s = ''.join(map(lambda i: '0' if i < avg else '1', img.getdata())) # 每个像素进行比对,大于avg为1,反之为0
return ''.join(map(lambda j: '%x' % int(s[j:j+4], 2), range(0, 256, 4)))
def hamming(hash1, hash2, n=20):
b = False
assert len(hash1) == len(hash2)
if sum(ch1 != ch2 for ch1, ch2 in zip(hash1, hash2)) < n:
b = True
return b
def resolution(): # 获取屏幕分辨率
return win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)
print("函数定义完成")
print("正在读取对比图")
hash1='fe7ef01e6386c7c287e08ff01df83ffc3ffc1bf809f00de003c001820004381f'
hash3='09e03ff07ff27fe47ff87f783f7f7f0f4fef7e3f302f00000000000180000000'
print("脚本准备完成")
print("正在图像识别")
window_size=get_window_info()
topx, topy = window_size[0], window_size[1]
a=topx + get_posx(732, window_size)
b=topy + get_posy(408, window_size)
c=topx + get_posx(832, window_size)
d=topy + get_posy(508, window_size)
img_ready = ImageGrab.grab((topx + get_posx(732, window_size), topy + get_posy(408, window_size),
topx + get_posx(832, window_size), topy + get_posy(508, window_size)))
# 查看图片
# img_ready
print("图像识别完成")
print("ma'shang")
hash2=get_hash(img_ready)
n=int(input("请输入要刷几次御魂:"))
for i in range(n):
print("正在进行第"+str(i)+"次御魂")
if hamming(hash1, hash2, n=20):
e=random.randint(int(a)+10,int(c)-10)
f=random.randint(int(b)+10,int(d)-10)
move_click(e,f)
time.sleep(random.randint(3,4))
e=random.randint(1013,1112)
f=random.randint(520,667)
move_click(e,f)
win32api.SetCursorPos((random.randint(200,500),random.randint(200,500)))
time.sleep(random.randint(1,3))
win32api.SetCursorPos((random.randint(200,500),random.randint(200,500)))
move_click(random.randint(200,500),random.randint(200,500))
g=random.randint(40,45)
time.sleep(g)
window_size=get_window_info()
topx, topy = window_size[0], window_size[1]
a1=topx + get_posx(350, window_size)
c1=topx + get_posx(510, window_size)
b1=topy + get_posy(280, window_size)
d1=topy + get_posy(450, window_size)
img2 = ImageGrab.grab((a1,b1,c1,d1))
hash4=get_hash(img2)
if hamming(hash3,hash4,n=20):
e=random.randint(1013,1112)
f=random.randint(520,667)
move_click(e,f)
time.sleep(random.randint(3,4))
print("第"+str(i+1)+"次御魂已完成")
print("任务全部完成")