defina脚本制作

文章目录


前言

本脚本使用python来实现,先准备需要的库

import time
import datetime
import cv2   
from matplotlib import pyplot as plt
import pyautogui
import tkinter as tk
import threading
import traceback
from PIL import Image,ImageTk

1.代码块

代码如下(示例):

class wakuang():

    def __init__(self):

        self.caikuang = 'caikuang.png'
        self.zhencha = 'zhencha.png'
        self.zijin = 'zijin.png'
        self.lanjing = 'lanjing.png'
        self.dinong = 'dinong.png'
        self.gaonong = 'gaonong.png'
        self.lanjingkuang = 'lanjingkuang.png'
        self.confirm_button = 'confirm_button.png'
        self.myminers = 'myminers.png'
        self.protection = 'protection.png'
        self.zhiya = 'zhiya.png'
        self.queding = 'queding.png'
        self.tuichu = 'x.png'
        self.enemy = 'enemy.png'
        self.fanhui = 'fanhui.png'
        self.page1 = '1.png'
        self.drink = 'drink.png'
        self.usg = 'usg.png'
        self.backage = 'backage.png'
        self.minerinfo = 'minerinfo.png'
        self.zijinkuang = 'zijinkuang.png'
        self.gaononglanjingkuang = 'gaononglanjingkuang.png'
        self.dinonglanjingkuang = 'dinonglanjingkuang.png'
        self.break_program = False
        self.shouhuo = 'shouhuo.png'
        self.fail = 'fail.png'
        self.victory = 'victory.png'
        self.receive = 'receive.png'


    def read(self,picture):

        # 读入图片 big1.png是背景大图; small.png是需要寻找的小图(格式.jpg .png都行)
        img = cv2.imread("current.png",0) # 0 读入灰度图
        img3 = cv2.imread("current.png",1) # 1 读入彩色图
        img2 = img.copy()
        template = cv2.imread(picture,0)
        w, h = template.shape[::-1]
        # print(w,h)

        # 6种算法的列表
        methods = 'cv2.TM_CCOEFF_NORMED'
        img = img2.copy()
        method = eval(methods)

        # 应用模板算法,返回一系列指标
        res = cv2.matchTemplate(img,template,method)
        # 从res中挑选最优指标
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        print(max_val)
        if max_val > 0.7:
            # 注意 TM_SQDIFF 或者 TM_SQDIFF_NORMED 算法使用最小值为最优
            top_left = max_loc
            bottom_right = (top_left[0] + w/2, top_left[1] + h/2)
            # print(bottom_right)
            return bottom_right
        else:
            return None
        # 显示图片
        # cv2.rectangle(img3,top_left, bottom_right, (0,0,255), 2) # 画出矩形框
        # plt.axis('off') # 关闭坐标
        # img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB) # 颜色转换
        # plt.subplot(121),plt.imshow(res,cmap = 'gray')
        # plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        # plt.subplot(122),plt.imshow(img3)
        # plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        # plt.suptitle(meth)
        # plt.show()
    def compare(self,picture,current_png):
        # 读入图片 big1.png是背景大图; small.png是需要寻找的小图(格式.jpg .png都行)
        img = cv2.imread(current_png, 0)  # 0 读入灰度图
        img2 = img.copy()
        template = cv2.imread(picture, 0)
        methods = 'cv2.TM_CCOEFF_NORMED'
        img = img2.copy()
        method = eval(methods)
        # 应用模板算法,返回一系列指标
        res = cv2.matchTemplate(img, template, method)
        # 从res中挑选最优指标
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        return max_val

    def run(self,value,value1,value2):

        time.sleep(2)
        pyautogui.PAUSE = 0.5
        pyautogui.FAILSAFE = True
        #进入游戏界面截图,点击采矿模式
        current_screen = pyautogui.screenshot()
        current_screen.save('current.png')
        wakuang = self.read(picture=self.caikuang)
        if wakuang is not None:
            pyautogui.click(wakuang)
            time.sleep(1)
        else:
            print('找不到采矿按钮')

        list_pools = {'zijin': '紫金矿', 'lanjing': '蓝晶矿', 'dinong': '低浓蓝晶矿', 'gaonong': '高浓蓝晶矿'}
        kuang = list(list_pools.keys())[list(list_pools.values()).index(value)]
        kuang = kuang + '.png'
        list_time = []
        count = 0



        while True:
            if self.break_program == True:
                print('停止运行')
                break
            try:
                # 点击侦查
                time.sleep(1)
                if value2 == 1:
                    self.protect(lenth=int(value1))

                #质押收矿
                self.miners(lenth=int(value1))
                #选矿
                self.selection(kuang=kuang)
                current_screen = pyautogui.screenshot()
                current_screen.save('current1.png')
                current_screen.save('current.png')
                enemy = self.read(picture=self.enemy)
                if enemy is not None:
                    hero = (enemy[0] + 40, enemy[1] + 500)
                    # print('hero location is {}'.format(hero))
                    hero_list = [hero]
                    # 一组英雄
                    if value1 == '1':
                        time.sleep(1)
                        for i in range(6):
                            hero = (hero[0] + 122, hero[1])
                            hero_list.append(hero)
                        # print('hero list location is {} '.format(hero_list))
                        for hero in hero_list:
                            pyautogui.click(hero)
                            time.sleep(1)
                        # 开始战斗
                        fighting = self.read(picture='fighting.png')
                        pyautogui.click(fighting)
                        time.sleep(1)
                        #确认是否进入了战斗画面
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current2.png')
                        compare_result1 = float(self.compare(picture='fighting.png',current_png='current2.png'))
                        compare_result2 = float(self.compare(picture='fightingscreen.png',current_png='current2.png'))

                        if compare_result1 < compare_result2:
                            print('进入战斗画面')
                            #等待战斗结束
                            time.sleep(30)
                            while True:
                                current_screen = pyautogui.screenshot()
                                current_screen.save('current.png')
                                victory = float(self.compare(picture=self.victory,current_png='current.png'))
                                fail = float(self.compare(picture=self.fail,current_png='current.png'))

                                if victory > fail and victory > 0.7:
                                    print('确认胜利')
                                    start = time.time()
                                    end = start + float(10800)
                                    print('开始计时')

                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)

                                    break

                                if fail > victory and fail > 0.7:
                                    print('确认攻击失败')
                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)
                                    break

                        else:
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            time.sleep(1)
                            fanhui = self.read(picture=self.fanhui)
                            pyautogui.click(fanhui)
                            time.sleep(1)


                    # 二组英雄
                    if value1 == '2':
                        time.sleep(1)
                        for i in range(8):
                            hero = (hero[0] + 122, hero[1])
                            hero_list.append(hero)
                        # print('hero list location is {} '.format(hero_list))
                        #分成两组英雄池
                        list1 = []
                        list2 = []
                        for index, value in enumerate(hero_list):
                            if index % 2 == 0:
                                list1.append(value)
                            else:
                                list2.append(value)

                        for hero in list1:
                            pyautogui.click(hero)
                            time.sleep(1)

                        for hero in list2:
                            pyautogui.click(hero)
                            time.sleep(1)

                        # 开始战斗
                        fighting = self.read(picture='fighting.png')
                        pyautogui.click(fighting)
                        time.sleep(2)
                        #是否进入战斗画面,比较图像得到的值
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current2.png')
                        compare_result1 = float(self.compare(picture='fighting.png', current_png='current2.png'))
                        compare_result2 = float(self.compare(picture='fightingscreen.png', current_png='current2.png'))
                        print(compare_result1, compare_result2)
                        #如果已经进入战斗画面,则等待75秒战斗结束
                        if compare_result1 < compare_result2:
                            time.sleep(30)
                            #确定胜利或失败
                            while True:
                                current_screen = pyautogui.screenshot()
                                current_screen.save('current.png')

                                victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                if victory > fail and victory > 0.7:

                                    start = time.time()
                                    end = start + float(30)
                                    list_time.append(end)
                                    print(list_time)
                                    if count < 2:
                                        count += 1
                                    print('胜利数量{}'.format(count))

                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)

                                    break

                                if fail > victory and fail > 0.7:
                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)
                                    break

                        else:

                            #进入选英雄界面遍历英雄,准备战斗
                            for hero in hero_list:
                                pyautogui.click(hero)
                                time.sleep(1)
                            fighting = self.read(picture='fighting.png')
                            pyautogui.click(fighting)
                            time.sleep(1)
                            # 是否进入战斗画面,比较图像得到的值
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            compare_result1 = float(self.compare(picture='fighting.png', current_png='current.png'))
                            compare_result2 = float(self.compare(picture='fightingscreen.png', current_png='current.png'))
                            print(compare_result1, compare_result2)
                            # 如果已经进入战斗画面,则等待75秒战斗结束
                            if compare_result1 < compare_result2:
                                time.sleep(30)
                                # 确定胜利或失败
                                while True:

                                    current_screen = pyautogui.screenshot()
                                    current_screen.save('current.png')
                                    victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                    fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                    if victory > fail and victory > 0.7:
                                        start = time.time()
                                        end = start + float(30)
                                        list_time.append(end)
                                        print(list_time)
                                        if count < 2:
                                            count += 1
                                        print('胜利数量{}'.format(count))

                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)

                                        break

                                    if fail > victory and fail > 0.7:
                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        break

                            else:
                                current_screen = pyautogui.screenshot()
                                current_screen.save('current.png')
                                time.sleep(1)
                                fanhui = self.read(picture=self.fanhui)
                                pyautogui.click(fanhui)
                                time.sleep(1)


                        time.sleep(1)

                    # 三组英雄
                    if value1 == '3':
                        time.sleep(1)
                        for i in range(1,9):
                            hero1 = (hero[0] + i * 122, hero[1])
                            hero_list.append(hero1)
                        # print('hero list location is {} '.format(hero_list))
                        # 分成三组英雄池
                        list3 = hero_list[0:3]
                        list4 = hero_list[3:6]
                        list5 = hero_list[6:9]

                        for i in range(3):
                            pyautogui.click(list3[i])
                            time.sleep(1)
                            pyautogui.click(list4[i])
                            time.sleep(1)
                            pyautogui.click(list5[i])
                            time.sleep(1)
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            fighting_button = self.read(picture='fighting.png')
                            # 移动英雄池第1次
                            pyautogui.moveTo(fighting_button[0] - 100, fighting_button[1], duration=2)
                            # 将鼠标拖动到指定位置
                            pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                            time.sleep(2)
                            list6 = hero_list[1:7]
                            for item in list6:
                                pyautogui.click(item)
                                time.sleep(1)
                            # 开始战斗
                            fighting = self.read(picture='fighting.png')
                            pyautogui.click(fighting)
                            time.sleep(2)
                            # 是否进入战斗画面,比较图像得到的值
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current2.png')
                            compare_result1 = float(
                                self.compare(picture='fighting.png', current_png='current2.png'))
                            compare_result2 = float(
                                self.compare(picture='fightingscreen.png', current_png='current2.png'))
                            print(compare_result1, compare_result2)
                            # 如果已经进入战斗画面,则等待75秒战斗结束
                            if compare_result1 < compare_result2:
                                time.sleep(30)
                                # 确定胜利或失败
                                while True:
                                    current_screen = pyautogui.screenshot()
                                    current_screen.save('current.png')

                                    victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                    fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                    if victory > fail and victory > 0.7:

                                        start = time.time()
                                        end = start + float(30)
                                        list_time.append(end)
                                        print(list_time)
                                        if count < 3:
                                            count += 1
                                        print('胜利数量{}'.format(count))

                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)

                                        break

                                    if fail > victory and fail > 0.7:
                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)
                                        break

                                time.sleep(2)

                            else:
                                # 移动英雄池回原来位置
                                print('英雄坐标{}'.format(hero))
                                pyautogui.moveTo(hero[0],hero[1], duration=2)
                                # 将鼠标拖动到指定位置
                                pyautogui.dragTo(x=fighting_button[0] - 100, y=fighting_button[1], duration=2)
                                time.sleep(2)

                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        time.sleep(1)
                        fanhui = self.read(picture=self.fanhui)
                        pyautogui.click(fanhui)

                        time.sleep(1)

                    # 四组英雄
                    if value1 == '4':
                        time.sleep(1)
                        for i in range(1, 9):
                            hero1 = (hero[0] + i * 122, hero[1])
                            hero_list.append(hero1)
                        # print('hero list location is {} '.format(hero_list))
                        # 分成两组英雄池
                        list6 = hero_list[0:4]
                        list7 = hero_list[4:8]


                        for i in range(4):
                            pyautogui.click(list6[i])
                            time.sleep(1)
                            pyautogui.click(list7[i])
                            time.sleep(1)

                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            fighting_button = self.read(picture='fighting.png')
                            # 移动英雄池第1次
                            pyautogui.moveTo(fighting_button[0] - 100, fighting_button[1], duration=2)
                            # 将鼠标拖动到指定位置
                            pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                            time.sleep(2)

                            pyautogui.click(list6[i])
                            time.sleep(1)
                            pyautogui.click(list7[i])
                            time.sleep(1)
                            # 开始战斗
                            fighting = self.read(picture='fighting.png')
                            pyautogui.click(fighting)
                            time.sleep(2)
                            # 是否进入战斗画面,比较图像得到的值
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current2.png')
                            compare_result1 = float(
                                self.compare(picture='fighting.png', current_png='current2.png'))
                            compare_result2 = float(
                                self.compare(picture='fightingscreen.png', current_png='current2.png'))
                            print(compare_result1, compare_result2)
                            # 如果已经进入战斗画面,则等待75秒战斗结束
                            if compare_result1 < compare_result2:
                                time.sleep(30)
                                # 确定胜利或失败
                                while True:
                                    current_screen = pyautogui.screenshot()
                                    current_screen.save('current.png')

                                    victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                    fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                    if victory > fail and victory > 0.7:

                                        start = time.time()
                                        end = start + float(30)
                                        list_time.append(end)
                                        print(list_time)
                                        if count < 4:
                                            count += 1
                                        print('胜利数量{}'.format(count))

                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)
                                        break

                                    if fail > victory and fail > 0.7:
                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)
                                        break

                                time.sleep(2)

                            else:
                                # 移动英雄池回原来位置
                                print('英雄坐标{}'.format(hero))
                                pyautogui.moveTo(hero[0], hero[1], duration=2)
                                # 将鼠标拖动到指定位置
                                pyautogui.dragTo(x=fighting_button[0] - 100, y=fighting_button[1], duration=2)
                                time.sleep(2)

                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        time.sleep(1)
                        fanhui = self.read(picture=self.fanhui)
                        pyautogui.click(fanhui)
                        time.sleep(1)

                    # 五组英雄
                    if value1 == '5':
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        fighting_button = self.read(picture='fighting.png')
                        time.sleep(1)
                        for i in range(1, 9):
                            hero1 = (hero[0] + i * 122, hero[1])
                            hero_list.append(hero1)
                        # print('hero list location is {} '.format(hero_list))
                        # 分成两组英雄池
                        list8 = hero_list[0:4]
                        list9 = hero_list[5:9]
                        page1 = hero_list[1:5]
                        page2 = hero_list[5:9]

                        for i in range(4):
                            pyautogui.click(list8[i])
                            time.sleep(1)
                            pyautogui.click(list9[i])
                            time.sleep(1)
                            # 移动英雄池第1次
                            pyautogui.moveTo(fighting_button[0] - 100, fighting_button[1], duration=2)
                            # 将鼠标拖动到指定位置
                            pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                            time.sleep(2)

                            pyautogui.click(page1[i])
                            time.sleep(1)
                            pyautogui.click(page2[i])
                            time.sleep(1)
                            # 开始战斗
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            fighting = self.read(picture='fighting.png')
                            pyautogui.click(fighting)
                            time.sleep(2)
                            # 是否进入战斗画面,比较图像得到的值
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current2.png')
                            compare_result1 = float(
                                self.compare(picture='fighting.png', current_png='current2.png'))
                            compare_result2 = float(
                                self.compare(picture='fightingscreen.png', current_png='current2.png'))
                            print(compare_result1, compare_result2)
                            # 如果已经进入战斗画面,则等待75秒战斗结束
                            if compare_result1 < compare_result2:
                                time.sleep(30)
                                # 确定胜利或失败
                                while True:
                                    current_screen = pyautogui.screenshot()
                                    current_screen.save('current.png')

                                    victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                    fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                    if victory > fail and victory > 0.7:

                                        start = time.time()
                                        end = start + float(30)
                                        list_time.append(end)
                                        print(list_time)
                                        if count < 4:
                                            count += 1
                                        print('胜利数量{}'.format(count))

                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)
                                        break

                                    if fail > victory and fail > 0.7:
                                        confirm_option = self.read(picture=self.confirm_button)
                                        pyautogui.click(confirm_option)
                                        time.sleep(1)
                                        self.selection(kuang=kuang)
                                        break

                                time.sleep(2)

                            else:
                                # 移动英雄池回原来位置
                                print('英雄坐标{}'.format(hero))
                                pyautogui.moveTo(hero[0], hero[1], duration=2)
                                # 将鼠标拖动到指定位置
                                pyautogui.dragTo(x=fighting_button[0] - 100, y=fighting_button[1], duration=2)
                                time.sleep(2)

                        i = 4
                        hero1 = (hero[0] + i * 122, hero[1])
                        pyautogui.click(hero1)
                        time.sleep(1)

                        for i in range(2):
                            # 移动英雄池两次
                            pyautogui.moveTo(fighting_button[0] - 100, fighting_button[1], duration=2)
                            # 将鼠标拖动到指定位置
                            pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                            time.sleep(2)
                        for i in range(6,9):
                            hero1 = (hero[0] + i * 122, hero[1])
                            pyautogui.click(hero1)
                            time.sleep(1)

                        # 开始战斗
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        fighting = self.read(picture='fighting.png')
                        pyautogui.click(fighting)
                        time.sleep(2)
                        # 是否进入战斗画面,比较图像得到的值
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current2.png')
                        compare_result1 = float(
                            self.compare(picture='fighting.png', current_png='current2.png'))
                        compare_result2 = float(
                            self.compare(picture='fightingscreen.png', current_png='current2.png'))
                        print(compare_result1, compare_result2)
                        # 如果已经进入战斗画面,则等待75秒战斗结束
                        if compare_result1 < compare_result2:
                            time.sleep(30)
                            # 确定胜利或失败
                            while True:
                                current_screen = pyautogui.screenshot()
                                current_screen.save('current.png')

                                victory = float(self.compare(picture=self.victory, current_png='current.png'))
                                fail = float(self.compare(picture=self.fail, current_png='current.png'))

                                if victory > fail and victory > 0.7:

                                    start = time.time()
                                    end = start + float(30)
                                    list_time.append(end)
                                    print(list_time)
                                    if count < 4:
                                        count += 1
                                    print('胜利数量{}'.format(count))

                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)
                                    self.selection(kuang=kuang)
                                    break

                                if fail > victory and fail > 0.7:
                                    confirm_option = self.read(picture=self.confirm_button)
                                    pyautogui.click(confirm_option)
                                    time.sleep(1)
                                    self.selection(kuang=kuang)
                                    break

                            time.sleep(2)

                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        time.sleep(1)
                        fanhui = self.read(picture=self.fanhui)
                        pyautogui.click(fanhui)
                        time.sleep(1)

                    # # 五组英雄
                    # if value1 == '5':
                    #     time.sleep(1)
                    #     for i in range(3):
                    #         # 分组,选择每组的第一个最厉害的英雄
                    #         hero_button = (hero[0] + i * 122, hero[1])
                    #         pyautogui.click(hero_button)
                    #         time.sleep(2)
                    #         j = i + 5
                    #         hero_button = (hero[0] + j * 122, hero[1])
                    #         pyautogui.click(hero_button)
                    #         time.sleep(2)
                    #
                    #         current_screen = pyautogui.screenshot()
                    #         current_screen.save('current.png')
                    #         fighting_button = self.read(picture='fighting.png')
                    #         # 移动英雄池第1次
                    #
                    #         pyautogui.moveTo(fighting_button[0] - 143 ,fighting_button[1] + 28, duration=2)
                    #         # 将鼠标拖动到指定位置
                    #         pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                    #         time.sleep(2)
                    #
                    #         for j in range(i*2+3,i*2+5):
                    #             hero_button = (hero[0] + j * 122, hero[1])
                    #             pyautogui.click(hero_button)
                    #             time.sleep(2)
                    #
                    #
                    #             # 开始战斗
                    #
                    #             pyautogui.click(fighting_button)
                    #             time.sleep(2)
                    #             # 是否进入战斗画面,比较图像得到的值
                    #             current_screen = pyautogui.screenshot()
                    #             current_screen.save('current2.png')
                    #             compare_result1 = float(self.compare(picture='fighting.png', current_png='current2.png'))
                    #             compare_result2 = float(
                    #                 self.compare(picture='fightingscreen.png', current_png='current2.png'))
                    #             print(compare_result1, compare_result2)
                    #             # 如果已经进入战斗画面,则等待75秒战斗结束
                    #             if compare_result1 < compare_result2:
                    #                 time.sleep(30)
                    #                 # 确定胜利或失败
                    #                 while True:
                    #                     current_screen = pyautogui.screenshot()
                    #                     current_screen.save('current.png')
                    #
                    #                     victory = float(self.compare(picture=self.victory, current_png='current.png'))
                    #                     fail = float(self.compare(picture=self.fail, current_png='current.png'))
                    #
                    #                     if victory > fail and victory > 0.7:
                    #                         start = time.time()
                    #                         end = start + float(30)
                    #                         list_time.append(end)
                    #                         print(list_time)
                    #                         if count < 5:
                    #                             count += 1
                    #                         print('胜利数量{}'.format(count))
                    #
                    #                         confirm_option = self.read(picture=self.confirm_button)
                    #                         pyautogui.click(confirm_option)
                    #                         time.sleep(1)
                    #
                    #                         #选矿
                    #                         self.selection(kuang=kuang)
                    #                         break
                    #                     #打不过
                    #                     if fail > victory and fail > 0.7:
                    #                         confirm_option = self.read(picture=self.confirm_button)
                    #                         pyautogui.click(confirm_option)
                    #                         time.sleep(1)
                    #                         # 选矿
                    #                         self.selection(kuang=kuang)
                    #                         break
                    #
                    #
                    #             else:
                    #                 # 移动英雄池回原来位置
                    #                 print('英雄坐标{}'.format(hero))
                    #                 pyautogui.moveTo(hero[0], hero[1], duration=2)
                    #                 # 将鼠标拖动到指定位置
                    #                 pyautogui.dragTo(x=fighting_button[0] - 143, y=fighting_button[1] + 28, duration=2)
                    #                 time.sleep(2)
                    #                 # 第四组英雄池
                    #                 i = 3
                    #                 # 分组,选择每组的第一个最厉害的英雄
                    #                 hero_button = (hero[0] + i * 122, hero[1])
                    #                 pyautogui.click(hero_button)
                    #                 time.sleep(2)
                    #                 j = 8
                    #                 hero_button = (hero[0] + j * 122, hero[1])
                    #                 pyautogui.click(hero_button)
                    #                 time.sleep(2)
                    #                 current_screen = pyautogui.screenshot()
                    #                 current_screen.save('current.png')
                    #                 fighting_button = self.read(picture='fighting.png')
                    #
                    #                 # 移动英雄池第1#                 pyautogui.moveTo(fighting_button[0] - 143, fighting_button[1] + 28, duration=2)
                    #                 # 将鼠标拖动到指定位置
                    #                 pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                    #                 time.sleep(2)
                    #
                    #                 # 移动英雄池第2#                 pyautogui.moveTo(fighting_button[0] - 143, fighting_button[1] + 28, duration=2)
                    #                 # 将鼠标拖动到指定位置
                    #                 pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                    #                 time.sleep(2)
                    #                 for i in range(6, 8):
                    #                     hero_button = (hero[0] + i * 122, hero[1])
                    #                     pyautogui.click(hero_button)
                    #                     time.sleep(2)
                    #
                    #                 # 开始战斗
                    #
                    #                 pyautogui.click(fighting_button)
                    #                 time.sleep(2)
                    #                 # 是否进入战斗画面,比较图像得到的值
                    #                 current_screen = pyautogui.screenshot()
                    #                 current_screen.save('current2.png')
                    #                 compare_result1 = float(
                    #                     self.compare(picture='fighting.png', current_png='current2.png'))
                    #                 compare_result2 = float(
                    #                     self.compare(picture='fightingscreen.png', current_png='current2.png'))
                    #                 print(compare_result1, compare_result2)
                    #                 # 如果已经进入战斗画面,则等待75秒战斗结束
                    #                 if compare_result1 < compare_result2:
                    #                     time.sleep(30)
                    #                     # 确定胜利或失败
                    #                     while True:
                    #                         current_screen = pyautogui.screenshot()
                    #                         current_screen.save('current.png')
                    #
                    #                         victory = float(self.compare(picture=self.victory, current_png='current.png'))
                    #                         fail = float(self.compare(picture=self.fail, current_png='current.png'))
                    #
                    #                         if victory > fail and victory > 0.7:
                    #                             start = time.time()
                    #                             end = start + float(30)
                    #                             list_time.append(end)
                    #                             print(list_time)
                    #                             if count < 5:
                    #                                 count += 1
                    #                             print('胜利数量{}'.format(count))
                    #
                    #                             confirm_option = self.read(picture=self.confirm_button)
                    #                             pyautogui.click(confirm_option)
                    #                             time.sleep(1)
                    #                             self.selection(kuang=kuang)
                    #                             break
                    #
                    #                         if fail > victory and fail > 0.7:
                    #                             confirm_option = self.read(picture=self.confirm_button)
                    #                             pyautogui.click(confirm_option)
                    #                             time.sleep(1)
                    #                             self.selection(kuang=kuang)
                    #                             break
                    #                 else:
                    #                     # 移动英雄池回原来位置
                    #
                    #                     pyautogui.moveTo(hero[0], hero[1], duration=2)
                    #                     # 将鼠标拖动到指定位置
                    #                     pyautogui.dragTo(x=fighting_button[0] - 143, y=fighting_button[1] + 28, duration=2)
                    #                     time.sleep(2)
                    #
                    #                     i = 1
                    #                     # 分组,选择每组的第一个最厉害的英雄
                    #                     hero_button = (hero[0] + i * 122, hero[1])
                    #                     pyautogui.click(hero_button)
                    #                     time.sleep(2)
                    #
                    #                     for i in range(6, 8):
                    #                         hero_button = (hero[0] + i * 122, hero[1])
                    #                         pyautogui.click(hero_button)
                    #                         time.sleep(2)
                    #
                    #                     # 移动英雄池第1#                     pyautogui.moveTo(fighting_button[0] - 143, fighting_button[1] + 28, duration=2)
                    #                     # 将鼠标拖动到指定位置
                    #                     pyautogui.dragTo(x=hero[0], y=hero[1], duration=2)
                    #                     time.sleep(2)
                    #
                    #                     for i in range(8, 9):
                    #                         hero_button = (hero[0] + i * 122, hero[1])
                    #                         pyautogui.click(hero_button)
                    #                         time.sleep(2)
                    #
                    #                     # 开始战斗
                    #                     current_screen = pyautogui.screenshot()
                    #                     current_screen.save('current.png')
                    #                     fighting_button = self.read(picture='fighting.png')
                    #                     pyautogui.click(fighting_button)
                    #                     time.sleep(2)
                    #                     # 是否进入战斗画面,比较图像得到的值
                    #                     current_screen = pyautogui.screenshot()
                    #                     current_screen.save('current2.png')
                    #                     compare_result1 = float(
                    #                         self.compare(picture='fighting.png', current_png='current2.png'))
                    #                     compare_result2 = float(
                    #                         self.compare(picture='fightingscreen.png', current_png='current2.png'))
                    #                     print(compare_result1, compare_result2)
                    #                     # 如果已经进入战斗画面,则等待75秒战斗结束
                    #                     if compare_result1 < compare_result2:
                    #                         time.sleep(30)
                    #                         # 确定胜利或失败
                    #                         while True:
                    #                             current_screen = pyautogui.screenshot()
                    #                             current_screen.save('current.png')
                    #
                    #                             victory = float(
                    #                                 self.compare(picture=self.victory, current_png='current.png'))
                    #                             fail = float(self.compare(picture=self.fail, current_png='current.png'))
                    #
                    #                             if victory > fail and victory > 0.7:
                    #                                 start = time.time()
                    #                                 end = start + float(30)
                    #                                 list_time.append(end)
                    #                                 print(list_time)
                    #                                 if count < 5:
                    #                                     count += 1
                    #                                 print('胜利数量{}'.format(count))
                    #
                    #                                 confirm_option = self.read(picture=self.confirm_button)
                    #                                 pyautogui.click(confirm_option)
                    #                                 time.sleep(1)
                    #                                 self.selection(kuang=kuang)
                    #                                 break
                    #
                    #                             if fail > victory and fail > 0.7:
                    #                                 confirm_option = self.read(picture=self.confirm_button)
                    #                                 pyautogui.click(confirm_option)
                    #                                 time.sleep(1)
                    #                                 self.selection(kuang=kuang)
                    #                                 break
                    #
                    #     fanhui = self.read(picture=self.fanhui)
                    #     pyautogui.click(fanhui)
                    #     time.sleep(1)

                else:
                    print('找不到战斗位置')


            except Exception as e:
                print(e)
                print(traceback.print_exc())

    def selection(self,kuang):

        while True:

            if self.break_program == True:
                break

            # list_pools = {'zijin': '紫金矿', 'lanjing': '蓝晶矿', 'dinong': '低浓蓝晶矿', 'gaonong': '高浓蓝晶矿'}
            # kuang = list(list_pools.keys())[list(list_pools.values()).index(value)]
            # kuang = kuang + '.png'
            current_screen = pyautogui.screenshot()
            current_screen.save('current.png')
            xuankuang = self.read(picture=kuang)
            if xuankuang is not None:
                pyautogui.click(xuankuang)
                time.sleep(1)
                pls_selection = self.read(picture='pls_selection.png')
                pyautogui.click(pls_selection[0], pls_selection[1] + 287)
                time.sleep(2)
                current_screen = pyautogui.screenshot()
                current_screen.save('current.png')

                if kuang == 'zijin.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.zijinkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'lanjing.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.lanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'gaonong.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.gaononglanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'dinong.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.dinonglanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

            # 进入采矿模式,点击侦查
            current_screen = pyautogui.screenshot()
            current_screen.save('current.png')
            zhencha = self.read(picture=self.zhencha)
            if zhencha is not None:
                pyautogui.click(zhencha)
                time.sleep(1)
            else:
                print('找不到侦查按钮')
            # 选择矿类型
            current_screen = pyautogui.screenshot()
            current_screen.save('current.png')

            xuankuang = self.read(picture=kuang)
            if xuankuang is not None:
                pyautogui.click(xuankuang)
                time.sleep(1)
                pls_selection = self.read(picture='pls_selection.png')
                pyautogui.click(pls_selection[0], pls_selection[1] + 287)
                time.sleep(2)
                current_screen = pyautogui.screenshot()
                current_screen.save('current.png')

                if kuang == 'zijin.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.zijinkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'lanjing.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.lanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'gaonong.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.gaononglanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')

                if kuang == 'dinong.png':
                    print('开挖')
                    kaiwa = self.read(picture=self.dinonglanjingkuang)
                    if kaiwa is not None:
                        pyautogui.click(kaiwa)
                        time.sleep(1)
                        break

                    else:
                        print('找不到开挖按钮')


    def miners(self,lenth):

        # 进入我的矿场
        current_screen = pyautogui.screenshot()
        current_screen.save('current.png')

        mymines = self.read(picture=self.myminers)
        if mymines is not None:
            pyautogui.click(mymines)
            time.sleep(1)
            current_screen = pyautogui.screenshot()
            current_screen.save('current.png')
            minerinfo = self.read(picture=self.minerinfo)
            if minerinfo is not None:

                for i in range(lenth):
                    position = 120 * i
                    page1 = (minerinfo[0], minerinfo[1] - 60)
                    print(position)
                    pyautogui.click((page1[0] + position, page1[1]))
                    time.sleep(1)
                    # 质押
                    for i in range(2):
                        zhiya = self.read(picture=self.zhiya)
                        if zhiya is not None:
                            pyautogui.click(zhiya)
                            time.sleep(1)
                        else:
                            print('没有质押按钮')

                        # 确定
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        queding = self.read(picture=self.queding)
                        if queding is not None:
                            pyautogui.click(queding)
                            time.sleep(1)
                        else:
                            print('没有确定按钮')

                    current_screen = pyautogui.screenshot()
                    current_screen.save('current.png')
                    # 检查收获
                    rece = self.read(picture=self.receive)
                    if rece is not None:
                        pyautogui.click(rece)
                        time.sleep(1)
                        pyautogui.click(rece)
                        time.sleep(1)

                    else:
                        print('没有收获按钮')

                tuichu = self.read(picture=self.tuichu)
                if tuichu is not None:
                    pyautogui.click(tuichu)
                    time.sleep(1)

            else:
                print('没有页数按钮')

        else:
            print('我的矿场没有按钮')

    def protect(self,lenth):
        # 进入我的矿场
        current_screen = pyautogui.screenshot()
        current_screen.save('current.png')

        mymines = self.read(picture=self.myminers)
        if mymines is not None:
            pyautogui.click(mymines)
            time.sleep(1)
            current_screen = pyautogui.screenshot()
            current_screen.save('current.png')
            minerinfo = self.read(picture=self.minerinfo)
            if minerinfo is not None:
                for i in range(lenth):
                    position = 120 * i
                    page1 = (minerinfo[0], minerinfo[1] - 60)
                    print(position)
                    pyautogui.click((page1[0] + position, page1[1]))
                    time.sleep(1)

                    # 保护检测
                    current_screen = pyautogui.screenshot()
                    current_screen.save('current.png')
                    protection = self.read(picture=self.protection)
                    if protection is not None:

                        pyautogui.click(protection)
                        time.sleep(1)
                        current_screen = pyautogui.screenshot()
                        current_screen.save('current.png')
                        backage = self.read(picture=self.backage)
                        print(backage)
                        if backage is not None:
                            # 点击药水
                            pyautogui.click((backage[0] + 34, backage[1] + 95))
                            time.sleep(1)
                            # 点击使用
                            usg = self.read(picture=self.usg)
                            pyautogui.click(usg)
                            # pyautogui.click((backage[0] + 434, backage[1] + 357))
                            time.sleep(1)
                            current_screen = pyautogui.screenshot()
                            current_screen.save('current.png')
                            result1 = float(self.compare(picture=self.backage, current_png='current.png'))
                            result2 = float(self.compare(picture='current2.png', current_png='current.png'))
                            print(result1, result2)
                            if result1 > result2:
                                print('喝药失败')
                                # 点击x按钮
                                pyautogui.click((backage[0] + 638, backage[1] - 20))
                                time.sleep(1)
                            else:
                                print('喝药成功')

                        else:
                            print('没有使用按钮')
                    else:
                        print('没有保护按钮')

        # 退出
        current_screen = pyautogui.screenshot()
        current_screen.save('current.png')
        tuichu = self.read(picture=self.tuichu)
        if tuichu is not None:
            pyautogui.click(tuichu)
            time.sleep(3)
        else:
            print('没有退出按钮')

    # def get_coin(self,lenth):
    #     current_screen = pyautogui.screenshot()
    #     current_screen.save('current.png')
    #     mymines = self.read(picture=self.myminers)
    #     if mymines is not None:
    #         pyautogui.click(mymines)
    #         time.sleep(1)
    #         for i in range(lenth):
    #             minerinfo = self.read(picture=self.minerinfo)
    #             if minerinfo is not None:
    #
    #                 position = 120 * i
    #                 page1 = (minerinfo[0], minerinfo[1] - 60)
    #                 print(position)
    #                 pyautogui.click((page1[0] + position, page1[1]))
    #                 time.sleep(1)
    #             else:
    #                 print('没有页数按钮')
    #
    #             current_screen = pyautogui.screenshot()
    #             current_screen.save('current.png')
    #             #检查收获
    #             rece = self.read(picture=self.receive)
    #             if rece is not None:
    #                 pyautogui.click(rece)
    #                 time.sleep(1)
    #                 pyautogui.click(rece)
    #                 time.sleep(1)
    #
    #             else:
    #                 print('没有收获按钮')
    #
    #         tuichu = self.read(picture=self.tuichu)
    #         if tuichu is not None:
    #             pyautogui.click(tuichu)
    #             time.sleep(1)

    def window(self):
        #建立窗口
        window = tk.Tk()
        #给窗口的可视化起名字
        window.title('船长社区Defina')
        #设置窗口大小
        window.geometry('450x600')
        #创建变量,用var1用来接收鼠标点击具体选项的值
        var1 = tk.StringVar()
        var0 = tk.StringVar()
        #在窗口上创建一个标签label用以显示和接收

        #创建listbox并为其添加挖矿类型
        var2 = tk.StringVar()
        var2.set(('紫金矿','高浓蓝晶矿','蓝晶矿','低浓蓝晶矿'))
        lb = tk.Listbox(window,listvariable=var2)
        lb.place(x=0,y=0,anchor='nw')
        var3 = tk.StringVar()

        lb1 = tk.Radiobutton(window,text='7英雄以下',variable=var3,value='1')
        lb1.place(x=230,y=0)
        lb2 = tk.Radiobutton(window, text='8-9英雄', variable=var3, value='2')
        lb2.place(x=230,y=25)
        lb3 = tk.Radiobutton(window, text='12-15英雄', variable=var3, value='3')
        lb3.place(x=230,y=50)
        lb4 = tk.Radiobutton(window, text='16-19英雄', variable=var3, value='4')
        lb4.place(x=230,y=75)
        lb5 = tk.Radiobutton(window, text='20英雄', variable=var3, value='5')
        lb5.place(x=230,y=100)

        # var4 = tk.IntVar()  # 定义var1和var2整型变量用来存放选择行为返回值
        # c1 = tk.Checkbutton(window, text='是否开启自动保护', variable=var4, onvalue=1, offvalue=0,
        #                     )  # 传值原理类似于radiobutton部件
        # c1.place(x=1160,y=330)

        def confirm_selection():


            value = lb.get(lb.curselection())
            value1 = var3.get()
            value2 = var5.get()
            var1.set('脚本开启中,请勿动鼠标和键盘,否则失效需重启脚本')
            wakuang.run(value=value,value1=value1,value2=value2)
            # wakuang.miners()

        #启动多线程 窗口不卡死
        def threading_ship():
            self.break_program = False
            th = threading.Thread(target=confirm_selection)
            th.setDaemon(True)
            th.start()

        def stop_run():
            var0.set('如果进入英雄选择界面,停止程序要等一次循环,一般30秒时间')
            self.break_program = True

        var5 = tk.IntVar()
        c5 = tk.Checkbutton(window, text='是否开启自动吃药保护', variable=var5, onvalue=1, offvalue=0)
        c5.place(x=235, y=161)

        tk.Button(window, text='请确认', width=15, height=2,command=threading_ship).place(x=30,y=260)
        tk.Button(window, text='结束按钮', width=15, height=2, command=stop_run).place(x=230,y=260)

        l = tk.Label(window, bg='red', fg='yellow', font=('Arial', 6), width=50,
                     textvariable=var1)
        l.place(x=35,y=200)
        z = tk.Label(window, bg='red', fg='yellow', font=('Arial', 6), width=50,
                     textvariable=var0)
        z.place(x=35, y=225)
        tk.Label(window,text='关注公众号,会员可免费使用脚本',bg='red', fg='yellow', font=('Arial', 6),height=3).place(x=216,y=337,anchor='n')

        im = Image.open('scancode.jpg')
        IM = ImageTk.PhotoImage(im)
        scanQR = tk.Label(window,image=IM).place(x=215,y=396,anchor='n')


        window.mainloop()




if __name__ == '__main__':

    wakuang = wakuang()
    wakuang.window()

总结

以上就是今天要讲的内容,本文仅仅简单脚本自动。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我懒所以我用脚本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值