import pyautogui
import keyboard
import time
import random
from PIL import ImageGrab
CHECK_INTERVAL = 1
HP_THRESHOLD = 30
SAFE_ZONE = (100, 100)
ATTACK_KEY = 'f'
POTION_KEY = '1'
def check_hp():
"""通过像素颜色检测血量(需自定义)"""
screenshot = ImageGrab.grab(bbox=(50, 50, 200, 60))
red_pixels = sum(1 for pixel in screenshot.getdata() if pixel[0] > 200)
return red_pixels / (screenshot.width * screenshot.height) * 100
def attack():
"""执行攻击动作"""
pyautogui.press(ATTACK_KEY)
time.sleep(random.uniform(0.1, 0.3))
def use_potion():
"""使用血瓶"""
pyautogui.press(POTION_KEY)
print("使用血瓶")
def auto_loot():
"""自动拾取(需根据游戏调整按键)"""
pyautogui.press('e')
time.sleep(0.5)
def emergency_escape():
"""紧急脱险"""
pyautogui.keyDown('shift')
pyautogui.press('s')
time.sleep(2)
pyautogui.keyUp('shift')
pyautogui.click(SAFE_ZONE)
def main_loop():
try:
while True:
if keyboard.is_pressed('F12'):
print("脚本终止")
break
current_hp = check_hp()
if current_hp < HP_THRESHOLD:
use_potion()
emergency_escape()
attack()
if random.random() < 0.3:
auto_loot()
if random.random() < 0.2:
pyautogui.moveRel(random.randint(-10,10), random.randint(-10,10))
time.sleep(CHECK_INTERVAL + random.uniform(-0.2, 0.2))
except Exception as e:
print(f"发生错误: {str(e)}")
if __name__ == "__main__":
print("挂机脚本启动(按F12终止)")
time.sleep(3)
main_loop()