Python制作安卓游戏外挂

article/2025/9/24 6:12:13

Python制作安卓游戏外挂

最近在玩一款背单词的手机游戏-单词英雄,是一个将背单词和卡牌游戏相结合的游戏,通过选择正确的单词意思进行有效攻击,边玩游戏就把单词给背了。

  游戏的界面是这样的:


Paste_Image.png


  通过选择单词的意思进行攻击,选对了就正常攻击,选错了就象征性的攻击一下。玩了一段时间之后琢磨可以做成自动的,通过PIL识别图片里的单词和选项,然后翻译英文成中文意思,根据中文模糊匹配选择对应的选项。
  查找了N多资料以后开始动手,程序用到以下这些东西:
PIL       Python Imaging Library 大名鼎鼎的图片处理模块
pytesser     Python下用来驱动tesseract-ocr来进行识别的模块
Tesseract-OCR  图像识别引擎,用来把图像识别成文字,可以识别英文和中文,以及其它语言
autopy     Python下用来模拟操作鼠标和键盘的模块。

安装步骤(win7环境):
  (1)安装PIL,下载地址:http://www.pythonware.com/products/pil/,安装PythonImaging Library 1.1.7 for Python 2.7。
  (2)安装pytesser,下载地址:http://code.google.com/p/pytesser/,下载解压后直接放在
C:\Python27\Lib\site-packages下,在文件夹下建立pytesser.pth文件,内容为C:\Python27\Lib\site-packages\pytesser_v0.0.1
  (3)安装Tesseract OCR engine,下载:https://github.com/tesseract-ocr/tesseract/wiki/Downloads,下载Windows installer of tesseract-ocr 3.02.02 (including English language data)的安装文件,进行安装。
  (4)安装语言包,在https://github.com/tesseract-ocr/tessdata下载chi_sim.traineddata简体中文语言包,放到安装的Tesseract OCR目标下的tessdata文件夹内,用来识别简体中文。
  (5)修改C:\Python27\Lib\site-packages\pytesser_v0.0.1下的pytesser.py的函数,将原来的image_to_string函数增加语音选择参数language,language='chi_sim'就可以用来识别中文,默认为eng英文。
改好后的pytesser.py:

"""OCR in Python using the Tesseract engine from Google
http://code.google.com/p/pytesser/
by Michael J.T. O'Kelly
V 0.0.1, 3/10/07"""import Image
import subprocess
import util
import errorstesseract_exe_name = 'tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True  # Temporary files cleaned up after OCR operationdef call_tesseract(input_filename, output_filename, language):"""Calls external tesseract.exe on input file (restrictions on types),outputting output_filename+'txt'"""args = [tesseract_exe_name, input_filename, output_filename, "-l", language]proc = subprocess.Popen(args)retcode = proc.wait()if retcode!=0:errors.check_for_errors()def image_to_string(im, cleanup = cleanup_scratch_flag, language = "eng"):"""Converts im to file, applies tesseract, and fetches resulting text.If cleanup=True, delete scratch files after operation."""try:util.image_to_scratch(im, scratch_image_name)call_tesseract(scratch_image_name, scratch_text_name_root,language)text = util.retrieve_text(scratch_text_name_root)finally:if cleanup:util.perform_cleanup(scratch_image_name, scratch_text_name_root)return textdef image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True, language = "eng"):"""Applies tesseract to filename; or, if image is incompatible and graceful_errors=True,converts to compatible format and then applies tesseract.  Fetches resulting text.If cleanup=True, delete scratch files after operation."""try:try:call_tesseract(filename, scratch_text_name_root, language)text = util.retrieve_text(scratch_text_name_root)except errors.Tesser_General_Exception:if graceful_errors:im = Image.open(filename)text = image_to_string(im, cleanup)else:raisefinally:if cleanup:util.perform_cleanup(scratch_image_name, scratch_text_name_root)return textif __name__=='__main__':im = Image.open('phototest.tif')text = image_to_string(im)print texttry:text = image_file_to_string('fnord.tif', graceful_errors=False)except errors.Tesser_General_Exception, value:print "fnord.tif is incompatible filetype.  Try graceful_errors=True"print valuetext = image_file_to_string('fnord.tif', graceful_errors=True)print "fnord.tif contents:", texttext = image_file_to_string('fonts_test.png', graceful_errors=True)print text

  (6)安装autopy,下载地址:https://pypi.python.org/pypi/autopy,下载autopy-0.51.win32-py2.7.exe进行安装,用来模拟鼠标操作。

说下程序的思路:
 1. 首先是通过模拟器在WINDOWS下执行安卓的程序,然后用PicPick进行截图,将战斗画面中需要用到的区域进行测量,记录下具体在屏幕上的位置区域,用图中1来判断战斗是否开始(保存下来用作比对),用2,3,4,5,6的区域抓取识别成文字。


1485005-75900c16b01ca1f3.png


计算图片指纹的程序:

    def get_hash(self, img):#计算图片的hash值image = img.convert("L")pixels = list(image.getdata())avg = sum(pixels) / len(pixels)return "".join(map(lambda p : "1" if p > avg else "0", pixels))

图片识别成字符:

    #识别出对应位置图像成字符,把字符交给chose处理def getWordMeaning(self):pic_up = ImageGrab.grab((480,350, 480+300, 350+66))pic_aws1 = ImageGrab.grab((463,456, 463+362, 456+45))pic_aws2 = ImageGrab.grab((463,530, 463+362, 530+45))pic_aws3 = ImageGrab.grab((463,601, 463+362, 601+45))pic_aws4 = ImageGrab.grab((463,673, 463+362, 673+45))str_up = image_to_string(pic_up).strip().lower()#判断当前单词和上次识别单词相同,就不继续识别if str_up <> self.lastWord:#如果题目单词是英文,选项按中文进行识别if str_up.isalpha():eng_up = self.dt[str_up].decode('gbk') if self.dt.has_key(str_up) else ''chs1 = image_to_string(pic_aws1, language='chi_sim').decode('utf-8').strip()chs2 = image_to_string(pic_aws2, language='chi_sim').decode('utf-8').strip()chs3 = image_to_string(pic_aws3, language='chi_sim').decode('utf-8').strip()chs4 = image_to_string(pic_aws4, language='chi_sim').decode('utf-8').strip()print str_up, ':', eng_upself.chose(eng_up, (chs1, chs2, chs3, chs4))#如果题目单词是中文,选项按英文进行识别else:chs_up = image_to_string(pic_up, language='chi_sim').decode('utf-8').strip()eng1 = image_to_string(pic_aws1).strip()eng2 = image_to_string(pic_aws2).strip()eng3 = image_to_string(pic_aws3).strip()eng4 = image_to_string(pic_aws4).strip()e2c1 = self.dt[eng1].decode('gbk') if self.dt.has_key(eng1) else ''e2c2 = self.dt[eng2].decode('gbk') if self.dt.has_key(eng2) else ''e2c3 = self.dt[eng3].decode('gbk') if self.dt.has_key(eng3) else ''e2c4 = self.dt[eng4].decode('gbk') if self.dt.has_key(eng4) else ''print chs_upself.chose(chs_up, (e2c1, e2c2, e2c3, e2c4))self.lastWord = str_upreturn str_up

  2. 对于1位置的图片提前截一个保存下来,然后通过计算当前画面和保存下来的图片的距离,判断如果小于40的就表示已经到了选择界面,然后识别2,3,4,5,6成字符,判断如果2位置识别成英文字符的,就用2解析出来的英文在字典中获取中文意思,然后再通过2的中文意思和3,4,5,6文字进行匹配,匹配上汉字最多的就做选择,如果匹配不上默认返回最后一个。之前本来考虑是用Fuzzywuzzy来进行模糊匹配算相似度的,不过后来测试了下对于中文匹配的效果不好,就改成按汉字单个进行匹配计算相似度。
匹配文字进行选择:

    #根据传入的题目和选项进行匹配选择def chose(self, g, chs_list):j, max_score = -1, 0same_list = None#替换掉题目里的特殊字符re_list = [u'~', u',', u'.', u';', u' ', u'a', u'V', u'v', u'i', u'n', u'【', u')', u'_', u'W', u'd', u'j', u'-', u't']for i in re_list:g = g.replace(i, '')print type(g)#判断2个字符串中相同字符,相同字符最多的为最佳答案for i, chsWord in enumerate(chs_list):print type(chsWord)l = [x for x in g if x in chsWord and len(x)>0]score = len(l) if l else 0if score > max_score:max_score = scorej = isame_list = l#如果没有匹配上默认选最后一个if j ==-1:print '1. %s; 2. %s; 3. %s; 4. %s; Not found choice.' % (chs_list[0], chs_list[1], chs_list[2], chs_list[3])else:print '1. %s; 2. %s; 3. %s; 4. %s; choice: %s' % (chs_list[0], chs_list[1], chs_list[2], chs_list[3], chs_list[j])for k, v in enumerate(same_list):print str(k) + '.' + v,order = j + 1self.mouseMove(order)return order

  3.最后通过mouseMove调用autopy操作鼠标点击对应位置进行选择。
程序运行的录像:
http://v.youku.com/v_show/id_XMTYxNTAzMDUwNA==.html
  程序完成后使用正常,因为图片识别准确率和字典的问题,正确率约为70%左右,效果还是比较满意。程序总体来说比较简单,做出来也就是纯粹娱乐一下,串联使用了图片识别、中文模糊匹配、鼠标模拟操作,算是个简单的小外挂吧,源程序和用到的文件如下:
http://git.oschina.net/highroom/My-Project/tree/master/Word%20Hero


http://chatgpt.dhexx.cn/article/elvJnXfK.shtml

相关文章

一步步用python制作游戏外挂

玩过电脑游戏的同学对于外挂肯定不陌生&#xff0c;但是你在用外挂的时候有没有想过如何做一个外挂呢&#xff1f;&#xff08;当然用外挂不是那么道义哈&#xff0c;呵呵&#xff09;&#xff0c;那我们就来看一下如何用python来制作一个外挂。。。。 我打开了4399小游戏网&am…

游戏外挂怎么做?

文章目录 1.什么是游戏外挂2.外挂的分类及实现原理2.1 辅助类外挂2.2 专用插件类外挂2.3 通用工具2.4 内存修改器2.5 变速器2.6 按键精灵2.7 模拟器2.8 破解版 转载自&#xff1a;Anti-Cheat Expert 游戏安全专家 干货&#xff01;什么是游戏外挂&#xff0c;外挂的种类及实现原…

Windows关闭某个端口的服务

在开发过程中有些服务没有正常关闭&#xff0c;在重新启动新版本的服务的时候会存在端口占用的情况。就需要先停止之前的服务进程。 例如7777端口被占用。 1 查看7777端口被什么服务占用 netstat -ano | findstr 77772 停止该服务 使用taskkill /PID 55684 /T /F 这行命令停…

windows关闭端口

关闭8004端口 找到占用8080端口对应的程序的PID号 netstat -aon|findstr "8004"找到对应的程序名 tasklist|findstr "PID号"杀死程序 taskkill /f /t /im java.exe或者 taskkill /pid 7952 /F

win10防火墙怎么关闭端口?

我们都知道win10系统自带防火墙功能&#xff0c;如果用户使用的是公用网络&#xff0c;防火墙就会提醒各种问题。有很多用户使用完端口想要关闭&#xff0c;但是不知道如何关闭。下面小编就给大家讲讲如何关闭防火墙端口的方法。 若要关闭Microsoft Defender防火墙中的端口&…

关闭Windows指定端口

Windows默认情况下&#xff0c;很多端口是开发的&#xff0c;这就给网络病毒或黑客通过这些开放的端口登录你的电脑提供的机会。 可以采用Zenmap工具扫描指定ip所开放的端口&#xff0c;且应在局域网和互联网等条件下进行端口扫描&#xff0c;保障端口的开放的可控&#xff0c…

windows10 关闭指定端口

今天在Windows10电脑上安装zk和dubbo&#xff0c;运行Tomcat总是包端口占用异常&#xff0c;重启了一次电脑后觉得一直重启不是个好办法&#xff0c;于是学会了用命令关闭进程的技能 查看指定端口的使用情况 使用命令&#xff1a; netstat -ano | findstr 端口号1 如下所示&…

win10关闭某个端口

前言 作为一个程序员&#xff0c;经常遇到启动某个软件时&#xff0c;会爆出某个端口被占用&#xff0c;所以使用命令关闭端口的技能必须掌握 第一步&#xff1a;查询端口对应的PID netstat -ano|findstr 1099 第二步&#xff1a;根据PID关闭该进程 【F参数&#xff1a;表…

WINDOWS10如何关闭占用的端口号

WINDOW10 如何关闭占用的端口号 今天运行服务的时候&#xff0c;发现报错。 Web server failed to start. Port xxxx was already in use.这里的’xxxx‘是端口号。原因是程序没关闭&#xff0c;电脑就关机了&#xff0c;因此再次运行该程序的时候发现端口号被占用。 这样的…

Windows系统关闭端口教程

打开控制面板----管理工具----本地安全策略 单击选中“IP安全策略在本地计算机”&#xff0c;在右侧空白处右击&#xff0c;选择创建IP安全策略 点击下一步 输入新建策略的名称----点击下一步 下一步 单击完成 在新IP安全策略属性窗口下 新建安全规则----单击添加 单击添加 在…

Debian上如何打开关闭端口

1. 可以通过"netstat -anp" 来查看哪些端口被打开。 &#xff08;注&#xff1a;加参数-n会将应用程序转为端口显示&#xff0c;即数字格式的地址&#xff0c;如&#xff1a;nfs->2049, ftp->21&#xff0c;因此可以开启两个终端&#xff0c;一一对应一下程序所…

windows关闭端口命令cmd

打开命令行窗口 输入 netstat -ano |findstr &#xff08;需要关闭的端口&#xff09; 比如要关闭8081这个端口 输入 taskkill /t /f /im (根据端口得到的进程号) 例如&#xff1a; 21500就是上面查到的进程号

Windows端口开启关闭

亲测可用&#xff0c;若有疑问请私信 netstat-a #显示所有活动的TCP连接以及计算机监听的TCP和UDP端口。 netstat-e #显示以太网发送和接收的字节数、数据包数等。 netstat-n #以数字形式显示所有活动的TCP连接的地址和端口号。 netstat-o #显示活动的TCP连接并包括每个连…

Linux下开启、关闭端口的方法

Linux下开启、关闭端口的方法 1、查看防火墙状态2、开启防火墙3、开启端口&#xff08;以端口443为例&#xff09;4、重启防火墙5、重新载入防火墙6、查看已开启的端口7、如何关闭端口 1、查看防火墙状态 在Linux控制台输入&#xff1a;firewall-cmd --state 此时控制台返回&a…

Windows如何关闭端口(图文)

方法1&#xff1a;创建防火墙策略进行阻止端口&#xff08;防火墙开启才生效&#xff09; 方法2&#xff1a;本地安全策略关闭 控制面板--系统--管理工具--本地安全策略 新建安全策略 去掉添加向导的勾选&#xff0c;添加筛选器列表 设置ip地址 设置协议类型 设置端口 添加筛…

命令行关闭端口

1.打开cmd&#xff08;winR或者搜索框搜索cmd&#xff09; 2.netstat -ano | findstr :8082 3.taskkill /F /PID 25108 附&#xff1a;如果结束端口提示不成功请使用管理员权限打开cmd在执行前面的步骤即可

cmd关闭端口

cmd关闭端口的命令是“taskkill”&#xff0c;该命令用于结束一个或多个任务或流程。关闭端口的方法&#xff1a;1、使用“taskkill /pid 端口号 -t -f”命令来强制关闭&#xff1b;2、使用“taskkill /f /t /im 进程名”命令来关闭。 通过CMD命令行打开防火墙端口 Windows 使用…

windows端口占用关闭指定端口

项目无法启动&#xff0c;端口冲突 1、查看windows端口进程 点击开始菜单选择运行&#xff08;winr&#xff09;&#xff0c;接着在运行对话框中输入“cmd”&#xff0c;回车打开命令提示符窗口&#xff0c;然后在窗口中输入【netstat -aon|findstr “端口”】&#xff0c;回车…

windows系统关闭指定端口

一、在dos窗口中输入指令&#xff1a;netstat -ano | findstr 9095&#xff0c;其中9095是指你指定的端口号&#xff0c;然后会显示出现在占用该端口号的pid。 二、输入 taskkill /f /pid 5372,关闭该进程即可

windows系统关闭指定的端口

【win】 【r】打开cmd窗口 使用【netstat -nao】查看所有的IP和端口。 在cmd窗口中输入【netstat -ano | findstr 8800】指令&#xff0c;8800是你需要关闭的端口号。 然后再输入【taskkill /f /pid 14328】关闭这个进程即可。