好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#实现微信跳一跳小游戏的自动跳跃助手开发实战

一、前言:

前段时间微信更新了新版本后,带来的一款h5小游戏[跳一跳]在各朋友圈里又火了起来,类似以前的[打飞机]游戏,这游戏玩法简单,但加上了积分排名功能后,却成了[装逼]的地方,于是很多人花钱花时间的刷积分抢排名。后来越来越多的聪明的[程序哥们]弄出了不同方式不同花样的跳一跳助手(外挂?),有用js实现的、有java实现的、有python实现的,有直接物理模式的、有机械化的、有量尺子的等等,简直是百花齐放啊……

赶一下潮流,刚好有点时间,于是花了一个下午时间,我也弄了一个c#版本的简单实现。

二、实现:

简单的实现流程: 连接手机 -> 获取跳一跳游戏界面 -> 获取位置(棋子位置和要跳跃的落脚点位置) -> 点击棋子跳跃

1、连接手机

电脑要连接并操作安卓手机,一般是通过adb协议连接手机并进行操作。连接手机前要求手机已开启usb调试模式,可通过usb线或者tcp方式连接手机。正常只要电脑安装了adb sdk tools之类的工具包,就会自带有adb命令,所以c#要能操作手机,简单实现就是直接利用现成的adb命令。

手机通过usb线接入电脑后,在cmd窗口输入以下adb devices命令,如果显示有device列表则表示手机已连接成功可以对手机进行操作了。

?

c:\users\k>adb devices

list of devices attached

e832acb device

2、获取游戏界面

获取手机界面的截图可通过以下adb命令获取:

?

adb shell screencap -p [filename]

参数 :

- p 表示截图保存格式为png图像格式。

filename: 截图保存的路径地址(手机路径),如果不输入则将截图数据直接输出到当前控制台会话,否则会将截图保存到相关路径地址(必须有写权限)

为避免文件保存到手机后还要再执行adb pull(拉文件到本地电脑)的操作,所以选择不带filename参数的命令。在c#代码里通过process这个类进行adb命令的调用执行,实现代码如下:

?

var startinfo = new processstartinfo( "adb" , "shell screencap -p" );

startinfo.createnowindow = true ;

startinfo.errordialog = true ;

startinfo.redirectstandardoutput = true ;

startinfo.useshellexecute = false ;

var process = process.start(startinfo);

process.start();

var memostream = new memorystream();

process.standardoutput.basestream.copyto(memostream);

但由于adb client的原因,在它输出的截图数据流中会对'\n'(0a)这个字符替换为''\r\n'(0d0a)这两个字符,并且在测试中还发现不同的手机替换次数还不相同的,有可能替换一次,也有可能替换二次!所以为解决这个问题,先计算在最开始的10字节里的数据出现了多少次'\r'(0d)字符后再出现‘\n'(0a)字符,因为正常的png文件,在文件头的第4,第5个字节位置里会有'\r\n'(0d0a)标志,所以检查出来的出现次数就表示'\n'(0a)被adb client替换了多少次,之后再对整个接收到的数据流进行'\n'(0a)还原(删除无用的'\r'(0d)字符)。

>>统计'\n'被替换了次

?

private static int find0dcount(memorystream stream)

{

int count = 0;

stream.position = 0;

while (stream.position < 10 && stream.position < stream.length)

{

int b = stream.readbyte();

if (b == '\r' )

{

  count++;

}

else if (b == '\n' )

{

  return count;

} else if (count > 0)

{

  count = 0;

}

}

return 0;

}

>>对接受到的截图数据流进行'\n'字符还原            

?

var count = find0dcount(memostream);

var newstream = new memorystream();

memostream.position = 0;

while (memostream.position != memostream.length)

{

  var b = memostream.readbyte();

  if (b == '\r' )

  {

  int c = 1;

  var b1 = memostream.readbyte();

  while (b1 == '\r' && memostream.position != memostream.length)

  {

  c++;

  b1 = memostream.readbyte();

  }

  if (b1 == '\n' )

  {

  if (c == count)

  {

  newstream.writebyte(( byte ) '\r' );

  }

  newstream.writebyte(( byte )b1);

  }

  else

  {

  for ( int i=0; i<c; i++) newstream.writebyte(( byte ) '\r' );

  newstream.writebyte(( byte )b1);

  }

  }

  else {

  newstream.writebyte(( byte )b);

  }

}

return new bitmap(newstream);

3、获取棋子与跳跃落脚点位置

将获取到的手机界面截图显示到软件窗体上的picturebox控件上,可用鼠标的左右键分别点击图片位置标示棋子位置和需要跳的落脚点位置,鼠标点击的坐标位置即表示手机界面的坐标位置。由于手机界面截图在picturebox控件显示时为了能一屏全图显示,对图片做了缩放处理,且图片缩放后如果图片的宽度小于picturebox控件的宽度,picturebox会将图片居中后显示。所以鼠标点击的坐标位置还需要进行坐标转换才可以映射为手机界面里的绝对坐标位置。

转换计算方法:先计算picturebox控件的图片缩放值和图片显示的左边距,然后再对鼠标点击坐标进行缩放计算。代码如下:      

?

private point calpoint(point p)

  {

  if ( this .cbzoom. checked && this .picturebox1.image != null )

  {

  var zoom = ( double ) this .picturebox1.height / this .picturebox1.image.height;

  var width = ( int )( this .picturebox1.image.width * zoom);

  var left = this .picturebox1.width / 2 - width / 2;

  return new point(( int )((p.x - left) / zoom), ( int )(p.y / zoom));

  }

  else

  {

  return p;

  }

  }

如全靠手动鼠标点击坐标位置来玩游戏,这和直接在手机里手动玩游戏是没有什么区别的,区别只在于能够跳跃精准些(跳跃力度能自动计算出,下面会讲),所以程序还要能够实现自动化,就是要能够自动找出棋子与跳跃落脚点的位置。

a、找棋子的坐标位置

棋子的位置非常的好找,对游戏界面里的棋子(图2黄色块)进行放大可以发现棋子底部有一块区域(图3白色块)的颜色值是固定的r(54)g(60)b(102)颜色,如下两图:

(图2)

(图3)

根据棋子的这一颜色特点在获取到手机界面截图时,对图片象素进行扫描,查找r(54)g(60)b(102)这一颜色,找到的坐标位置就是棋子的位置。为了能快速扫描图片,不采用效率较低下的getpixel方法获取颜色值,而采用lockbits方法锁定图片数据到内存,再采用指针移动获取象素颜色,由于采用了指针,代码需要开启unsafe定义。且棋子正常情况下不会在最顶部和最底部出现,所以不需要对整张界面图片扫描,只扫描20%-63%区域的数据,并且从底部开始找起。

b、找跳跃的落脚点位置

写此助手只是无聊时的产出物,所以我只是简单实现。游戏中如果连续跳到了目标物的中间位置时,新目标物的中间部分会出现一个白色圈(如上图2的红色块),如果再跳中此位置,会进行加分。根据这一特点,程序找出那一白色圈圈的位置即可做为落脚点位置,白色圈的颜色值为r(254)g(254)b(254),如果没有此白色圈位置,则手动鼠标选择落脚点位置。实现此功能后,程序基本上也能实现90%左右的自动化跳跃了。

查找代码实现如下:

?

private static point findpointimpl(bitmap bitmap, out point combopoint)

  {

  var standpcolor = color.fromargb(54, 60, 102);

  var combopcolor = color.fromargb(245, 245, 245);

 

  point standpoint = point.empty;

  combopoint = point.empty;

 

  int y1 = ( int )(bitmap.height * 0.2);

  int y2 = ( int )(bitmap.height * 0.63);

 

  pixelformat pf = pixelformat.format24bpprgb;

 

  bitmapdata bitmapdata = bitmap.lockbits( new rectangle(0, y1, bitmap.width, y2), imagelockmode. readonly , pf);

  try

  {

  unsafe

  {

   int w = 0;

   while (y2 > y1)

   {

   byte * p = ( byte *)bitmapdata.scan0 + (y2 - y1 - 1) * bitmapdata.stride;

   w = bitmap.width;

   int endcolorcount = 0;

   while (w > 40)

   {

   iccolor* pc = (iccolor*)(p + w * 3);

   if (standpoint == point.empty &&

   pc->r == standpcolor.r && pc->g == standpcolor.g && pc->b == standpcolor.b)

   {

   standpoint = new point(w - 3, y2);

   if (combopoint != point.empty) break ;

   }

   else if (combopoint == point.empty)

   {

   if (pc->r == combopcolor.r && pc->g == combopcolor.g && pc->b == combopcolor.b)

   {

    endcolorcount++;

   }

   else

   {

    if (endcolorcount > 0)

    {

    combopoint = new point(w + 5, y2 - 1);

    if (standpoint != point.empty) break ;

    }

    endcolorcount = 0;

   }

   }

   w--;

   }

   if (combopoint == point.empty)

   {

   if (endcolorcount > 10)

   {

   combopoint = new point(w + 5, y2 - 1);

   }

   }

   if (standpoint != point.empty && combopoint != point.empty) break ;

   y2--;

   }

  }

  return standpoint;

  }

  finally

  {

  bitmap.unlockbits(bitmapdata);

  }

  }

4、棋子跳跃

要能跳跃,首先需要知道一个蓄力时间,就是按住棋子多久的时间,此蓄力时间的计算公式如下:

蓄力时间 = 距离 * 力度系数

[距离]就是棋子位置与跳跃落脚点位置的距离,根据上面的方法得出这两个位置的坐标点后,根据直角三角形的勾股定理即可求出,代码如下:      

?

public double distance

{

get

{

if (! this .cando) return -1;

int w = math.abs( this .p2.x - this .p1.x);

int h = math.abs( this .p2.y - this .p1.y);

return math.sqrt(( double )(w * w) + (h * h));

}

}

[力度系数]  是一个常量值,具体怎么定义没去细查,我采用的计算公式是: [力度系数 = 1495 / 手机分辨率的宽度值], 如我的手机分辨率是1080*1920,则力度系数就是 1495 / 1080 = 1.3842....

算出了蓄力时间后通过以下adb命令发送到手机即可模拟点击操作。

?

adb shell input swipe <x1> <y1> <x2> <y2> [duration(ms)]

x1, y1 就是棋子的坐标位置

x2, y2 还是棋子的坐标位置

duration 蓄力时间值,由距离*力度系数得出。

代码如下:   

?

public bool do ()

{

if (! this .cando) return false ;

var startinfo = new processstartinfo( "adb" , string .format( "shell input swipe {0} {1} {0} {1} {2}" , this .p1.x, this .p1.y, this .time));

startinfo.createnowindow = true ;

startinfo.errordialog = true ;

startinfo.useshellexecute = false ;

var process = process.start(startinfo);

return process.start();

}

三、结束语

程序实现很简单,都是通过adb命令与手机进行交互操作。如果你认为对你有帮助麻烦赞下即可:)积分别玩太过哦。

可执行文件下载地址: jumperhelper.rar

代码仓库:https://github.com/kingthy/jumperhelper

总结

以上所述是小编给大家介绍的c#实现微信跳一跳小游戏的自动跳跃助手开发实战,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

原文链接:https://www.cnblogs.com/kingthy/p/jumperhelper.html

dy("nrwz");

查看更多关于C#实现微信跳一跳小游戏的自动跳跃助手开发实战的详细内容...

  阅读:51次