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

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

前言

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

思路

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

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

相关知识

ADB创建

·在https://adb.clockworkmod.com提前下载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

抛砖引玉 仅供学习!

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

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