Python 自动登录某游戏平台及游戏自动登录

某游戏平台及自动登录功能

以下原代码为本人原创,仅供交流使用,不做任何其他功能,如作他用后果自负

import time
from os import getcwd
from xunlu1_1 import ImgPro, KeyBord
from subprocess import Popen

USER = '123456789'
PASSWOED = '......'
WeGame_path = 'E:\Program Files (x86)\WeGame\wegame.exe'


class DengLu():

    def __init__(self, WeGame_path, opt='DNF'):
        self.WeGame_path = WeGame_path
        self.g = ImgPro()  # 运行前需要获得句柄参数,否则无法截图  图片处理工具类
        self.key = KeyBord()  # 配置参数port   键鼠模拟工具类
        self.g.play_name = "WeGame"
        self.J_PATH = getcwd() + '/'  # 绝对路径
        self.opt = opt

    def __del__(self):
        print('模拟键盘关闭')
        self.key.close()

    # 打开Wegame
    def open_wegame(self, We=None):
        if We is None:
            Popen(self.WeGame_path)
        for i in range(10):
            time.sleep(0.5)
            if self.g.get_handle():
                self.g.set_window()
                # print("已打开界面")
                return True
        print("打开界面失败")

    # 输入
    def __shuru(self):
        for i in range(10):
            time.sleep(0.5)
            try:
                log_gin = self.g.temp_late(self.J_PATH + 'img_page/denglu_img/01.png', xishu=0.85)
                if log_gin:
                    self.key.click_left(log_gin[0] + 100, log_gin[1] + 186)  # 账号位置186
                    for i in range(13):
                        self.key.down_up("backspace")
                    self.key.down_up(USER)
                    self.key.click_left(log_gin[0] + 100, log_gin[1] + 222)  # 密码位置222
                    time.sleep(1)
                    self.key.down_up(PASSWOED)
                    self.key.click_left(log_gin[0] + 100, log_gin[1] + 320)  # 确认登录位置320
                    print('输入账号登录成功')
                    return True
            except:
                print('输入账号错误次数:', i)
        print("输入账号失败...")

    # Wegame选择游戏
    def __opt_game(self):
        row = 200
        if self.opt == 'LOL':
            row = row + 50
        for i in range(10):
            time.sleep(0.5)
            try:
                if not self.open_wegame(We=True):
                    break
                log_gin = self.g.temp_late(self.J_PATH + 'img_page/denglu_img/02.png', xishu=0.85)
                if log_gin:
                    self.key.click_left(log_gin[0] + 350, log_gin[1])
                    time.sleep(0.8)
                    self.key.click_left(log_gin[0] + 30, log_gin[1] + row)  # 第一个游戏
                    time.sleep(1)
                    print("WeGame选择游戏成功")
                    return True
            except:
                print('WeGame登录错误次数:', i)
        print("选择游戏错误")

    # 启动游戏
    def __qidong(self):
        for i in range(10):
            time.sleep(0.5)
            try:
                log_gin = self.g.temp_late(self.J_PATH + 'img_page/denglu_img/03.png', xishu=0.85)
                if log_gin:
                    self.key.click_left(log_gin)
                    print("登录中")
                    return True
            except:
                print('识别启动失败次数:', i)
        print("启动游戏点击失败")

    # 是否完成登录
    def __is_login(self):
        self.g.play_name = '地下城与勇士'
        st1 = time.time()
        while True:
            time.sleep(1)
            if time.time() - st1 > 60:
                print('登录超时....')
                return
            if self.g.get_handle():
                print("运行DNF成功")
                return True

    # DNF选择角色
    def opt_hero(self):
        if self.__is_login():
            for i in range(31):
                time.sleep(2)
                print('登陆中', i)
                try:
                    log_gin = self.g.temp_late(self.J_PATH + 'img_page/denglu_img/04.png', xishu=0.93)
                    if log_gin:
                        try:
                            self.g.set_window()
                        except:
                            continue
                        self.key.click_left(log_gin)
                        print("选择常用角色")
                        time.sleep(1)
                        self.key.click_left(log_gin)
                        time.sleep(3)
                        self.key.down_up('esc')
                        return True
                except:
                    print('识别选择角色失败次数:', i)
            print("选择角色失败")

    # 登录游戏
    def run_denglu(self):
        if self.open_wegame():
            self.__shuru()
        if self.__opt_game():
            self.__qidong()
            self.opt_hero()

    # 重新启动游戏
    def run_chongqi(self):
        self.open_wegame()
        if self.__opt_game():
            pass
            self.__qidong()
            self.opt_hero()

    # 登录WEgame
    def Wegame_denglu(self):
        if self.open_wegame():
            self.__shuru()
            self.__opt_game()


if __name__ == '__main__':
    # o = DengLu(WeGame_path, opt='LOL')
    o = DengLu(WeGame_path)

其中的ImgPro, KeyBord为自己封装的图片处理识别,键盘模拟库

你可能感兴趣的:(python,python)