仅用于学习python
pyautogui和os库,MUMU模拟器,背单词软件为乐词
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()