微信跳一跳辅助Java代码实现
微信跳一跳辅助的Java具体实现代码,供大家参考,具体内容如下
1.参考知乎教你用Python来玩微信跳一跳,鉴于本人Python一直都是半吊子水平,之前打算用python刷分,可无奈安装python环境各种模块缺失,报错不停,于是乎,使用Java重新实现了一下。
2.环境配置及相关说明:
1)、Windows系统,本人win10
2)、AVA环境安装,JDK7以上即可
3)、安卓手机一部、数据线一条
4)、电脑安装ADB驱动,连接安卓手机,同时打开USB调试模式
5)、打开微信小程序的跳一跳游戏,JAVA程序跑起来,具体代码往下看
6)、本人所用为魅蓝note2安卓手机,屏幕分辨率1920x1080,不同型号的手机,可能需要调整相关参数,具体看代码注释
7)、增加了刷分失败后游戏自动重新开局功能
8)、娱乐而已,不要较真,据说微信官方已经关注,分数太高可能会被清零,哈哈
3、废话不多说,上代码:
packagecom.yihusitian.gamehelper; importjava.awt.image.BufferedImage; importjava.io.BufferedReader; importjava.io.File; importjava.io.IOException; importjava.io.InputStreamReader; importjava.util.Arrays; importjava.util.concurrent.TimeUnit; importjavax.imageio.ImageIO; /** *参考知乎 * *@linkhttps://zhuanlan.zhihu.com/p/32452473 * *跳一跳辅助 * *@authorLeeHo */ publicclassJumpJumpHelper { privatestaticfinalStringIMAGE_NAME="current.png"; privatestaticfinalStringSTORE_DIR="d:/jump_screencapture"; //数量 privatestaticfinalintimageLengthLength=5; //存放图片的大小 privatestaticfinallong[]imageLength=newlong[imageLengthLength]; privatefinalRGBInforgbInfo=newRGBInfo(); privatefinalString[]ADB_SCREEN_CAPTURE_CMDS= {"adbshellscreencap-p/sdcard/"+IMAGE_NAME, "adbpull/sdcard/current.png"+STORE_DIR}; //截屏中游戏分数显示区域最下方的Y坐标,300是1920x1080的值,根据实际情况修改 privatefinalintgameScoreBottomY=300; //按压的时间系数,可根据具体情况适当调节 privatefinaldoublepressTimeCoefficient=1.35; //按压的起始点坐标,也是再来一局的起始点坐标 privatefinalintswipeX=550; privatefinalintswipeY=1580; //二分之一的棋子底座高度 privatefinalinthalfBaseBoardHeight=20; //棋子的宽度,从截屏中量取,自行调节 privatefinalinthalmaBodyWidth=74; //游戏截屏里的两个跳板的中点坐标,主要用来计算角度,可依据实际的截屏计算,计算XY的比例 privatefinalintboardX1=813; privatefinalintboardY1=1122; privatefinalintboardX2=310; privatefinalintboardY2=813; /** *获取跳棋以及下一块跳板的中心坐标 * *@return *@authorLeeHo *@throwsIOException *@update2017年12月31日下午12:18:22 */ privateint[]getHalmaAndBoardXYValue(FilecurrentImage)throwsIOException { BufferedImagebufferedImage=ImageIO.read(currentImage); intwidth=bufferedImage.getWidth(); intheight=bufferedImage.getHeight(); System.out.println("宽度:"+width+",高度:"+height); inthalmaXSum=0; inthalmaXCount=0; inthalmaYMax=0; intboardX=0; intboardY=0; //从截屏从上往下逐行遍历像素点,以棋子颜色作为位置识别的依据,最终取出棋子颜色最低行所有像素点的平均值,即计算出棋子所在的坐标 for(inty=gameScoreBottomY;y 50&&rValue<60&&gValue>53&&gValue<63&&bValue>95&&bValue<110) { halmaXSum+=x; halmaXCount++; //棋子底行的Y坐标值 halmaYMax=y>halmaYMax?y:halmaYMax; } } } if(halmaXSum!=0&&halmaXCount!=0) { //棋子底行的X坐标值 inthalmaX=halmaXSum/halmaXCount; //上移棋子底盘高度的一半 inthalmaY=halmaYMax-halfBaseBoardHeight; //从gameScoreBottomY开始 for(inty=gameScoreBottomY;y 0) { break; } intboardXSum=0; intboardXCount=0; for(intx=0;x 10) { boardXSum+=x; boardXCount++; } } if(boardXSum>0) { boardX=boardXSum/boardXCount; } } //按实际的角度来算,找到接近下一个board中心的坐标 boardY=(int)(halmaY-Math.abs(boardX-halmaX)*Math.abs(boardY1-boardY2) /Math.abs(boardX1-boardX2)); if(boardX>0&&boardY>0) { int[]result=newint[4]; //棋子的X坐标 result[0]=halmaX; //棋子的Y坐标 result[1]=halmaY; //下一块跳板的X坐标 result[2]=boardX; //下一块跳板的Y坐标 result[3]=boardY; returnresult; } } returnnull; } /** *执行命令 * *@paramcommand *@authorLeeHo *@update2017年12月31日下午12:13:39 */ privatevoidexecuteCommand(Stringcommand) { Processprocess=null; try { process=Runtime.getRuntime().exec(command); System.out.println("execcommandstart:"+command); process.waitFor(); BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(process.getErrorStream())); Stringline=bufferedReader.readLine(); if(line!=null) { System.out.println(line); } System.out.println("execcommandend:"+command); } catch(Exceptione) { e.printStackTrace(); } finally { if(process!=null) { process.destroy(); } } } /** *ADB获取安卓截屏 * *@authorLeeHo *@update2017年12月31日下午12:11:42 */ privatevoidexecuteADBCaptureCommands() { for(Stringcommand:ADB_SCREEN_CAPTURE_CMDS) { executeCommand(command); } } /** *跳一下 * *@paramdistance *@authorLeeHo *@update2017年12月31日下午12:23:19 */ privatevoiddoJump(doubledistance) { System.out.println("distance:"+distance); //计算按压时间,最小200毫秒 intpressTime=(int)Math.max(distance*pressTimeCoefficient,200); System.out.println("pressTime:"+pressTime); //执行按压操作 Stringcommand=String.format("adbshellinputswipe%s%s%s%s%s",swipeX,swipeY,swipeX,swipeY, pressTime); System.out.println(command); executeCommand(command); } /** *再来一局 * *@authorLeeHo *@update2017年12月31日下午12:47:06 */ privatevoidreplayGame() { Stringcommand=String.format("adbshellinputtap%s%s",swipeX,swipeY); executeCommand(command); } /** *计算跳跃的距离,也即两个点之间的距离 * *@paramhalmaX *@paramhalmaY *@paramboardX *@paramboardY *@return *@authorLeeHo *@update2017年12月31日下午12:27:30 */ privatedoublecomputeJumpDistance(inthalmaX,inthalmaY,intboardX,intboardY) { returnMath.sqrt(Math.pow(Math.abs(boardX-halmaX),2)+Math.pow(Math.abs(boardY-halmaY),2)); } publicstaticvoidmain(String[]args) { try { FilestoreDir=newFile(STORE_DIR); if(!storeDir.exists()){ booleanflag=storeDir.mkdir(); if(!flag){ System.err.println("创建图片存储目录失败"); return; } } JumpJumpHelperjumpjumpHelper=newJumpJumpHelper(); //执行次数 intexecuteCount=0; for(;;) { //执行ADB命令,获取安卓截屏 jumpjumpHelper.executeADBCaptureCommands(); FilecurrentImage=newFile(STORE_DIR,IMAGE_NAME); if(!currentImage.exists()) { System.out.println("图片不存在"); continue; } longlength=currentImage.length(); imageLength[executeCount%imageLengthLength]=length; //查看是否需要重新开局 jumpjumpHelper.checkDoReplay(); executeCount++; System.out.println("当前第"+executeCount+"次执行!"); //获取跳棋和底板的中心坐标 int[]result=jumpjumpHelper.getHalmaAndBoardXYValue(currentImage); if(result==null) { System.out.println("TheresultofmethodgetHalmaAndBoardXYValueisnull!"); continue; } inthalmaX=result[0]; inthalmaY=result[1]; intboardX=result[2]; intboardY=result[3]; System.out.println("halmaX:"+halmaX+",halmaY:"+halmaY+",boardX:"+boardX+",boardY:" +boardY); //计算跳跃的距离 doublejumpDistance=jumpjumpHelper.computeJumpDistance(halmaX,halmaY,boardX,boardY); jumpjumpHelper.doJump(jumpDistance); //每次停留2.5秒 TimeUnit.MILLISECONDS.sleep(2500); } } catch(Exceptione) { e.printStackTrace(); } } /** *检查是否需要重新开局 * *@authorLeeHo *@update2017年12月31日下午1:39:18 */ privatevoidcheckDoReplay() { if(imageLength[0]>0&&imageLength[0]==imageLength[1]&&imageLength[1]==imageLength[2] &&imageLength[2]==imageLength[3]&&imageLength[3]==imageLength[4]) { //此时表示已经连续5次图片大小一样了,可知当前屏幕处于再来一局 Arrays.fill(imageLength,0); //模拟点击再来一局按钮重新开局 replayGame(); } } /** *获取指定坐标的RGB值 * *@parambufferedImage *@paramx *@paramy *@authorLeeHo *@update2017年12月31日下午12:12:43 */ privatevoidprocessRGBInfo(BufferedImagebufferedImage,intx,inty) { this.rgbInfo.reset(); intpixel=bufferedImage.getRGB(x,y); //转换为RGB数字 this.rgbInfo.setRValue((pixel&0xff0000)>>16); this.rgbInfo.setGValue((pixel&0xff00)>>8); this.rgbInfo.setBValue((pixel&0xff)); } classRGBInfo { privateintRValue; privateintGValue; privateintBValue; publicintgetRValue() { returnRValue; } publicvoidsetRValue(intrValue) { RValue=rValue; } publicintgetGValue() { returnGValue; } publicvoidsetGValue(intgValue) { GValue=gValue; } publicintgetBValue() { returnBValue; } publicvoidsetBValue(intbValue) { BValue=bValue; } publicvoidreset() { this.RValue=0; this.GValue=0; this.BValue=0; } } }
更多内容大家可以参考专题《微信跳一跳》进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。