实现原理:
对各个按钮等需要点击的地方进行截图,然后对整个屏幕截屏,通过模板匹配得到截图在截屏中的坐标位置,进行点击
####截图方法1####
import win32gui
import win32ui
import win32con
import win32api
def Printscreen():
# 获取桌面
hdesktop = win32gui.GetDesktopWindow()
# 分辨率适应
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
# width=1920 # 因为用win32api.GetSystemMetrics获取的分辨率不对,于是我直接手输入进去的
# height=1080
# 创建设备描述表
desktop_dc = win32gui.GetWindowDC(hdesktop)
img_dc = win32ui.CreateDCFromHandle(desktop_dc)
# 创建一个内存设备描述表
mem_dc = img_dc.CreateCompatibleDC()
# 创建位图对象
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(img_dc, width, height)
mem_dc.SelectObject(screenshot)
# 截图至内存设备描述表
mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top), win32con.SRCCOPY)
# 将截图保存到文件中
screenshot.SaveBitmapFile(mem_dc, 'yuan.png')
# 内存释放
mem_dc.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
# 测试
Printscreen()
####截图方法2####
from PIL import Image
from PIL import ImageGrab
def Printscreen():
# 截图坐标 左上角 ,右下角
size = (0, 0,1920,1080)
img = ImageGrab.grab(size)
# 保存截图
img.save("yuan.png")
print('截图进行了一次刷新')
# 打开截图
# img.show()
# 测试
Printscreen()
通过模板匹配得到截图的坐标位置
注:模板匹配具有自身的局限性,主要表现在它只能进行平行移动,若原图像中的匹配目标发生旋转或大小变化,该算法无效。所以打开阴阳师后不要进行放大或缩小。
import cv2
import numpy as np
from matplotlib import pyplot as plt
import math
###图像匹配###
def Image_Discern(imgone,imgtwo):
# 1.模板匹配
# 大图
img = cv2.imread(imgone)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 小图
template = cv2.imread(imgtwo, 0)
h, w = template.shape[:2] # rows->h, cols->w
img2 = img.copy()
# 对比图像
res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF_NORMED)
# 返回坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
# 计算中心坐标
a1, a2 = top_left
b1, b2 = bottom_right
c1 = (a1 + w/2)*0.8 # 0.8匹配屏幕分辨率(因为分辨率原因这里乘0.8用于适应平复分辨率坐标位置)
c2 = (a2 + h/2)*0.8
e1 = math.ceil(c1)
e2 = math.ceil(c2)
d1 = (e1, e2)
# print('中心坐标为:' , d1)
###测试图像匹配,弹出图像显示匹配位置###
# 在匹配点画小圆心
# cv2.circle(res, top_left, 10, 0, 2)
# cv2.imshow("res", res)
# # 画矩形
# cv2.rectangle(img2, top_left, bottom_right, (0, 255, 0), 2)
# cv2.imshow("img2",img2)
# cv2.waitKey(0)
# 两张图片是否匹配
# print('各个参数为:',min_val, max_val, min_loc, max_loc)
###进行图像筛选###
if min_val <= 0.03:
# print('图片匹配')
return(d1)
else:
# print('图片不匹配')
return(0)
# 测试
# Image_Discern('e1.png','a2.png') # (大图,小图)匹配图像
通过坐标位置进行点击
import win32api
import win32con
import win32gui
import time
import random
def xunzhao():
wdname = u'阴阳师-网易游戏'
# 取得窗口句柄
hwnd = win32gui.FindWindow(0, wdname)
if not hwnd:
print("窗口找不到,请确认窗口句柄名称:【%s】" % wdname )
exit()
# 窗口显示最前面
win32gui.SetForegroundWindow(hwnd)
def move_click(x, y, t=0): # 移动鼠标并点击左键
suiji1 = random.randint(0,10)
suiji2 = random.randint(0,10)
# print('鼠标抖动随机数为:+',suiji2,' +',suiji1)
win32api.SetCursorPos((x+suiji1, y+suiji2)) # 设置鼠标位置(x, y),设置随机数,以防被封
time.sleep(0.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) # 点击鼠标左键
# print('模拟点击')
return 0
# 测试
# while True :
# suiji = random.randint(0,5) #获得随机数
# xunzhao() #游戏顶置,获得句柄
# time.sleep(suiji) #间隔时间
# move_click(1280, 685) #坐标点击
一个简陋的实例
有基本的方法后,然后就可以写代码进行挂机了。
贴一个自己刷探索的代码,其中click模块为鼠标模拟点击,printscreen模块为截屏,image_discern模块为模板匹配
import click
import printscreen
import image_discern
import time
###进行图标点击###
def Identify_Click(little):
a = 0.1
click.xunzhao()
time.sleep(a) # 间隔秒数
printscreen.Printscreen() # 截图
time.sleep(a)
b1 = image_discern.Image_Discern('yuan.png', little) #图像对比
for i in range(1,10):
if b1 == 0:
print('准备再次寻找第',i,'次')
time.sleep(a)
click.xunzhao()
time.sleep(a)
printscreen.Printscreen()
time.sleep(a)
b1 = image_discern.Image_Discern('yuan.png', little)
else:
time.sleep(a)
printscreen.Printscreen() # 截图
b1 = image_discern.Image_Discern('yuan.png', little) #图像对比
a1, a2 = b1 #地址赋值
click.move_click(a1, a2) #模拟点击
time.sleep(a)
break
###寻找场景内是否有相应图标###
def Identify_Click_Seek(little):
a = 0.1
click.xunzhao()
time.sleep(a) # 间隔秒数
printscreen.Printscreen() # 截图
time.sleep(a)
b1 = image_discern.Image_Discern('yuan.png', little) #图像对比
if b1 == 0:
# print('没有相应图标')
return(False)
else:
# print('找到相应图标')
return(True)
上面那代码块为identify_click模块
import time
import identify_click
import random
def Tansuo():
b=0
while True:
a = ('-----------------')
##判断体力是否足够##
if identify_click.Identify_Click_Seek('./png/tansuo/tansuo13.png'):
print(a)
print('体力不足')
break #结束循环
###组队结束战斗###
if identify_click.Identify_Click_Seek('./png/tansuo/tansuo17.png'):
print(a)
print('战斗结束,回到组队页面')
break #结束循环
##判断是否结束##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo12.png'):
print(a)
print('探索结束!!')
break
##判断战斗结束##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo04.png'):
identify_click.Identify_Click('./png/tansuo/tansuo04.png')
print(a)
print('战斗结束')
continue #跳出本次循环
##判断准备按钮##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo08.png'):
identify_click.Identify_Click('./png/tansuo/tansuo08.png')
print(a)
print('战斗开始')
while True:
if identify_click.Identify_Click_Seek('./png/tansuo/tansuo15.png'):
identify_click.Identify_Click('./png/tansuo/tansuo15.png')
break
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo04.png'):
identify_click.Identify_Click('./png/tansuo/tansuo04.png')
break
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo16.png'):
identify_click.Identify_Click('./png/tansuo/tansuo16.png')
else:
time.sleep(0.1)
if identify_click.Identify_Click_Seek('./png/tansuo/tansuo15.png'):
print('!!!!!!!!!!!')
print('!战斗失败,中止脚本!')
print('!!!!!!!!!!!')
break
continue #跳出本次循环
##BOSS##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo07.png'):
print(a)
print('发现BOSS')
identify_click.Identify_Click('./png/tansuo/tansuo07.png')
continue
##小怪##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo03.png'):
print(a)
print('发现小怪')
identify_click.Identify_Click('./png/tansuo/tansuo03.png')
continue
##奖励宝箱##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo09.png'):
identify_click.Identify_Click('./png/tansuo/tansuo09.png')
time.sleep(random.randint(1,2))
while True:
if identify_click.Identify_Click_Seek('./png/tansuo/tansuo10.png'):
identify_click.Identify_Click('./png/tansuo/tansuo11.png')
break
else:
time.sleep(2)
print(a)
print('领取奖励宝箱')
continue
##进行走动##
elif identify_click.Identify_Click_Seek('./png/tansuo/tansuo05.png'):
print(a)
print('进行走动')
b = b+1
if b < 5:
identify_click.Identify_Click('./png/tansuo/tansuo05.png')
print('向右')
time.sleep(random.randint(3,4)) #间隔3~4秒
continue
else:
identify_click.Identify_Click('./png/tansuo/tansuo06.png')
print('向左')
time.sleep(random.randint(3,4)) #间隔3~4秒
continue
time.sleep(0.5) #间隔3~4秒
return(True)
# 测试
Tansuo()
当中的图片
这些是最近东拼西凑的代码,简单的代替了下无聊的手点鼠标环节。
还有一个简单实现的方法,运用pyautogui库,进行鼠标点击、截屏等操作
源地址;https://www.cnblogs.com/ananing/p/python.html
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有