python - 游戏脚本之无脑的循环挑战

三国挂机手游

  1. 模拟点按并循环,在指定位置截图
# coding: utf-8

import os
import time
import wda
import random
#import pytesseract
from PIL import Image, ImageDraw

wdaClient = wda.Client()
wdaClientSession = wdaClient.session()

screenshot_historyLog_dir = 'historyLog/'
if not os.path.isdir(screenshot_historyLog_dir):
    os.mkdir(screenshot_historyLog_dir)

def log_screenshot(ts):
    wdaClient.screenshot("godlike.png")
    image = Image.open("./godlike.png")

def main():
    i = 1
    while True:
        
        #当前时间
        ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        
        #挑战
        wdaClientSession.tap(200, 549)
        time.sleep(0.1)
        #跳过
        wdaClientSession.tap(200, 647)
        time.sleep(0.1)
        
        #把挑战数据h截图保存下来
        log_screenshot(ts)
        
        #关闭
        wdaClientSession.tap(257, 581)
        time.sleep(0.1)
        #购买次数 
        if (i%13) == 0:
            wdaClientSession.tap(277, 400)
        
        print i,'   ',ts
        i += 1

if __name__ == '__main__':
    main()
  1. 基于截图进行筛选,删除不必要的图片,保留有价值的数据
# coding: utf-8

import os
import time
import wda
import random
import pytesseract
from PIL import Image, ImageDraw, ImageEnhance
import shutil
import string
import re

x = 170
y = 905
w = 217
h = 50

screenshot_historyLog_dir = 'historyLog/'
if not os.path.isdir(screenshot_historyLog_dir):
    os.mkdir(screenshot_historyLog_dir)

filterLog_dir = 'filter/'
if not os.path.isdir(filterLog_dir):
    os.mkdir(filterLog_dir)

unknowLog_dir = 'unknow2/'
if not os.path.isdir(unknowLog_dir):
    os.mkdir(unknowLog_dir)

def initTable(threshold=77):#43
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    return table

def log_screenshot(file):
    imageName = '{}/{}'.format(screenshot_historyLog_dir, file)
    regionName = "./godlikeRegion.png";
    image = Image.open(imageName)
    region = image.crop((x, y, x+w, y+h))
    region.save(regionName)
    imL = region.convert('L')
    imL.save(regionName)
    binaryImage = imL.point(initTable(), '1')
    binaryImage.save(regionName)
    text = pytesseract.image_to_string(binaryImage,lang='chi_sim')
    #清除空格
    text = ''.join(text.split())
    print "识别数据 - ",text
    if text == u"战斗至最后":
        shutil.move(imageName,'{}{}'.format(filterLog_dir, file))
    elif text == "":
        print "will remove file " + imageName
        os.remove(imageName)
    else:
        textNum = re.sub('\D','',text)
        if textNum == "": #未能识别
            os.remove(imageName)
#            shutil.move(imageName,'{}{}'.format(unknowLog_dir, file))
            return;
        intNum  = int(textNum)
        if intNum > 44 :
            print "will move file " + text
            os.remove(imageName)
#            shutil.move(imageName,'{}{}'.format(filterLog_dir, file))
        else :
            print "will remove file " + imageName
            os.remove(imageName)

def main():
    
    while True:
        for file in os.listdir(screenshot_historyLog_dir):
            if os.path.splitext(file)[-1] == ".png":
                log_screenshot(file)
        time.sleep(52)

if __name__ == '__main__':
    main()

你可能感兴趣的:(python - 游戏脚本之无脑的循环挑战)