借鑒了網上一些大神的代碼和思路,這里整理一下寫出點擊跳躍玩跳一跳這個小游戲的思路
一、整體思路
棋子所在的坐標以及下一步所要到的坐標,根據兩個坐標計算出兩點之間距離進行跳躍。
二、分布思路
1、根據命令截圖獲取初始圖保存到手機,然后上傳到本地文件夾
2、將獲取的截圖放入新建的坐標軸中(matplotlib)
3、通過鼠標點擊事件獲取所在初始坐標以及重點坐標,并計算出直線距離
4、進行跳躍,跳躍完成后清空坐標并更新截圖
三、所用到的相關技術或模塊
1、python3基礎
2、numpy
3、matplotlib
4、python中的os模塊
5、adb工具包
四、代碼
__author__ = '周雁冰'import osimport PIL,numpyimport matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimationimport timeneed_update = True# 獲取手機截圖def get_screen_image(): os.system('adb shell screencap -p /sdcard/screen.png') # 獲取手機當前界面截圖 os.system('adb pull /sdcard/screen.png') # 下載當前截圖到電腦當前文件夾下 return numpy.array(PIL.Image.open('screen.png')) #轉為array返回# 計算弦的長度def jump_to_next(point1, point2): x1, y1 = point1; x2, y2 = point2 distance = ((x2-x1)**2 + (y2-y1)**2)**0.5 # 計算弦長度 os.system('adb shell input swipe 320 410 320 410 {}'.format(int(distance*1))) # 按下橫縱左邊 放開橫縱坐標 按壓時間 2K的屏幕彈跳系數為1# 綁定鼠標單擊事件def on_calck(event, coor=[]): # [(x,y),(x2,y2)] global need_update coor.append((event.xdata, event.ydata)) # 獲取x和y坐標位置放入coor數組中 if len(coor) == 2: jump_to_next(coor.pop(), coor.pop()) # 獲取到兩個坐標后計算長度并清空數組 need_update = True def update_screen(frame): # 更新圖片 global need_update if need_update: time.sleep(1) # 因為跳躍需要時間所以這里需要休眠1s,然后重新獲取圖片 axes_image.set_array(get_screen_image()) need_update = False return axes_image, # 返回元祖figure = plt.figure() # 創建一個空白的的圖片對象/創建畫布axes_image = plt.imshow(get_screen_image(), animated=True) # 把獲取的圖片放進坐標軸figure.canvas.mpl_connect('button_press_event', on_calck)ani = FuncAnimation(figure, update_screen, interval=50, blit=True) # 實例化 FuncAnimation更新畫布圖片 50為50msplt.show() # 展示坐標圖請點擊這里獲取:跳一跳源代碼
更多內容大家可以參考專題《微信跳一跳》進行學習。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答