1. 程式人生 > >遊戲輔助腳本(python)

遊戲輔助腳本(python)

open div and ecif set pen ati 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()

上述只是點擊了一處更多實現請自行解決(沒搞過這個懂 希望給出更好的方法 我好學習學習)

遊戲輔助腳本(python)