None

# _*_ coding:utf-8 _*_


from pymouse import *
from pykeyboard import *
from PIL import ImageGrab
import numpy as np
import threading
import win32gui
import pyaudio
import time
import wave
import cv2

# 获取游戏句柄
def get_all_hwnd(hwnd, mouse):
    global Jub
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
        if win32gui.GetWindowText(hwnd) == 'xxxx':
            Jub = hwnd
win32gui.EnumWindows(get_all_hwnd, 0)

def counter():
    number = 0
    while True:
        time.sleep(1)
        number += 1

class AutoTools(object):

    def __init__(self):
        pass

    def ShowTheGame(self):
        win32gui.SetForegroundWindow(Jub)

    def ScreenshotOfGame(self, name):
        pic = ImageGrab.grab()
        pic.save(name)

    def MatchThePicture(self, A, B):
        target = cv2.imread(A)
        template = cv2.imread(B)
        result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
        cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
        x = min_loc[0] + 30
        y = min_loc[1] + 30
        return x, y


    def StartRecording(self, x, y):
        CHUNK = 512
        FORMAT = pyaudio.paInt16
        CHANNELS = 1
        RATE = 48000
        p = pyaudio.PyAudio()
        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)
        fishtime = int(time.time())
        while (True):
            if int(time.time()) - fishtime >= 30:
                k.press_key(' ')
                k.release_key(' ')
                break
            for i in range(0, 10):
                data = stream.read(CHUNK)
            audio_data = np.frombuffer(data, dtype=np.short)
            temp = np.max(audio_data)
            if temp > 300:
                print('钓到鱼了!!!')
                break
        stream.stop_stream()
        stream.close()
        time.sleep(0.1)
        m.click(x, y, 2)

class Script(object):

    def Eat(self):
        time.sleep(2)
        #ao.ShowTheGame()
        k.press_key('0')
        k.release_key('0')
        k.press_key('=')
        k.release_key('=')
        time.sleep(5.1)


    def Fishing(self):
        #ao.ShowTheGame()
        k.press_key('9')
        k.release_key('9')
        time.sleep(2)
        ao.ScreenshotOfGame('b.png')
        x, y = ao.MatchThePicture('b.png', './gouzi.png')
        m.move(x, y)
        ao.StartRecording(x, y)


def run():
    sc.Eat()
    starttime = int(time.time())
    while True:
        time.sleep(2)
        if int(time.time()) - starttime >= 600:
            sc.Eat()
            starttime = int(time.time())
        time.sleep(1)
        sc.Fishing()


if __name__ == '__main__':
    ao = AutoTools()
    sc = Script()
    m = PyMouse()
    k = PyKeyboard()
    run()
原文地址:https://www.cnblogs.com/liuhedong/p/12454362.html