Python Forum

Full Version: naming images adding to number within multiple functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to map an image within a game that is a scrollable background. I am trying to figure out how to name the screenshots taken and adding +1 to the image name after every screenshot is taken however it is 3 functions within a function and i'm running into a dead end as i cannot get it to update a string either outside or within the functions, or maybe i am and it's reverting it back to 0. is it possible to do it using a global variable or is there a better way?
def f(x):
    print(currentnumber + x)
currentnumber = 0

def leftscroll():
    def f(x):
        global currentnumber
        print(currentnumber + x)
        currentnumber += 1
    screenshot = r'C:\Users\brady\PycharmProjects\Bot\map' + str(currentnumber) + '.png'
    pyautogui.moveTo(x=193, y=474, duration=0.2)
    pyautogui.screenshot(screenshot,region=(193,180,727,294))
    pyautogui.mouseDown(button='left')
    pyautogui.moveTo(x=990,y=474, duration=0.5)
    time.sleep(0.4)
    pyautogui.mouseUp(button='left')
    return


#next row


def scrollup():
    def f(x):
        global currentnumber
        print(currentnumber + x)
        currentnumber += 1
    screenshot = r'C:\Users\brady\PycharmProjects\Bot\map' + str(currentnumber) + '.png'
    pyautogui.moveTo(x=193, y=180, duration=0.3)
    pyautogui.mouseDown(button='left')
    pyautogui.moveTo(x=193, y=474, duration=0.5)
    time.sleep(0.5)
    pyautogui.mouseUp(button='left')
    pyautogui.screenshot(screenshot, region=(193,180,727,294))
    return
def rightscroll():
    def f(x):
        global currentnumber
        print(currentnumber + x)
        currentnumber += 1
    screenshot = r'C:\Users\brady\PycharmProjects\Bot\map' + str(currentnumber) + '.png'
    pyautogui.moveTo(x=990, y=474, duration=0.2)
    pyautogui.mouseDown(button='left')
    pyautogui.moveTo(x=193,y=474, duration=0.5)
    time.sleep(0.4)
    pyautogui.mouseUp(button='left')
    pyautogui.screenshot(screenshot, region=(193,180,727,294))
    return
def repeat_fun(times, j):
    for i in range(times): j()



while pyautogui.locateOnScreen('endimage.png') == None:
    def f(x):
        global currentnumber
        print(currentnumber + x)
        currentnumber += 1
    repeat_fun(5,leftscroll)
    print(currentnumber)
    pyautogui.locateOnScreen('endimage.png', confidence=0.8)
    scrollup()
    print(currentnumber)
    repeat_fun(5,rightscroll)
    print(currentnumber)
    pyautogui.locateOnScreen('endimage.png', confidence=0.8)
    scrollup()

got it