游戏辅助脚本(python)

本文介绍怎样用Python写游戏辅助脚本

主要实现方式是通过图片的对比,在游戏中就行点击。运行程序需要以下东西。

PIL: 图片处理模块     (python3 换成了 pillow)  下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/

pywin32 :  用来模拟点击用的      pip   install   pypiwin32  

tesseract  :  实现图片文字识别             这里是安装教程   https://blog.csdn.net/dcrmg/article/details/78233459?locationNum=7&fps=1

#获取电脑上的窗口句柄
def
foo(hwnd,mouse): if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd): titles.add(GetWindowText(hwnd))
# 下面这段代码实现  查找模拟器并并根据设置的坐标使游戏界面在指定位置打开
def
playGame(): """Click the game icon in the simulator to enter and displays to the specified location""" EnumWindows(foo, 0) list = [] for title in titles: if title: list.append(title) for title in list: a = '夜神模拟器' if title.find(a) != -1: hwnd = win32gui.FindWindow(0,a) win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, 640, 360, win32con.SWP_SHOWWINDOW) hwnd = win32gui.FindWindow(0,a) size = win32gui.GetWindowRect(hwnd) # 在模拟器点击游戏图标进入游戏 win32api.SetCursorPos([size[0] + 410, size[1] + 186]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0) time.sleep(10) return size
def game():
    """Click to implement in the game"""

    # 点击我知道
    size = playGame()
    time.sleep(15)
    topx, topy = size[0], size[1]
    ImageGrab.grab((topx + 287, topy + 307, topx + 350, topy + 330)).save('D:\ ceshi.jpg') # 根据给定尺寸在游戏中截取图片
# 利用图片hash算法对比两张图片的相识度 hash_size
= 6 hash1 = imagehash.average_hash(Image.open('D:\ ceshi.jpg'), hash_size=hash_size) hash2 = imagehash.average_hash(Image.open('D:\我知道了.jpg'), hash_size=hash_size) a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2) print(a) if a > 0.6:
# 操作鼠标点击 win32api.SetCursorPos([topx
+ 290, topy + 310]) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)

对于上方的图片哈希算法   https://blog.csdn.net/sinat_26917383/article/details/78582064?locationNum=8&fps=1这种相对来说准确率不高,后面会根据识别图片上的文字来进行匹配。

现在给出完整代码(仅供参考)

import win32gui
import win32api
import win32con
from win32gui import *
import time

from PIL import Image
from PIL import ImageGrab
import imagehash
import pymouse,pykeyboard,os,sys
from pymouse import *
from pykeyboard import PyKeyboard
m = PyMouse()
k = PyKeyboard()
titles = set()


def foo(hwnd,mouse):
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))



def playGame():
    """Click the game icon in the simulator to enter and displays to the specified location"""
    EnumWindows(foo, 0)
    list = []
    for title in titles:
        if title:
           list.append(title)
    for title in list:
        a = '夜神模拟器'
        if title.find(a) != -1:
            hwnd = win32gui.FindWindow(0,a)
            win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, 640, 360, win32con.SWP_SHOWWINDOW)
            hwnd = win32gui.FindWindow(0,a)
            size = win32gui.GetWindowRect(hwnd)
            # 在模拟器点击游戏图标进入游戏
            win32api.SetCursorPos([size[0] + 410, size[1] + 186])
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
            time.sleep(10)
            return size


def game():
    """Click to implement in the game"""

    # 点击我知道
    size = playGame()
    time.sleep(15)
    topx, topy = size[0], size[1]
    ImageGrab.grab((topx + 287, topy + 307, topx + 350, topy + 330)).save('D:\ ceshi.jpg')
    hash_size = 6
    hash1 = imagehash.average_hash(Image.open('D:\ ceshi.jpg'), hash_size=hash_size)
    hash2 = imagehash.average_hash(Image.open('D:\我知道了.jpg'), hash_size=hash_size)
    a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2)
    print(a)
    if a > 0.6:
        win32api.SetCursorPos([topx + 290, topy + 310])
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
if __name__ == '__main__':
game()

上述只是点击了一处更多实现请自行解决(没搞过这个懂  希望给出更好的方法  我好学习学习)

猜你喜欢

转载自www.cnblogs.com/shuomi/p/9319294.html