如何跟踪点击次数而不是两次点击同一个地方

问题描述

这是我的代码

while (1):
    pic = pyautogui.screenshot(region=(200,150,1600,800))
    width,height = pic.size
    for x in range (0,width,1):
    
        for y in range (0,height,1):

            r,g,b = pic.getpixel((x,y))
            if r == 71 and g == 38:

                click(x+200,y+150)
                
                time.sleep(0.5)
               
                if pyautogui.locateOnScreen('kalk.png',grayscale=True,confidence=0.8) != None: 
                    click(1111,906)
                    time.sleep(0.5)
                    click(1155,165)
                    time.sleep(0.5)
                    click(1342,994)
                    pyautogui.press('a')
                    time.sleep(0.5)
                    pyautogui.press('a')
                    time.sleep(0.5)
                    pyautogui.press('a')
                else: 
                    pyautogui.press('ctrl')
                    continue

我想确保我的代码不能两次点击同一个地方,而是记住它点击过的地方,而不是第二次点击同一个地方。我该怎么做?

解决方法

因为您的所有坐标对都可以表示为一个元组,所以您可以使用 set 来跟踪已单击的位置,并且仅单击尚未单击的位置:

while(1):
    pic = pyautogui.screenshot(region=(200,150,1600,800))
    width,height = pic.size
    clicked = set() # Create set
    for x in range (0,width,1):
        for y in range (0,height,1):
            r,g,b = pic.getpixel((x,y))
            if r == 71 and g == 38 and (x,y) not in clicked:  # Check if coordinates already clicked
                clicked.add((x,y))  # Mark coordinates as clicked
                click(x+200,y+150)
                
                ...