编辑代码

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')  # 假设e键拾取
    time.sleep(0.5)

def emergency_escape():
    """紧急脱险"""
    pyautogui.keyDown('shift')  # 假设shift加速跑
    pyautogui.press('s')
    time.sleep(2)
    pyautogui.keyUp('shift')
    pyautogui.click(SAFE_ZONE)

# ===== 主循环 =====
def main_loop():
    try:
        while True:
            # 检测退出快捷键(F12终止)
            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:  # 30%概率拾取
                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()