Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import pyautogui as pt
from time import sleep

# Helper function


def nav_to_image(image, clicks, off_x=0, off_y=0):
    position = pt.locateCenterOnScreen(image, confidence=.7)

    if position is None:
        print(f'{image} not found...')
        return 0
    else:
        pt.moveTo(position, duration=.1)
        pt.moveRel(off_x, off_y, duration=.1)
        pt.click(clicks=clicks, interval=.3)

# Moves the character/player
# m = attack
# n = place


def move_character(key_press, duration, action='walking'):
    pt.keyDown(key_press)

    if action == 'walking':
        print('Walking')
    elif action == 'attack':
        pt.keyDown('m')

    sleep(duration)
    pt.keyUp('m')
    pt.keyUp(key_press)

def locate_lava():
    position = pt.locateCenterOnScreen('Images/lava.png', confidence=.4)

    if position is None:
        return False
    else:

        move_character('s', 2)
        print('Found lava!!!')
        return True

    # Start the game
    sleep(3)
    nav_to_image('Images/startgame.png', 3)


What I have tried:

I have tried debugging nothing I tried changeing it to time.sleep(3) nope pls help
Posted
Updated 25-Oct-21 12:53pm
Comments
jeron1 25-Oct-21 18:35pm    
the if-else statement, immediately before the sleep, calls return for all (2) conditions, meaning the sleep() method never gets executed.

1 solution

Quote:
Whats wrong with my Python code it says sleep(3) is unreachable

Because anything after a return is unreachable.
Python
def locate_lava():
    position = pt.locateCenterOnScreen('Images/lava.png', confidence=.4)

    if position is None:
        return False # because of the 'return' anything in the 'if' is unreachable
    else:
        move_character('s', 2)
        print('Found lava!!!')
        return True # because of the 'return' anything in the 'else' is unreachable

    # Because both the 'if' and the 'else' ends with a return, anything after the 'if-else ' is unreachable
    # Start the game
    sleep(3)
    nav_to_image('Images/startgame.png', 3)

You should read carefully the documentation about 'return'.
 
Share this answer
 
v2
Comments
CPallini 26-Oct-21 2:19am    
5.
Patrice T 26-Oct-21 7:00am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900