文章目录
- 原理
- 全部代码
原理
启动后截图。
丢球以后再截图。
如果两图一致,说明没成功,读档重来。
如果两图不一致,说明成功了。
while True:
press(A)
time.sleep(2)
if is_same_img(ImageGrab.grab(), data_img):
press(save2)
else:
break
全部代码
cai_yang.py没改动。
from PIL import Image
import random
# 获得采样数据
def cai_yang(target: Image, rec: (int, int, int, int), count):
result = []
i = 1
while i <= count:
# 随机XY
x, y = random.randint(rec[0], rec[2]), random.randint(rec[1], rec[3])
result.append(((x, y), target.getpixel((x, y))))
i = i + 1
return result
# 获得对比结果
def is_same_img(img: Image, data):
for i in data:
if img.getpixel(i[0]) != i[1]:
return False
return True
main.py。
import time
from PIL import Image, ImageGrab
from settings import *
from cai_yang import *
# 脚本核心
def a1():
c.press(ACCELERATE)
# 截屏
data_img = cai_yang(ImageGrab.grab(), rec, 8)
# 选中精灵球
press(RIGHT)
press(A)
press(A)
# 存档
c.press(save1)
c.press(save2)
time.sleep(0.1)
c.release(save1)
c.release(save2)
while True:
press(A)
time.sleep(2)
if is_same_img(ImageGrab.grab(), data_img):
press(save2)
else:
break
c.release(ACCELERATE)
def press(key, interval=0.2, interval2=0.1): # 按一下
c.press(key)
time.sleep(interval)
c.release(key)
time.sleep(interval2)
if __name__ == '__main__':
# 键盘事件处理
def a0(key):
if key == kb.Key.f12:
a1()
# 控制键盘
c = kb.Controller()
# 监听键盘
with kb.Listener(on_press=a0):
while True:
pass
settings.py。键位设置。
from pynput import keyboard as kb
# 键位设置
RIGHT = "d"
A = "j"
ACCELERATE = "u"
save1 = kb.Key.shift
save2 = kb.Key.f1
rec = (1272, 106, 1549, 431)