为什么这会抛出索引?

发布于 2025-01-17 19:47:17 字数 1726 浏览 1 评论 0原文

免责声明:我不知道如果您要运行代码会发生什么,因为我已经对其进行了一些更改。

对于那些好奇的人来说,代码的作用都没关系:它可以使像素艺术绘画游戏自动化,其中正方形以数字为标志,您可以用相应的颜色单击它们。这并不是该代码的作用,但这并不重要。

错误: ”在此处输入图像描述

from pyautogui import *
import pyautogui
import win32api, win32con

white = (198, 198, 196)
blue = (125, 149, 199)

x1, y1, width, height = 10, 20, 1029, 798

x2 = x1 + width
y2 = y1 + height

while True:
    s = pyautogui.screenshot(region=(x1, y1, width, height))
    for x in range(x1, x2):
        for y in range(y1, y2):
            if s.getpixel((x, y)) == blue:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            if s.getpixel((x, y)) == white:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
                time.sleep(0.01)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            elif s.getpixel((x, y)) != white and s.getpixel((x, y)) != blue:
                break
            else:
                continue

Disclaimer: I don't know exactly what would happen if you were to run the code since I've made some changes to it.

It doesn't matter what the code does but for those curious: It automates pixel art painting games where squares are marked by numbers and you click on them with the corresponding color. That's not exactly what this code does but it doesn't matter.

The error: enter image description here

from pyautogui import *
import pyautogui
import win32api, win32con

white = (198, 198, 196)
blue = (125, 149, 199)

x1, y1, width, height = 10, 20, 1029, 798

x2 = x1 + width
y2 = y1 + height

while True:
    s = pyautogui.screenshot(region=(x1, y1, width, height))
    for x in range(x1, x2):
        for y in range(y1, y2):
            if s.getpixel((x, y)) == blue:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            if s.getpixel((x, y)) == white:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
                time.sleep(0.01)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            elif s.getpixel((x, y)) != white and s.getpixel((x, y)) != blue:
                break
            else:
                continue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2025-01-24 19:47:17

屏幕截图图像中的像素索引从 0 开始,而不是从 x1y1 开始。所以你需要从你的索引中减去它们。

while True:
    s = pyautogui.screenshot(region=(x1, y1, width, height))
    for x in range(x1, x2):
        for y in range(y1, y2):
            curpixel = s.getpixel((x - x1, y - y1))
            if curpixel == blue:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            if curpixel == white:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
                time.sleep(0.01)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            elif scurpixel != white and curpixel != blue:
                break
            else:
                continue

Pixel indexes in the screenshot image start from 0, not x1 and y1. So you need to subtract them from your indexes.

while True:
    s = pyautogui.screenshot(region=(x1, y1, width, height))
    for x in range(x1, x2):
        for y in range(y1, y2):
            curpixel = s.getpixel((x - x1, y - y1))
            if curpixel == blue:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            if curpixel == white:
                s = pyautogui.screenshot(region=(x1, y1, width, height))
                win32api.SetCursorPos((range(x1, x2), range(y1, y2)))
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
                time.sleep(0.01)
                win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)
                time.sleep(0.01)
                s = pyautogui.screenshot(region=(x1, y1, width, height))
            elif scurpixel != white and curpixel != blue:
                break
            else:
                continue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文