文章目录
- 原理
- 全部代码
原理
没有新东西。
安全区选在了这里,上面是洞口,下面是治疗。
因为逛的区域大了点,这次用跑的。(好像之前也能跑)
def chu_qu(): # 右逛
c.press(B)
press(UP, 0.4)
c.release(B)
def hui_qu(): # 左逛
c.press(B)
press(DOWN, 0.4)
c.release(B)
全部代码
main.py
from PIL import ImageGrab
from cai_yang import is_same_img
from core import *
# 脚本核心
def a1():
global state_can_attack
if is_same_img(ImageGrab.grab(), data_safe): # 在安全区
if state_can_attack: # 去战斗
chu_qu()
else: # 去治疗
zhi_liao()
state_can_attack = True
elif ImageGrab.grab().getpixel(data_attack[0]) == data_attack[1]: # 在战斗
if is_same_img(ImageGrab.grab(), data_pp): # 没PP
state_can_attack = False
if state_can_attack == False: # 逃跑
tao_pao()
else: # 战斗
zhan_dou()
else: # 战斗外
hui_qu()
if __name__ == '__main__':
# 键盘事件处理
def a0(key):
if key == kb.Key.f12:
global state12
state12 = not state12
print(f"自动练级:{state12}")
# 模拟器加速
if state12:
c.press(ACCELERATE)
else:
c.release(ACCELERATE)
# 开关
state12 = False
# 是否能够战斗
state_can_attack = True
# 监听键盘
with kb.Listener(on_press=a0):
while True:
if state12:
a1()
core.py
import time
from pynput import keyboard as kb
from settings import *
c = kb.Controller()
def press(key, interval=0.2, interval2=0.1): # 按一下
c.press(key)
time.sleep(interval)
c.release(key)
time.sleep(interval2)
def walk(key): # 走一步
c.release(ACCELERATE)
press(key)
c.press(ACCELERATE)
def zhi_liao(): # 去治疗
walk(RIGHT)
walk(DOWN)
walk(DOWN)
press(RIGHT, 0.3)
walk(LEFT)
press(UP, 0.4)
for i in [1, 2, 3, 4]:
press(A)
for i in [1, 2, 3, 4]:
press(B)
press(DOWN, 0.4)
walk(UP)
walk(UP)
walk(UP)
press(LEFT, 0.4)
walk(UP)
walk(UP)
walk(LEFT)
def tao_pao(): # 逃跑
for i in [1, 2, 3]:
press(B)
press(DOWN)
press(RIGHT)
press(A)
for i in [1, 2, 3]:
press(B)
def chu_qu(): # 右逛
c.press(B)
press(UP, 0.4)
c.release(B)
def hui_qu(): # 左逛
c.press(B)
press(DOWN, 0.4)
c.release(B)
def zhan_dou(): # 战斗
press(A)
settings.py
from PIL import Image
from cai_yang import cai_yang
# 图片采样数据
data_safe = cai_yang(Image.open("安全区1.png"), (911, 717, 1019, 804), 12)
data_pp = cai_yang(Image.open("没有PP.png"), (110, 815, 722, 1018), 12)
# 像素数据
data_attack = ((1084, 301), (112, 96, 32))
# 键位设置
UP = "w"
DOWN = "s"
LEFT = "a"
RIGHT = "d"
ACCELERATE = "u"
A = "j"
B = "k"
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