File size: 1,127 Bytes
e012066 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # game_controller.py
import subprocess
from PIL import Image
import io
class GameController:
def __init__(self, device_id="emulator-5554"):
self.device_id = device_id
def screenshot(self):
"""截图并返回PIL Image对象"""
cmd = f"adb -s {self.device_id} exec-out screencap -p"
output = subprocess.check_output(cmd, shell=True)
img = Image.open(io.BytesIO(output))
return img
def tap(self, x, y):
"""点击指定坐标"""
cmd = f"adb -s {self.device_id} shell input tap {x} {y}"
subprocess.run(cmd, shell=True)
def swipe(self, x1, y1, x2, y2, duration=50):
"""滑动操作"""
cmd = f"adb -s {self.device_id} shell input swipe {x1} {y1} {x2} {y2} {duration}"
subprocess.run(cmd, shell=True)
def get_screen_size(self):
"""获取屏幕分辨率"""
cmd = f"adb -s {self.device_id} shell wm size"
output = subprocess.check_output(cmd, shell=True).decode()
size_str = output.split(":")[1].strip()
w, h = map(int, size_str.split("x"))
return w, h |