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

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

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

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

tesseract  :  实现图片文字识别             这里是安装教程   Windows下Tesseract4.0识别与中文手写字体训练_牧野的博客-CSDN博客_tesseract4.0

#获取电脑上的窗口句柄
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 = 6hash1 = 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 timefrom 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 sizedef 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 = 6hash1 = 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)相关推荐

  1. python 游戏辅助脚本,python写游戏脚本辅助作范文

    接上例,在程序刚开始的时候,我们应该取出上一次所存储的主窗体的位置,可以写成: frmMain.Top=GetSetting ("测试","main",&quo ...

  2. 不愧是大佬用Python做一个游戏辅助脚本,完整编程思路分享!

    一.说明 简述:本文将以4399小游戏< 宠物连连看经典版2 >作为测试案例,通过识别小图标,模拟鼠标点击,快速完成配对.对于有兴趣学习游戏脚本的同学有一定的帮助.文末有Python资料和 ...

  3. 用Python做一个游戏辅助脚本(含完整编程思路)

    一.说明 简述:本文将以4399小游戏< 宠物连连看经典版2 >作为测试案例,通过识别小图标,模拟鼠标点击,快速完成配对.对于有兴趣学习游戏脚本的同学有一定的帮助. 运行环境:Win10/ ...

  4. [Python] 用python做一个游戏辅助脚本,完整思路

    [Python] 用python做一个游戏辅助脚本,完整思路 一.说明 简述:本文将以4399小游戏<宠物连连看经典版2>作为测试案例,通过识别小图标,模拟鼠标点击,快速完成配对.对于有兴 ...

  5. python 游戏辅助脚本_python版微信跳一跳游戏辅助

    本文实例为大家分享了微信跳一跳游戏辅助python代码,供大家参考,具体内容如下 import os import PIL import numpy import matplotlib matplot ...

  6. 4399小游戏—宠物连连看经典版2—游戏辅助脚本

    引言 期末的时候看到一篇博客,写的宠物连连看的辅助脚本,感觉很有意思,就自己跟着博客自己实现了一遍,开发过程中遇到了一些问题,也体会到了解决问题的乐趣,遂在此记录一下. 先放一下博客的链接:https ...

  7. python辅助脚本教程_[Python] 用python做一个游戏辅助脚本,完整思路

    说明 简述:本文将以4399小游戏<宠物连连看经典版2>作为测试案例,通过识别小图标,模拟鼠标点击,快速完成配对.对于有兴趣学习游戏脚本的同学有一定的帮助. 运行环境:Win10/Pyth ...

  8. python+adb游戏辅助脚本

    不实用,仅作参考 零.大致思路 手动启动游戏界面 截图(adb/minicap) 计算滑动路径 按键输出(adb/minitouch) 一.截图 可用两种方式实现 adb shell minicap ...

  9. 教你如何使用Python写游戏辅助脚本

    主要实现方式是通过图片的对比,在游戏中就行点击.运行程序需要以下东西. PIL: 图片处理模块     (python3 换成了 pillow)  下载地址: https://www.lfd.uci. ...

最新文章

  1. Fragment的startActivityForResult详细解决方案
  2. 文章17周项目2--通过基准线结合(三个数字排序(指针参数))
  3. C++11中值得关注的几大变化
  4. AngularJs $anchorScroll、$controller、$document
  5. 中小企业虚拟化解决方案-VMware vSphere 6.5-日常管理入口v0.0.1
  6. mysql in优化_MySQL 探秘: 1 整体架构
  7. 光端机与交换机有什么区别?
  8. 《Java和Android开发学习指南(第2版)》——第2章,第2.10节本章小结
  9. 2个html文件顺序播放,CSS3两个动画顺序衔接播放
  10. wince6-Error: failed PB timebomb check
  11. dedecms织梦入门
  12. GBK/GBK2312字库寻址及使用原理
  13. ABB机器人RobotStudio编程指令大全
  14. Excel POI 导入导出(支持大数据量快速导出)
  15. 鹤林全集·闲情志友 | 第一篇——阿鑫
  16. mysql数据库连接池锁_数据库连接池deadlock
  17. iOS开发之获取实时气压、相对高度
  18. 无法通过windows功能控制面板自动安装或卸载windows server角色和功能
  19. 【Python】matplotlib画图设置标题、轴标签、刻度、刻度标签(系列1)
  20. 详解Android刘海屏适配

热门文章

  1. 用Python分析《权力的游戏》
  2. 【开服必备】用Python优雅的生成用户验证码
  3. 蕊蕊吃糖2870 C语言
  4. 《比海更深》 是枝裕和 读后感
  5. Win8初体验 内存/SSD硬盘性能挑Win7(一)
  6. 二元logistics回归
  7. python地铁车票_Python分析3034个地铁站,发现中国地铁名字的秘密。
  8. Android 获得手机屏幕大小
  9. Python基础从0到1自我学习(3)
  10. 首个汽车信息安全ISO国际标准正式发布ISO-SAE 21434