遊戲輔助指令碼(python)
阿新 • • 發佈:2019-01-01
本文介紹怎樣用Python寫遊戲輔助指令碼
主要實現方式是通過圖片的對比,在遊戲中就行點選。執行程式需要以下東西。
PIL: 圖片處理模組 (python3 換成了 pillow) 下載地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/
pywin32 : 用來模擬點選用的 pip install pypiwin32
#獲取電腦上的視窗控制代碼 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()