欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信跳一跳自动脚本C#代码实现

程序员文章站 2023-12-17 16:55:16
前言 csdn前阵子推送了篇文章,讲的是微信跳一跳的技术实现,大致浏览,发现难度不高,很适合练手。 思路 adb得到屏幕截图,转换成bitmap逐像素分析图像,得...

前言

csdn前阵子推送了篇文章,讲的是微信跳一跳的技术实现,大致浏览,发现难度不高,很适合练手。

思路

微信跳一跳自动脚本C#代码实现

adb得到屏幕截图,转换成bitmap逐像素分析图像,得到跳跃起点和终点坐标,最后adb按压屏幕进行跳跃 

相关知识

adb创建

·在提前下载adb

·通过 process类 创建进程运行adb 

 process p = new process();
 p.startinfo = new processstartinfo()
 {
 filename = @"e:\adb\adb.exe",
 arguments = str,//要执行的命令
 useshellexecute =false,//拒绝使用系统自带的shell
 redirectstandardinput =true,//接受输入
 redirectstandardoutput =true, //接受输出
 redirectstandarderror =true,//接受错误
 createnowindow =true,//不创建窗口
 };
 p.start();
 string s = p.standardoutput.readtoend();//读取输出
 p.waitforexit();

常用adb指令

·读取手机型号

cmd("shell getprop ro.product.model"); 


·获取屏幕截图

cmd(@"shell screencap -p/sdcard/1.png"); //屏幕截图并保存
cmd(@"pull /sdcard/1.pnge:\adb"); //上传文件 


·按压屏幕

 cmd(string.format("shellinput swipe {0} {1} {2} {3} {4}", x0, y0, x1, y1, time));
 //从0点点击到1点持续time毫秒

adb算是搞定了,现在写个界面,获取屏幕截图! 

微信跳一跳自动脚本C#代码实现

取棋子坐标思路

微信跳一跳自动脚本C#代码实现观察发现
     ·棋子的颜色为固定值,逐取出棋子底部颜色为 rgb(55, 52,92)
     ·棋子的底部y轴坐标在区间[1000,1250] 

实例化gitmap对象,写一个遍历像素点的循环

bitmap bitmap =new bitmap(@"e:\adb\1.png");
 pointchess =newpoint();
 //棋子颜色 color.fromargb(55, 52, 92))
 for (int y = 1000; y < 1250;y++)
 {
  for (int x = 0; x <bitmap.width; x++)
  {
  if(bitmap.getpixel(x,y) == color.fromargb(57, 58, 102))
  {
  chess.x = x;
  chess.y = y;
  break;
  }
  }
  if (chess != new point())
  {
  break;
  }
 }
 if (chess == new point())
 {
  messagebox.show("找不到棋子!初始化失败!");
  bitmap.dispose();
  return;
 }

底部坐标被正确的取了出来

 微信跳一跳自动脚本C#代码实现

完美!现在取出顶点和底部坐标!

微信跳一跳自动脚本C#代码实现观察发现
·背景颜色为渐变色,所以横向比较,与前一个点差别最大的点就是顶点
·平面颜色一般为纯色,也可能是渐变色,所以得到顶点后作竖向比较,最后一个与前点      差别最大的点就是底部坐标
·顶点的y轴坐标在区间[650-1050] 

首先写一个判断颜色相似度的方法 

bool colorabout(colorcolor0, color color1)
 {
 int i = 20; //颜色差值
 int r =math.max(color0.r,color1.r)- math.min(color0.r, color1.r);
 int g = math.max(color0.g,color1.g) - math.min(color0.g, color1.g);
 int b = math.max(color0.b,color1.b) - math.min(color0.b, color1.b);
 return!((math.max(math.max(r,g),b) + math.min(math.min(r, g), b)) > i);
 }

还是写一个遍历点的循环,调用颜色相似度方法作横向比较取出顶点坐标和底部坐标

point rectvertex = new point();
 point rectend = new point();
 
 for (int y = 650; y < 1050;y++)
 {
  for (int x = 1; x <bitmap.width; x++)
  {
  booliscolorabout = !colorabout(bitmap.getpixel(x - 1, y), bitmap.getpixel(x, y));
  if ((x < chess.x - 75 || x > chess.x + 75)&& iscolorabout) //排除棋子坐标,避免错误的将棋子作顶点
  {
  rectvertex.x = x;
  rectvertex.y = y;
  break;
  }
  }
  if (rectvertex !=new point())
  {
  break;
  }
 }
 if (rectvertex ==new point())
 {
  messagebox.show("未知的物体!初始化失败!");
  bitmap.dispose();
  return;
 }
 
 
 colorrectcolor = bitmap.getpixel(rectvertex.x,rectvertex.y+1);
 if (rectend == new point())
 {
  for (int y = rectvertex.y; y< 1200; y++)
  {
  booliscolorabout = colorabout(rectcolor, bitmap.getpixel(rectvertex.x, y));
  if(iscolorabout)
  {
  rectend.x = rectvertex.x;
  rectend.y = y;
  }
  }
 }

ok!取出了坐标剩下的就是计算距离(正好前几天才学的两点距离公式)和跳跃了!开始循环!

lanq 2017.1.6 github-wecharjump

抛砖引玉 仅供学习!

更多内容大家可以参考专题《微信跳一跳》进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: