[python]fps反应测试

90ms

import time
import keyboard
import win32api
import win32con
from PIL import ImageGrab

TARGET_COLOR = (75, 219, 106)

def click(x, y):
    win32api.SetCursorPos((x, y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    time.sleep(0.01)  # 添加 0.01 秒的延迟
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

while not keyboard.is_pressed('g'):
    # 获取当前鼠标位置
    x, y = win32api.GetCursorPos()
    
    # 获取屏幕图像并检查目标像素的颜色
    screen = ImageGrab.grab()
    pixel_color = screen.getpixel((x, y))
    if pixel_color == TARGET_COLOR:
        click(x, y)

print("脚本结束。")

20ms

import time
import keyboard
import win32api
import win32con
import mss

TARGET_COLOR = (75, 219, 106)

def click(x, y):
    win32api.SetCursorPos((x, y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)

def get_pixel_color(x, y, sct):
    # 只抓取1x1像素的区域
    monitor = {"top": y, "left": x, "width": 1, "height": 1}
    img = sct.grab(monitor)
    return img.pixel(0, 0)

with mss.mss() as sct:
    while not keyboard.is_pressed('g'):
        x, y = win32api.GetCursorPos()
        pixel_color = get_pixel_color(x, y, sct)

        if pixel_color == TARGET_COLOR:
            click(x, y)

print("脚本结束。")