为什么这会抛出索引?
免责声明:我不知道如果您要运行代码会发生什么,因为我已经对其进行了一些更改。
对于那些好奇的人来说,代码的作用都没关系:它可以使像素艺术绘画游戏自动化,其中正方形以数字为标志,您可以用相应的颜色单击它们。这并不是该代码的作用,但这并不重要。
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
屏幕截图图像中的像素索引从
0
开始,而不是从x1
和y1
开始。所以你需要从你的索引中减去它们。Pixel indexes in the screenshot image start from
0
, notx1
andy1
. So you need to subtract them from your indexes.