python3實現(xiàn)跳一跳點擊跳躍
借鑒了網(wǎng)上一些大神的代碼和思路,這里整理一下寫出點擊跳躍玩跳一跳這個小游戲的思路
一、整體思路
棋子所在的坐標以及下一步所要到的坐標,根據(jù)兩個坐標計算出兩點之間距離進行跳躍。
二、分布思路
1、根據(jù)命令截圖獲取初始圖保存到手機,然后上傳到本地文件夾
2、將獲取的截圖放入新建的坐標軸中(matplotlib)
3、通過鼠標點擊事件獲取所在初始坐標以及重點坐標,并計算出直線距離
4、進行跳躍,跳躍完成后清空坐標并更新截圖
三、所用到的相關技術或模塊
1、python3基礎
2、numpy
3、matplotlib
4、python中的os模塊
5、adb工具包
四、代碼
__author__ = '周雁冰' import os import PIL,numpy import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import time need_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的屏幕彈跳系數(shù)為1 # 綁定鼠標單擊事件 def on_calck(event, coor=[]): # [(x,y),(x2,y2)] global need_update coor.append((event.xdata, event.ydata)) # 獲取x和y坐標位置放入coor數(shù)組中 if len(coor) == 2: jump_to_next(coor.pop(), coor.pop()) # 獲取到兩個坐標后計算長度并清空數(shù)組 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() # 創(chuàng)建一個空白的的圖片對象/創(chuàng)建畫布 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為50ms plt.show() # 展示坐標圖
請點擊這里獲?。?a target="_blank" >跳一跳源代碼
更多內容大家可以參考專題《微信跳一跳》進行學習。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)讀取.nc數(shù)據(jù)并提取指定時間與經緯度維度對應的變量數(shù)值
這篇文章主要為大家詳細介紹了如何使用Python語言的netCDF4庫實現(xiàn)讀取.nc格式的數(shù)據(jù)文件,并提取指定維(時間、經度與緯度)下的變量數(shù)據(jù),需要的可以了解下2024-02-02Python深度學習TensorFlow神經網(wǎng)絡基礎概括
這篇文章主要為大家介紹了Python深度學習中TensorFlow神經網(wǎng)絡基礎概括,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10