SunAwtCanvas
SunAwtCanvas

Reputation: 1460

PyAutoGui - After clicking on a specific spot, it clicks on an undefined location

Hi i'm so sorry about the way I phrased the title. I think that's the best I can explain e_e.

So instead of explaining, here's a GIF to show u what's my issue. Note: I've only programmed clicks in the middle of the items

enter image description here

Here's the code that triggers the clicking:

INVEN is a list that contains 56 individual values. But the way I programmed it, is to have values in pairs of 2. Meaning, INVEN[0] = X Coords and INVEN[1] = Y Coords

for i in range(len(INVEN) - 1):
   keyboard.press(Key.shift)
   Click("clear_inven", (INVEN[i], INVEN[i + 1]))
   keyboard.release(Key.shift)

Here's the Click code:

def Click(pos, xy):
   i = 0
   while i <= 3:
       if pos.upper() == "CUT_TREE":
           pyautogui.click(xy[0], xy[1])
       elif pos.upper() == "COMPASS":
           pyautogui.click(xy[0], xy[1])
       elif pos.upper() == "CLEAR_INVEN":
           pyautogui.click(xy[0], xy[1])
       i += 1

Thank u so much for reading! I appreciate any help at all :)

Upvotes: 1

Views: 404

Answers (1)

r.ook
r.ook

Reputation: 13858

The problem is here: (INVEN[i], INVEN[i + 1]), your INVEN is not incrementing properly:

INVEN = [674, 362, 716, 362, 758, 362, ...]

# iteration i = 0
INVEN[0] = 674 # x coord
INVEN[1] = 362 # y coord

# iteration i = 1
INVEN[1] = 362 # y coord
INVEN[2] = 716 # x coord

You can see how this quickly goes out of hand.

Instead you want to do this:

for i in range(0, len(INVEN), 2):
   keyboard.press(Key.shift)
   Click("clear_inven", (INVEN[i], INVEN[i + 1]))
   keyboard.release(Key.shift)

So that i is incremented by 2 each time (step=2).

However it is rarely suggested to do range(len(INVEN)) as you can easily just loop through the objects without the range:

for pair in zip(INVEN[::2], INVEN[1::2]):
    Click('something', pair) 

Also it's probably better if you have your x, y set up in tuples:

[(674, 362), (716, 362), (758, 362), ...]

So that it's easier to recall them in pair:

for pair in INVEN:
    Click('something', pair)

As a side note, you might also want to use for loop instead of while in your Click() function:

def Click(pos, xy):
   for i in range(3):
       if pos.upper() == "CUT_TREE":
           pyautogui.click(xy[0], xy[1])
       elif pos.upper() == "COMPASS":
           pyautogui.click(xy[0], xy[1])
       elif pos.upper() == "CLEAR_INVEN":
           pyautogui.click(xy[0], xy[1])

Although I'm not sure why you would want to loop through the same input three times...

Upvotes: 1

Related Questions