from PIL import ImageGrab
import time
import os

from pynput.keyboard import Key, Controller


command = ["left", "left", "left"]

def do():

    global add

    image = ImageGrab.grab() #(863, 751, 1091, 767))
    print("Screenshot №" + str(i))

    # restart = image.getpixel((1037, 988))
    # if restart == (47, 238, 186):
    #     from pynput.mouse import Button, Controller
    #     mouse = Controller()
    #     print("RESTART!")
    #     command = ["left", "left", "left"]
    #     return




    color = image.getpixel((928, 491))
    if color == (167, 92, 42) or color == (129, 113, 130):

        add = "right"
        return add

    color2 = image.getpixel((989, 492))
    if color2 == (167, 92, 42) or color == (129, 113, 130):

        add = "left"
        return add

    else:
        print('skip')
        add = "left"
        return add

keyboard = Controller()
time.sleep(2)
for i in range(9999999999999999999999999999999999):


    time.sleep(0.3)


    if i % 2 == 0 or i == 0:
        # print(i)
        do()

    if command[i] == "left":
        keyboard.press(Key.left)
        keyboard.release(Key.left)
        print('\033[31mGo left!\033[0m')

    if command[i] == "right":
        keyboard.press(Key.right)
        keyboard.release(Key.right)
        print('\033[32mGo right!\033[0m')

    command.append(add)
    # os.system("clear")