1. 程式人生 > 實用技巧 >2020.10.13 第十課 跳一跳小程式專案

2020.10.13 第十課 跳一跳小程式專案

跳一跳程式

1.獲取手機的實時畫面

2.獲取人物起點座標

3.獲取人物終點座標

4.根據兩點求取距離長度

5.根據距離/速度得到按壓時長

6.根據時長模擬人物按壓手機螢幕

1.adb工具截圖,按壓手機螢幕

adb shell screencap-p /sdcard/screen.png
adb pull /sdcard/screen.png
 
//                   按下的座標  抬手座標 按壓時長
adb shell input swipe 200 200  195 195  x
system("xxx")放進去
    
    input swipe [duration(ms)] 
向裝置傳送一個滑動指令,並且可以選擇設定滑動時長。 
//滑動操作  //從 100 100 經歷300毫秒滑動到 200 200 
adb shell input swipe 100 100 200 200  300 
  
//長按操作
adb shell input swipe 100 100 100 100  1000 //在 100 100 位置長按 1000毫秒

2.修改圖片格式

//因為adb只能擷取.png
//easyx顯示圖片只能顯示.jpg .bmp格式圖片
//格式轉換

#include<atlimage.h>
CImage cimage;
cimage.Load("screen.png");
cimage.Save("screen.jpg");

3.貼圖三部曲

1.定義2.載入3.顯示
 //init=initialization(初始化)
 //graph=圖表
 #include<graphics.h>
initgraph(1080,1000,SHOWCONSOLE);//顯示控制檯
IMAGE img;
loadimage(&img,"screen.jpg")
putimage(0,0,&img)//0,-600
 //電腦為1920*1080 手機截圖為1080*1920
IMAGE img;
SetWorkingImage(&screen);//設定繪圖目標為screen
getimage(&img, 0,700, 1080, 1000);
/*getimage() 儲存影象函式
功能: 函式getimage()儲存左上角與右下角所定義的螢幕上畫素圖形到指定的記憶體區域。*/
SetWorkingImage(NULL);
putimage(0, 0, &img);
		

4.起點座標

/*getpixel()函式功能:該函式檢索指定座標點的畫素的RGB顏色值。
COLORREF GetPixel(HDC hdc, int nXPos, int nYPos)
hdc:裝置環境控制代碼。
nXPos:指定要檢查的畫素點的邏輯X軸座標。
nYPos:指定要檢查的畫素點的邏輯Y軸座標。*/
//RGB(紅,綠,藍)

int x, y;

		for (y = 1000; y >= 0; y--) {
			int flag = 0;
			for (x = 0; x <1080; x++) {
				if (RGB(55, 60, 100) == getpixel(x, y)) {
					flag = 1;
					break;
				}
				//if(flag==1)break;
			}

			if (flag == 1)break;

		}
		printf("起點座標為:(%d,%d)\n", x, y);

5.終點座標

COLORREF是一個32-bit整型數值,它代表了一種顏色。你可以使用RGB函式來初始化COLORREF,它的定義:
COLORREF變數有兩種賦值方法。
第一種:
COLORREF color = RGB(R,G,B);
第二種:
CColorDialog colorDialog;
COLORREF color;
if( colorDialog.DoModal() == IDOK )
{
    color = colorDialog.GetColor();
}
第二種方法使用了MFC中的顏色對話方塊。
如何從 COLORREF中取出RGB分量值?
可以使用巨集GetRValue、GetGValue、GetBValue。
double (Similar(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);

	return sqrt(double((R1 - R2) * (R1 - R2) + (G1 - G2) * (G1 - G2) + (B1 - B2) * (B1 - B2)));
}
int main(){
COLORREF bj = getpixel(30, 30);
	
		int dx, dy;
		for (dy = 0; dy < 1000; dy++) {
			int flag1 = 0;
			for (dx = 0; dx < 1080; dx++) {
				if (Similar(bj, (getpixel(dx, dy)) )>25) {
					flag1 = 1;
					break;
				}

			}
			if (flag1 == 1)break;
		}
}
		printf("終點座標為(%d,%d)\n", dx, dy);

6.時間=距離/速度

//距離  
double d=sqrt(double( (x-dx)*(x-dx)+(y-dy)*(y-dy) ) );
printf("距離=%f", d);

//速度1.18-1.25
double time=d/1.18

7.按壓時間

char str[128];
sprintf(str,"adb shell input swipe 200 200 195 195 %d",(int)time);
system(str)