0
from PIL import ImageGrab
import keyboard
import threading

threads = []

play_num = input('are you playing as player (1) or (2)> ')

def get_img(player_num):
    image = ImageGrab.grab()
    if player_num == '1':
        b1 = image.getpixel((246, 144))
        b2 = image.getpixel((426, 151))
        b3 = image.getpixel((591, 136))
        b4 = image.getpixel((761, 151))
        return b1 and b2 and b3 and b4
    elif player_num == '2':
        b1 = image.getpixel((1161, 151))
        b2 = image.getpixel((1325, 164))
        b3 = image.getpixel((1489, 128))
        b4 = image.getpixel((1671, 149))
        return b1 and b2 and b3 and b4
    else:
        quit()


def arrow_one(color_index):
    if not color_index[0] == 255:
        pass
    else:
        keyboard.press('a')
        keyboard.release('a')

def arrow_two(color_index):
    if not color_index[0] == 255:
        pass
    else:
        keyboard.press('s')
        keyboard.release('s')

def arrow_tree(color_index):
    if not color_index[0] == 255:
        pass
    else:
        keyboard.press('d')
        keyboard.release('d')

def arrow_four(color_index):
    if not color_index[0] == 255:
        pass
    else:
        keyboard.press('f')
        keyboard.release('f')


def stop_code():
    if keyboard.is_pressed('a' and 'c'):
        print('gg')
        quit()


if __name__ == '__main__':
    while True:
        if keyboard.is_pressed('space'):
            while True:
                color1, color2, color3, color4 = get_img(play_num)
                for _ in range(1):
                    stop_check = threading.Thread(target=stop_code())
                    step1 = threading.Thread(target=arrow_one(color1))
                    step2 = threading.Thread(target=arrow_two(color2))
                    step3 = threading.Thread(target=arrow_tree(color3))
                    step4 = threading.Thread(target=arrow_four(color4))
                    step1.start()
                    step2.start()
                    step3.start()
                    step4.start()
                    stop_check.start()
                    threads.append(step1 and step2 and step3 and step4 and stop_check)

                for thread in threads:
                    thread.join()
        else:
            pass

Each funtion goes through to check if its pixel is white or not, and if its not white, it presses the button accordingly

the get_img fucntion is what gets all the colors and returns b1, b2, ect..., It shows that its expecting 4 (becuase i have 4 things connected to it), however I get the error:

ValueError: not enough values to unpack (expected 4, got 3)

but I clearly have 4 values being returned and im completly stumped, i would appreciate any help given.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 2
    You are expected to return 4 distinct variables `return b1, b2, b3, b4` within the function `get_img()` – Jeru Luke Jun 21 '22 at 17:10
  • 2
    BTW, `threads.append(a and b and c and d and e)` does not do what you want. It'll be identical to either `threads.append(e)` or `threads.append(None)` (potentially substituting some other falsey value in place of `None`). – Charles Duffy Jun 21 '22 at 17:18

1 Answers1

1

Python is not English. To return a tuple of 4 values use b1,b2,b3,b4 not b1 and b2 and b3 and b4. In Python and is a logical-AND so the result is the last value if all the values are "truthy" (True, non-zero and non-empty objects are "truthy"), and the first "falsey" value if any values are "falsey" (False, zero and empty objects are "falsey").

Example:

>>> b1,b2,b3,b4 = 1,2,0,3
>>> b1 and b2 and b3 and b4    # not all values are non-zero
0
>>> b1,b2,b3,b4 = 1,2,3,4      # result is last value since all are non-zero
>>> b1 and b2 and b3 and b4
4
>>> b1,b2,b3,b4                # A tuple of 4 values (what you want)
(1, 2, 3, 4)

See How do "and" and "or" act with non-boolean values? for more information.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251