脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 微信跳一跳python自动代码解读1.0

微信跳一跳python自动代码解读1.0

2021-01-04 00:47Big_Head_ Python

这篇文章主要为大家详细介绍了微信跳一跳python自动代码解读1.0,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

微信跳一跳自动代码,具体内容如下

那个跳一跳python“外挂”,有几个python文件,其中有一个是得到截图,然后鼠标在图片上点击两次,python窗口上会打印两次鼠标的位置,并且会跟上一行这两个点之间的距离。

微信跳一跳python自动代码解读1.0

这个功能我先给除去获取截屏,就说怎么在某张图片上算出两次点击的距离。

首先,需要用到图形模块,PIL:

?
1
2
from PIL import Image
img = Image.open('0.jpg')

然后用图形绘制模块matplotlib来给出一个plot对象:

?
1
2
import matplotlib.pyplot as plt
fig = plt.figure()

给这个对象加上刚刚打开图片的标签:

?
1
plt.imshow(img, animated = True)

然后用matplotlib的canvas.mpl_connect函数,将我们点击的动作和图片连接起来,这个函数的第二个参数要我们自己的写。

?
1
fig.canvas.mpl_connect('button_press_event', on_press)

在这个自定义的on_press函数,我们要实现得到两个点以后再算出距离。
那么我们就要有变量来储存两个点,临时储存点,来计算点击了多少次,横纵坐标
分别用全局变量cor=[0,0],coords=[], click_count=0,ix,iy

?
1
2
3
4
5
6
7
8
9
10
global ix,iy
 global click_count
 global cor
 
 ix,iy = event.xdata, event.ydata
 coords = []
 coords.append((ix,iy))
 print("now = ", coords)
 cor.append(coords)
 click_count += 1

先把点储存在临时的coords里面,打印出当前位置,然后将临时的放入全局变量cor里面, 并且点击次数+1.

?
1
2
3
4
5
6
7
8
9
if click_count > 1:
 click_count = 0
 
 cor1 = cor.pop()
 cor2 = cor.pop()
 
 distance = (cor1[0][0] - cor2[0][0]) **2 + (cor1[0][1] - cor2[0][1]) **2
 distance = distance ** 0.5
 print("distance = ", distance)

当点击次数大于1的时候,就说明已经储存了两个点了。
这里用的栈pop()方法得到两个点,分别放入cor1 和 cor2, 那么cor1 和 cor2 就是两个点了。
接着计算出距离distance就行了。

完整代码:

?
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
34
35
36
import numpy as np
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
from PIL import Image
def on_press(event):
 global ix,iy
 global click_count
 global cor
 
 ix,iy = event.xdata, event.ydata
 coords = []
 coords.append((ix,iy))
 print("now = ", coords)
 cor.append(coords)
 
 click_count += 1
 if click_count > 1:
  click_count = 0
 
  cor1 = cor.pop()
  cor2 = cor.pop()
 
  distance = (cor1[0][0] - cor2[0][0]) **2 + (cor1[0][1] - cor2[0][1]) **2
  distance = distance ** 0.5
  print("distance = ", distance)
 
cor = [0,0]
click_count = 0
fig = plt.figure()
img = Image.open('0.jpg')
#updata = True
 
plt.imshow(img, animated= True)
 
fig.canvas.mpl_connect('button_press_event', on_press)
plt.show()

最终效果:

微信跳一跳python自动代码解读1.0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/Big_Head_/article/details/78976302

延伸 · 阅读

精彩推荐