samedi 15 mai 2021

How to handle if statement inside a while loop - Python

A person new to python, I am creating a game bot for my own game, here in this(attached image is down bellow) game this stick man turns and turns away itself randomly.in the meantime I can use the click button to steal his money if you he turns while you are clicking on the button, game will be over.

what I am trying to perform is, taking x,y value of its forehead and and the pixel R value of RGB using pyautogui library and if it matches R value of its forehead I would perform a mouse click. and same for the if it doesn't match.

The problem I get is, when I tried to run the following script it runs perfectly but the mouse click event is fired more than one time.


    import keyboard
    import pyautogui
    
    while not keyboard.is_pressed('q'):
        if pyautogui.pixel(687, 144)[0] != 255:
            print("Looking at you")
            pyautogui.click(681, 662)
        else:
            print("Not looking at you")
            pyautogui.click(681,662)

I tried the upon script with bool variables in the following way, but it will only work onetime for bot Looking at you & Not looking at you once these variables become false the program doesn't work after.


    import keyboard
    import pyautogui
    
    
    boolval1=True
    boolval2=True
    
    while not keyboard.is_pressed('q'):
        if pyautogui.pixel(687, 144)[0] != 255:
            if boolval1:
                print("Looking at you")
                pyautogui.click(681, 662)
                boolval1 = False
        else:
            if boolval2:
                print("Not looking at you")
                pyautogui.click(681,662)
                boolval2 = False

is there anyway I can perform only a single click every time this stick man turns and turns away??

here I have attached a sketch of the game to get a brief idea

enter image description here

Aucun commentaire:

Enregistrer un commentaire