微信跳一跳自動(dòng)腳本C#代碼實(shí)現(xiàn)
前言
CSDN前陣子推送了篇文章,講的是微信跳一跳的技術(shù)實(shí)現(xiàn),大致瀏覽,發(fā)現(xiàn)難度不高,很適合練手。
思路
ADB得到屏幕截圖,轉(zhuǎn)換成bitmap逐像素分析圖像,得到跳躍起點(diǎn)和終點(diǎn)坐標(biāo),最后ADB按壓屏幕進(jìn)行跳躍
相關(guān)知識(shí)
ADB創(chuàng)建
·在https://adb.clockworkmod.com提前下載ADB
·通過 Process類 創(chuàng)建進(jìn)程運(yùn)行ADB
Process p = new Process(); p.StartInfo = new ProcessStartInfo() { FileName = @"E:\adb\adb.exe", Arguments = str,//要執(zhí)行的命令 UseShellExecute =false,//拒絕使用系統(tǒng)自帶的Shell RedirectStandardInput =true,//接受輸入 RedirectStandardOutput =true, //接受輸出 RedirectStandardError =true,//接受錯(cuò)誤 CreateNoWindow =true,//不創(chuàng)建窗口 }; p.Start(); string s = p.StandardOutput.ReadToEnd();//讀取輸出 p.WaitForExit();
常用ADB指令
·讀取手機(jī)型號(hào)
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點(diǎn)點(diǎn)擊到1點(diǎn)持續(xù)time毫秒
ADB算是搞定了,現(xiàn)在寫個(gè)界面,獲取屏幕截圖!
取棋子坐標(biāo)思路
觀察發(fā)現(xiàn)
·棋子的顏色為固定值,逐取出棋子底部顏色為 RGB(55, 52,92)
·棋子的底部y軸坐標(biāo)在區(qū)間[1000,1250]
實(shí)例化Gitmap對(duì)象,寫一個(gè)遍歷像素點(diǎn)的循環(huán)
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; }
底部坐標(biāo)被正確的取了出來
完美!現(xiàn)在取出頂點(diǎn)和底部坐標(biāo)!
觀察發(fā)現(xiàn)
·背景顏色為漸變色,所以橫向比較,與前一個(gè)點(diǎn)差別最大的點(diǎn)就是頂點(diǎn)
·平面顏色一般為純色,也可能是漸變色,所以得到頂點(diǎn)后作豎向比較,最后一個(gè)與前點(diǎn) 差別最大的點(diǎn)就是底部坐標(biāo)
·頂點(diǎn)的y軸坐標(biāo)在區(qū)間[650-1050]
首先寫一個(gè)判斷顏色相似度的方法
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); }
還是寫一個(gè)遍歷點(diǎn)的循環(huán),調(diào)用顏色相似度方法作橫向比較取出頂點(diǎn)坐標(biāo)和底部坐標(biāo)
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) //排除棋子坐標(biāo),避免錯(cuò)誤的將棋子作頂點(diǎn) { 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!取出了坐標(biāo)剩下的就是計(jì)算距離(正好前幾天才學(xué)的兩點(diǎn)距離公式)和跳躍了!開始循環(huán)!
LanQ 2017.1.6 GitHub-WeCharJump
拋磚引玉 僅供學(xué)習(xí)!
更多內(nèi)容大家可以參考專題《微信跳一跳》進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用FlubuCore用C#來寫DevOps腳本的方法詳解
- C#調(diào)用python腳本的方法步驟(2種)
- 在VS2017中用C#調(diào)用python腳本的實(shí)現(xiàn)
- 使用C# 的webBrowser寫模擬器時(shí)的javascript腳本調(diào)用問題
- C#調(diào)用Python腳本的簡(jiǎn)單示例
- 總結(jié)ASP.NET C#中經(jīng)常用到的13個(gè)JS腳本代碼
- C#創(chuàng)建數(shù)據(jù)庫及導(dǎo)入sql腳本的方法
- C#利用ScriptControl動(dòng)態(tài)執(zhí)行JS和VBS腳本
- 在C#中調(diào)用VBScript、javascript等腳本的實(shí)現(xiàn)代碼
- 常用工具之 vb轉(zhuǎn)化C# 數(shù)據(jù)連接串 正則表達(dá)式查詢 源碼世界 腳本字典
- C# 腳本引擎CS-Script的使用
相關(guān)文章
C#5.0中的異步編程關(guān)鍵字async和await
這篇文章介紹了C#5.0中的異步編程關(guān)鍵字async和await,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06C#自寫的一個(gè)HTML解析類(類似XElement語法)
這篇文章主要介紹了C#自寫的一個(gè)HTML解析類(類似XElement語法),本文給出了實(shí)現(xiàn)代碼和使用實(shí)例,同時(shí)給出了測(cè)試HTML實(shí)例,需要的朋友可以參考下2015-06-06