基于pyautogui的自动背单词脚本

仅用于学习python

pyautogui和os库,MUMU模拟器,背单词软件为乐词

运行效果B站
pyautogui库文档
在这里插入图片描述

import pyautogui as pag
import os
import time


class LoopException(Exception):
    pass


# 在模拟器中打开app
def open_app(image):
    s_time = time.time()
    while pag.locateOnScreen(image, grayscale=True, confidence=0.9) is None:
        pag.PAUSE = 1
        e_time = time.time()
        if e_time - s_time > 60:
            raise LoopException("未找到App")
    button = pag.locateOnScreen(image, grayscale=True, confidence=0.9)
    pag.click(button)
    print("App已经打开")


# 点击开始学词
def start_learn(image):
    s_time = time.time()
    while pag.locateOnScreen(image, grayscale=True, confidence=0.9) is None:
        pag.PAUSE = 1
        e_time = time.time()
        if e_time - s_time > 60:
            raise LoopException("未找到Go去学词")
    button = pag.locateOnScreen(image, grayscale=True, confidence=0.9)
    pag.click(button)

    if pag.locateOnScreen(image, grayscale=True, confidence=0.9) is not None:
        start_learn(image)
    else:
        print("开始背词")
        pag.PAUSE = 3


# 开始复习
def review(image):
    button = pag.locateOnScreen(image, confidence=0.9)
    while button is not None:
        pag.click(button)
        button = pag.locateOnScreen(image, confidence=0.9)
        pag.PAUSE = 1
    print("已经复习完")


# 开始学习新词
def learn(image_know, image_test, num):
    j = 0
    while j < num:
        # 判断按钮是否出现,因为软件可能有延迟
        while pag.locateOnScreen(image_know, grayscale=True, confidence=0.9) is None:
            pag.PAUSE = 1
            print("未找到“认识”")
        # 出现之后,锁定这个按钮的位置
        button = pag.locateOnScreen(image_know, grayscale=True, confidence=0.9)
        while button is not None:
            pag.click(button)
            button = pag.locateOnScreen(image_know, grayscale=True, confidence=0.9)
            pag.PAUSE = 1
        # 当这个按钮消失,进入到测试环节
        # 判断按钮是否出现
        while pag.locateOnScreen(image_test, grayscale=True, confidence=0.9) is None:
            pag.PAUSE = 1
            print("未找到“开始测试”")
        # 出现了之后点击开始测试
        button_test = pag.locateOnScreen(image_test, grayscale=True, confidence=0.9)
        pag.click(button_test)
        j += 1
        print("当前学习第", j, "组")
    print("已经学完")


# main
def main():

    learnGroup = 12

    app_dir = r'C:\Program Files (x86)\MuMu\emulator\nemu\EmulatorShell\NemuPlayer.exe'
    # 打开模拟器,需要管理员启动ide
    os.startfile(app_dir)

    # 导入按钮的图片
    image_App = "Button/App.png"
    image_StartLearn = "Button/StartLearn.png"
    image_StartReview = "Button/StartReview.png"
    image_Remember = "Button/Remember.png"
    image_LearnNewWords = "Button/LearnNewWords.png"
    image_Know = "Button/Know.png"
    image_StartTest = "Button/StartTest.png"

    try:
        open_app(image_App)
        start_learn(image_StartLearn)
        if pag.locateOnScreen(image_StartReview, grayscale=True, confidence=0.9) is not None:  # 判断是否有单词需要复习
            button_StartReview = pag.locateOnScreen(image_StartReview, grayscale=True, confidence=0.9)
            pag.click(button_StartReview)
            print("开始复习")
            review(image_Remember)
            while pag.locateOnScreen(image_LearnNewWords, grayscale=True, confidence=0.9) is None:  # 复习完成开始学习新单词
                pag.PAUSE = 1
            button_learn_new_words = pag.locateOnScreen(image_LearnNewWords, grayscale=True, confidence=0.9)
            pag.click(button_learn_new_words)
            learn(image_Know, image_StartTest, int(learnGroup))
        else:
            learn(image_Know, image_StartTest, int(learnGroup))
    except LoopException as error:
        print(error)
    finally:
        main()


# 运行
if __name__ == '__main__':
    main()


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值