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