C++ 寫微信跳一跳

// Jump.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。

#include "pch.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
IMAGE image,img;


//判斷顏色相似度
double Simailarity(COLORREF color1,COLORREF color2)
{
    int r1 = GetRValue(color1);
    int g1 = GetGValue(color1);
    int b1 = GetBValue(color1);
    
    int r2 = GetRValue(color2);
    int g2 = GetGValue(color2);
    int b2 = GetBValue(color2);


    //pow x的y次方
    return sqrt(double((r1 - r2)*(r1 - r2) + (g1 - g2)*(g1 - g2) + (b1 - b2)*(b1 - b2)));
}

int main()
{
    initgraph(690, 790,SHOWCONSOLE);
    while (true)
    {

        system("adb shell screencap -p /sdcard/screen.png");
        system("adb pull /sdcard/screen.png");
        loadimage(&image, L"screen.png", 720, 1440);
        putimage(0, 0, &image);
        SetWorkingImage(&image);
        getimage(&img, 5, 300, 690, 790);
        SetWorkingImage(NULL);
        putimage(0, 0, &img);


        int pex, pey;//人的座標
        for (int y = 789; y >= 0; y--)
        {
            int flag = 0;
            for (int x = 0; x <= 789; x++)
            {

                if (Simailarity(getpixel(x, y),RGB(54, 60, 102))<5)
                {
                    pex = x;
                    pey = y;
                    flag = 1;
                    break;
                }
            }
            if (flag)
                break;
        }


        int dx, dy;
        if (pex < 350)//人在左邊
        {
            printf("人物座標:pex=%d pey=%d\n", pex, pey);
            int background = getpixel(0, 0);
            for (int y = 0; y < 690; y++)
            {
                int flag = 0;
                for (int x = pex+80; x < 690; x++)
                {
                    if (Simailarity(getpixel(x, y), background) > 20)
                    {
                        dx = x;
                        dy = y;
                        flag = 1;
                        break;
                    }
                }
                if (flag)
                    break;
            }

        }
        else//人在右邊
        {
            printf("人物座標:pex=%d pey=%d\n", pex, pey);
            int background = getpixel(0, 0);
            for (int y = 0; y < 690; y++)
            {
                int flag = 0;
                for (int x = 0; x < pex-80; x++)
                {
                    if (Simailarity(getpixel(x, y), background) > 20)
                    {
                        dx = x;
                        dy = y;
                        flag = 1;
                        break;
                    }
                }
                if (flag)
                    break;
            }

        }
        printf("定點座標:dx=%d dy=%d\n", dx, dy);
        int tarx, tary;
        tarx = dx;
        if (pex < 350)//人在做,跳右
        {
            tary = dy + 20;
        }
        else//人在右,跳做
        {
            tary = dy + 30;
        }
        double dis = sqrt(double((tary - pey)*(tary - pey) + (tarx - pex)*(tarx - pex)));
        double time = 1.97 * dis;
        char str[100];
        sprintf_s(str, "adb shell input swipe 0 0 0 0 %d", (int)time);
        system(str);
        Sleep(500);
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章