Game assisted scripting (python)

This paper describes how to write game assistant scripts in Python

The main way to achieve this is to click on the line in the game by comparing pictures.Running a program requires the following.

PIL: Picture Processing Module (python3 replaced with pillow) Download address: https://www.lfd.uci.edu/~gohlke/pythonlibs/

pywin32: pip install pypiwin32 to simulate clicks

tesseract: Implement picture and text recognition. Here is the installation tutorial. https://blog.csdn.net/dcrmg/article/details/78233459?locationNum=7&fps=1

#Get the window handle on your computer
def foo(hwnd,mouse):
    if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
        titles.add(GetWindowText(hwnd))
# This code finds the simulator and opens the game interface at the specified location according to the coordinates set
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 = 'Night God simulator'
        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)
            # Click on the game icon in the emulator to enter the game
            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"""

    # Click I Know
    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') # Capture pictures in a game based on a given size
    # Comparing the Recognition of Two Pictures Using the Picture hash Algorithm
    hash_size = 6
    hash1 = imagehash.average_hash(Image.open('D:\ ceshi.jpg'), hash_size=hash_size)
    hash2 = imagehash.average_hash(Image.open('D:\I got it!.jpg'), hash_size=hash_size)
    a = (1 - (hash1 - hash2) / len(hash1.hash) ** 2)
    print(a)
    if a > 0.6:
        # Operate mouse clicks
        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)

For the image hashing algorithm above, https://blog.csdn.net/sinat_26917383/article/details/78582064?LocionNum=8&fps=1 is a relatively inexact algorithm, which will be matched based on the text on the recognized picture.

Now give some code (for reference only)

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 = 'Night God simulator'
        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)
            # Click on the game icon in the emulator to enter the game
            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"""

    # Click I Know
    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:\I got it!.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()

 

Keywords: simulator emulator Python pip

Added by carlheaton on Mon, 03 Feb 2020 18:54:38 +0200