Game Assist script (Python)

Source: Internet
Author: User

This article describes how to write game aid scripts in Python

The main way to achieve this is through a picture of the comparison, in the game on the line click. The following things are required to run the program.

PIL: Image processing module (Python3 replaced with pillow): https://www.lfd.uci.edu/~gohlke/pythonlibs/

Pywin32: Pip install Pypiwin32 used to simulate click-through

Tesseract: Implement picture text recognition here is the installation tutorial 78233459?locationnum=7&fps=1

#获取电脑上的窗口句柄
def foo (hwnd,mouse): if and and IsWindowVisible (hwnd): Titles.add (GetWindowText (HWND))
# The following code implements the Find simulator and makes the game interface open in the specified location based on the coordinates set
defplayGame ():"""Click The game icon in the simulator-enter and displays to the specified location"""EnumWindows (foo, 0) list= [] forTitleinchtitles:ifTitle:list.append (title) forTitleinchlist:a='Night God simulator' ifTitle.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 simulator to enter the gameWin32API. 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) returnSize

defgame ():"""Click to implement in the game"""    #Click I knowSize =playGame () time.sleep (15) topx, Topy= Size[0], size[1] Imagegrab.grab (topx+ 287, Topy + 307, TopX +, topy + +). Save ('d:\ ceshi.jpg'# to capture images in the game for a given size
# using the image hash algorithm to compare two pictures of the acquaintance degree hash_size= 6Hash1= Imagehash.average_hash (Image.open ('d:\ ceshi.jpg'), hash_size=hash_size) Hash2= Imagehash.average_hash (Image.open ('d:\, I know. jpg'), hash_size=hash_size) A= (1-(HASH1-HASH2)/len (hash1.hash) * * 2) Print(a)ifA > 0.6:
# operation Mouse click 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 above image hashing algorithm 78582064?locationnum=8&fps=1 This relatively accurate rate is not high, the following will be based on the recognition of the text on the image to match.

Now give the full code (for reference only)

ImportWin32guiImportWin32APIImportWin32con fromWin32guiImport*Import Time fromPILImportImage fromPILImportImagegrabImportImagehashImportPymouse,pykeyboard,os,sys fromPymouseImport* fromPykeyboardImportPYKEYBOARDM=pymouse () k=Pykeyboard () titles=set ()deffoo (hwnd,mouse):ifIsWindow (HWND) andIsWindowEnabled (HWND) andIsWindowVisible (HWND): Titles.add (GetWindowText (hwnd ))defplayGame ():"""Click The game icon in the simulator-enter and displays to the specified location"""EnumWindows (foo, 0) list= []     forTitleinchtitles:ifTitle:list.append (title) forTitleinchlist:a='Night God simulator'        ifTitle.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 simulator to enter the gameWin32API. 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)            returnsizedefgame ():"""Click to implement in the game"""    #Click I knowSize =playGame () time.sleep (15) topx, Topy= Size[0], size[1] Imagegrab.grab (topx+ 287, Topy + 307, TopX +, topy + +). Save ('d:\ ceshi.jpg') Hash_size= 6Hash1= Imagehash.average_hash (Image.open ('d:\ ceshi.jpg'), hash_size=hash_size) Hash2= Imagehash.average_hash (Image.open ('d:\, I know. jpg'), hash_size=hash_size) A= (1-(HASH1-HASH2)/len (hash1.hash) * * 2)    Print(a)ifA > 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 ()

The above is just click on a more implementation please self-solve (did not have this understanding hope to give a better way I learn to learn)

Game Assist script (Python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.