Python小脚本开完美国际斗兽袋子

Python小脚本开完美国际斗兽袋子


《完美国际》,使用Python自动自动开家园物品【凌云斗兽袋】。懒人福音。

粗版

将物品【凌云斗兽袋】放按键6上,然后每间隔5秒自动按6一次。
下载包pip insatll pyautogui,python代码如下:

完整代码

import time
import pyautogui

while True:
	pyautogui.press('6')
	time.sleep(5)

直接运行即可。

优化

  • 间隔5秒太长,冷却好了立马开下一个礼包。
  • 开完所有袋子后自动停止

思路

  1. 判断按键6是否存在物品,无物品结束;
  2. 判断是否开了一个礼盒在等冷却;
  3. 循环以上。

方法

使用图像识别判断相关部分。

完整代码

下载相关库pip install pyautogui opencv-python pyscreeze Pillow numpy

import time
import pyautogui
import cv2
import pygetwindow as gw
import numpy as np
import logging


logging.basicConfig(
    level=logging.INFO,
    format='[%(asctime)s] - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('log.txt', encoding='utf-8'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)



def get_xy(img_model_path, account, threashold=0.8):
    """
    :param img_model_path:用来检测的图片
    :return:以元组形式返回检测到的区域中心的坐标
    """
    # 获取窗口大小位置
    window = gw.getWindowsWithTitle(f'{account}')[0]  # 替换为实际窗口标题
    window.activate()  # 激活窗口以确保它在前台
    window.restore()  # 如果窗口最小化,恢复窗口
    # time.sleep(0.5)
    pyautogui.screenshot(region=(window.left, window.top, window.width, window.height)).save("./screenshot.png")  # 截图
    all_img = cv2.imread("./screenshot.png")  # 待读取图像
    origin_img = cv2.imread(f"{img_model_path}")  # 模板图像
    all_img = cv2.cvtColor(all_img, cv2.COLOR_BGR2GRAY)
    origin_img = cv2.cvtColor(origin_img, cv2.COLOR_BGR2GRAY)
    match = cv2.matchTemplate(all_img, origin_img, cv2.TM_CCOEFF_NORMED)  # 开始匹配

    threshold = threashold  # 设置阈值
    loc = np.where(match >= threshold)
    has_match = len(loc[0]) > 0
    return has_match


def main(account):

    while True:
        wait = get_xy(r"res\task.png", f"{account}")
        if wait:
            logger.info("等待")
            continue
        xy = get_xy(r"res\6.png", f"{account}")
        if xy:
            logger.info("有家园礼包,开始开礼包")
            pyautogui.press('6')
            time.sleep(0.5)
            continue
        logger.info("开完,结束")
        return


if __name__ == '__main__':
    account = "角色名"
    main(account)
    # print()

项目结构:在这里插入图片描述
资源文件:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值