用python实现qq游戏-大家来找茬辅助


起因是因为周末闲着蛋疼,在修仙的时候,看到了这个游戏。玩了几把发现玩不过别人。苦思一晚上,第二天去网上找思路,发现有前人写过这类代码。但缺点也很明显,不够准确。基本5个不同能找到3个左右。

参考链接

1
自己的思路是如图这样的。但后来发现第4步无法实现,可以说代码看的有点累,也有点懒了。去网上找了几个源码套进去,发现不尽如人意。短时间内理解不了一些算法。卒=。=然后就放弃了。在理解的基础上自己写出了前三步的代码。打包成了一个exe

流程图

流程图.png

前三步代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import win32gui,time
from PIL import Image
from PIL import ImageGrab
from PIL import ImageChops
hwnd=win32gui.FindWindow("#32770","大家来找茬") #找到窗口句柄
if hwnd:
# print(hwnd)
win32gui.SetForegroundWindow(hwnd) #激活窗口到最前
game_rect = win32gui.GetWindowRect(hwnd) #找到整个窗口矩形
time.sleep(0.1)
# print(game_rect[0],game_rect[1],game_rect[2],game_rect[3])
#0为窗口左边上离原点的x距离,1为0为窗口左边上离原点的y距离,2为窗口右下离原点的x距离,3为窗口右下离原点的y距离

width = game_rect[2] - game_rect[0] #矩形的位置
height = game_rect[3] - game_rect[1]
# print(width,height)
base_width=1024 #原来宽度
base_height=768 #原来高度
rate_width=width/base_width #计算缩放比例
rate_height=height/base_height #计算缩放比例
# print(rate_width,rate_height)
rect1 = (game_rect[0]+int(92*rate_width),game_rect[1]+int(312*rate_height),game_rect[0]+width/2-int(37*rate_width),game_rect[1]+int(598*rate_height))
#取四个值,截取。ImageGrab.grab((num, num, num, num)),指定范围内的图像
rect2 = (game_rect[0]+width/2+int(37*rate_width),game_rect[1]+int(312*rate_height),game_rect[2]-int(92*rate_width),game_rect[1]+int(598*rate_height))
#取四个值,截取。ImageGrab.grab((num, num, num, num)),指定范围内的图像
ima = ImageGrab.grab(rect1) #抓取当前屏幕内容
imb = ImageGrab.grab(rect2)
out = ImageChops.difference(ima,imb) #返回两幅图像逐像素差的绝对值形成的图像。
# ima.save("test1.jpg",'jpeg') #保存的左图,测试用,可注释
# imb.save("test2.jpg",'jpeg') #保存的右图,测试用,可注释
out.save("out.jpg",'jpeg') #最后比较完的结果图
aaa=Image.open("out.jpg")
aaa.show()

效果图

每一关运行exe。会直接显示出差异图。
6M0O4x.png

之后对图像识别、opencv、cv2理解更深,再来补