1.导入pyautogui和time模块。 2.定义屏幕网格的网格宽度和高度。 3.定义get_coordinate()函数获取用户输入的坐标。 4.定义move_mouse()函数,用于将鼠标移动到输入的坐标所在的网格中心。 5.调用get_coordinate()函数和move_mouse()函数。 示例代码如下:
import pyautogui import time
grid_width = 100 # 网格宽度 grid_height = 100 # 网格高度
def get_coordinate(): coord_str = input("Please enter the chess coordinate (e.g. a1): ") x = ord(coord_str[0]) - ord('a') y = int(coord_str[1]) - 1 return (x, y)
def move_mouse(x, y): center_x = x * grid_width + grid_width // 2 center_y = y * grid_height + grid_height // 2 pyautogui.moveTo(center_x, center_y)
coordinate = get_coordinate() move_mouse(coordinate[0], coordinate[1]) time.sleep(1) # 等待1秒钟 pyautogui.click() # 在鼠标位置单击一次