C++ 实现微信跳一跳的外挂

时间:2021-07-30 23:25:43

 需要安装手机驱动,用数据线连到电脑
 需要安卓手机
 需要控制台adb程序,并且把adb添加到path系统变量
 代码如下:

#include <windows.h> 
#include "atlimage.h"  
#include <math.h>
#include<time.h>
#include<stdlib.h>

#define WIDTH 	(720/2)
#define HEIGHT 	(1280/2)

HDC		hdc,memdc;		 //窗口绘图句柄 
HWND	hwnd;
HBITMAP bmp;		//空位图
HBITMAP Bmp;		//截图
CImage  image;

BOOL INIT(HWND hwnd);
VOID RUN(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prehInstance,
	LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX wnd = { 0 };				 //定义一个窗口
	wnd.cbSize = sizeof(WNDCLASSEX);     //窗口应该占用内存
	wnd.hInstance = hInstance;           //程序的当前实例句柄
	wnd.style = CS_DROPSHADOW; 
	wnd.hbrBackground = (HBRUSH)5;		 //背景颜色
	wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//鼠标样式  
	wnd.hIcon = NULL;					//程序的图标
	wnd.cbClsExtra = 0;					//程序的附加内存
	wnd.cbWndExtra = 0;					//窗口的附加内存
	wnd.lpszMenuName = NULL;			//菜单
	wnd.lpszClassName = L"jump";		//窗口的类名称
	wnd.lpfnWndProc = WndProc;			//窗口的过程函数

	RegisterClassEx(&wnd);				//检查窗口是否可用

	system("mode con cols=5 lines=5  ");

	HWND hWnd = CreateWindow(L"jump", L"跳一跳外挂", WS_CAPTION | WS_SYSMENU,
		1100, 1,200, 0,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hWnd, nShowCmd);
	UpdateWindow(hWnd);

	if (!INIT(hWnd))
		return 0;

	MSG msg = { 0 };
	while (msg.message != WM_QUIT)
	{
		if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))//消息队列去获得消息
		{
			TranslateMessage(&msg);  //翻译消息  虚拟键值--->字符消息
			DispatchMessage(&msg);   //发送消息 发送给系统 
		}
		else
			RUN(hWnd);
	}

	UnregisterClass(NULL, wnd.hInstance);  //程序准备结束,注销窗口类
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message == WM_DESTROY)
	{
		DeleteObject(bmp);
		DeleteDC(memdc);
		ReleaseDC(hwnd, hdc);
		PostQuitMessage(0);
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//游戏的初始化
BOOL INIT(HWND hwnd)
{
	hdc = GetDC(hwnd);
	memdc = CreateCompatibleDC(hdc);
	bmp = CreateCompatibleBitmap(hdc, WIDTH, HEIGHT);
	SelectObject(memdc, bmp);

	return TRUE;
}

void getRGB(const COLORREF color, BYTE &r, BYTE &g, BYTE &b)
{
	b = BYTE(color >> 16);
	r = BYTE(color << 8 >> 16);
	b = BYTE(color << 16 >> 16);
}

//把背景改为纯色
void turnColor(HDC &memdc)
{
#define CDIF 5//色差
	
	COLORREF oldColor, color;
	BYTE r, g, b, nr, ng, nb;//原颜色与新颜色

	//当前点为背景点
	oldColor = GetPixel(memdc, int(WIDTH*0.02), (int)(HEIGHT*0.4));
	getRGB(oldColor, r, g, b);

	for (int y = (int)(HEIGHT*0.3); y < (int)HEIGHT*0.75; y++)
		for (int x = int(WIDTH*0.02); x < int(WIDTH*0.98); x++)
		{
			color = GetPixel(memdc, x, y);
			getRGB(color, nr, ng, nb);
			if (r<nr + CDIF && r>nr - CDIF && g<ng + CDIF &&
				g>ng - CDIF && b<nb + CDIF && b>nb - CDIF)
				SetPixel(memdc, x, y, oldColor);//设置像素
		}
}

POINT endPt = { 0 }, stratPt = { 0 };
void identification(HDC &memdc)
{
	bool find = true;
	COLORREF path=0;//路的颜色
	COLORREF bottom = GetPixel(memdc, int(WIDTH*0.02), (int)(HEIGHT*0.4));//得到底色

//找下一步位置
	//找下一个物体最上端
	for (int y = (int)(HEIGHT*0.35); y < (int)HEIGHT*0.75; y++)
	{
		for (int x = int(WIDTH*0.2); x < int(WIDTH*0.8); x++)
		{
			path = GetPixel(memdc, x, y);
			if (path != bottom)
			{
				endPt.x = x;
				endPt.y = y;
				find = false;
				break;
			}
		}
		if (!find) break;
	}
	//找下一个物体中点
	int tempX=0;
	for (int y = endPt.y+10; y < endPt.y + 60; y++)
	{
		for (int x = endPt.x; x < int(WIDTH*0.98); x++)
		{
			path = GetPixel(memdc, x, y);
			if (path == bottom)
			{
				if (x > tempX)
					tempX = x;
				else 
				{
					endPt.y = y;
					find = true;
					break;
				}
				break;
			}
		}
		if (find)
			break;
	}

//找人的位置 
	COLORREF blk = 0;
	for (int y = (int)(HEIGHT*0.35); y < (int)(HEIGHT*0.75); y++)
		for (int x = int(WIDTH*0.2); x < int(WIDTH*0.8); x++)
		{
			blk = GetPixel(memdc, x, y);
			BYTE r=204, g=204, b=204;
			getRGB(blk,r, g, b );
			if (r < 65 && r>45 && g < 214 && g>194 && b < 63 && b>43)
			{
				BYTE nr=204, ng=204, nb=204;
				COLORREF me = GetPixel(memdc, x, y + 60);
				getRGB(me, nr, ng, nb);
				if (nr < 63 && nr>43 && ng < 214 && ng>194 && nb < 65 && nb>45)
				{
					COLORREF jump = GetPixel(memdc, x, y - 10);
					BYTE jr=204, jg=204, jb=204;
					getRGB(jump, jr, jg, jb);
					if (jump==path)
					{
						stratPt.x = x;
						stratPt.y = y + 70;
						char str[50] = { 0 };
						int dis = (int)(sqrt((stratPt.x - endPt.x)*(stratPt.x - endPt.x) +
							(stratPt.y - endPt.y)*(stratPt.y - endPt.y)) * 3 * 1.35);
						srand((unsigned int)time(NULL));
						int arr[4];
						for (int i = 0; i < 4; i++)
							arr[i] = rand() % 300;
						sprintf_s(str, "adb shell input swipe %d %d %d %d %d",arr[0],arr[1]+200,arr[2],arr[3]+300, dis);
						system(str);
						Sleep(dis);
						return;
					}
				}
			}
		}
}


VOID RUN(HWND hwnd)
{
	system("adb shell screencap -p /sdcard/screen.png");
	system("adb pull /sdcard/screen.png");	
	image.Load(L"screen.png");
	image.Save(L"screen.bmp");

	Bmp = (HBITMAP)LoadImage(NULL,L"screen.bmp",
		IMAGE_BITMAP,WIDTH,HEIGHT,LR_LOADFROMFILE);

	SelectObject(memdc, Bmp);
	turnColor(memdc);//改背景
	identification(memdc);//识别图像并跳跃

	Sleep(650);
	image.~CImage();
	DeleteObject(Bmp);
}