1. 程式人生 > >python 實現 跳一跳遊戲 代碼解析

python 實現 跳一跳遊戲 代碼解析

起點 inpu enca log image nec sta .fig 電腦

這個代碼實現的是 手動點擊起點 和 終點 ,程序自動判斷距離、觸屏時間 完成跳躍

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
import math
import time
import os

def pull_screenshot(): 定義 截取手機屏幕 並 發送到電腦函數 os.system(adb shell screencap -p /sdcard/autojump.png) 發送 截屏命令 到手機 os.system(
adb pull /sdcard/autojump.png .) 發送 拉取圖片到電腦 命令 def jump(distance): 定義 跳躍函數 形參為距離 press_time = distance * 1.35 計算按屏幕 時間 press_time = int(press_time) cmd = adb shell input swipe 320 410 320 410 + str(press_time) 按屏幕命令 print(cmd) os.system(cmd) 發送 按屏幕命令 fig = plt.figure() 自定義畫布大小 的對象 index
= 0 cor = [0, 0] pull_screenshot() 執行截屏函數 img = np.array(Image.open(autojump.png)) update = True click_count = 0 cor = [] def update_data(): return np.array(Image.open(autojump.png)) im = plt.imshow(img, animated=True) def updatefig(*args): global update if update: time.sleep(
1.5) pull_screenshot() im.set_array(update_data()) update = False return im, def onClick(event): global update global ix, iy global click_count global cor # next screenshot ix, iy = event.xdata, event.ydata coords = [] coords.append((ix, iy)) print(now = , coords) cor.append(coords) click_count += 1 if click_count > 1: click_count = 0 cor1 = cor.pop() cor2 = cor.pop() distance = (cor1[0][0] - cor2[0][0])**2 + (cor1[0][1] - cor2[0][1])**2 distance = distance ** 0.5 print(distance = , distance) jump(distance) update = True fig.canvas.mpl_connect(button_press_event, onClick) ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) plt.show()

python 實現 跳一跳遊戲 代碼解析