Python Forum

Full Version: Non repetitive mouse click
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am writing a game bot as part of my learning process. The code I am writing requires a single mouse click to be performed at the start of the code block and then move on to the other actions that need to be performed. While I can get the mouse click to occur, the program is continuously sending mouse clicks and will not move on to the next set of instructions.

I have looked at different code segments and designs, including bringing this section outside the loop, but none of it is working. Any help would be appreciated

time.sleep(5)

#DEFINITIONS

def click(x,y):
    win32api.SetCursorPos((x,y))
    time.sleep(0.9)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) 

def debuff(key):
    pyautogui.keyDown("4")
    time.sleep(0.1)
    pyautogui.keyUp("4")
    time.sleep(0.1)

def attack1(key):
    pyautogui.keyDown("8")
    time.sleep(0.1)
    pyautogui.keyUp("8")
    time.sleep(0.9)    

def attack2(key):
    pyautogui.keyDown("2")
    time.sleep(0.1)
    pyautogui.keyUp("2")
    time.sleep(0.9)


#Used to stop the program
while keyboard.is_pressed('g') == False:
    
        #TARGET ENEMY <<< THIS IS WHAT I NEED TO ONLY RUN ONE TIME>>>>
        if pyautogui.pixel (1096,127) [0] == 16:
            click(1096,127) 
            
           
        #DEBUFF ATTACK
        if pyautogui.pixel (1452, 1271) [0] == 158:
            debuff("2")
                
        #ATTACK 1           
        if pyautogui.pixel (1351, 1267) [0] == 251:
            attack1("6")
            time.sleep(0.9)

        #ATTACK 2           
        if pyautogui.pixel (1351, 1267) [0] == 251:
            attack2("4")
            time.sleep(23)
Don't put this in your loop if it is only supposed to happen once.
        #TARGET ENEMY <<< THIS IS WHAT I NEED TO ONLY RUN ONE TIME>>>>
        if pyautogui.pixel (1096,127) [0] == 16:
            click(1096,127) 
These are getting called each time the loop executes.
        if pyautogui.pixel (1351, 1267) [0] == 251:
        if pyautogui.pixel (1452, 1271) [0] == 158
        if pyautogui.pixel (1351, 1267) [0] == 251:
If the associated code blocks don't execute it means the pixel[0] != 251 or 158.
(Apr-21-2023, 04:19 PM)deanhystad Wrote: [ -> ]Don't put this in your loop if it is only supposed to happen once.
        #TARGET ENEMY <<< THIS IS WHAT I NEED TO ONLY RUN ONE TIME>>>>
        if pyautogui.pixel (1096,127) [0] == 16:
            click(1096,127) 

Thanks for the reply. I thought I did that and it wasn't working. Can you tell me where this should be placed to be considered outside the loop? I had it to the far left of the code and not indented.
I got it. I stupidly had it still under the "while loop" line, I needed to bring it above that code.
Hey again,

You recommended that I place my code for the 1 x mouse click out of the loop however and that does work. However, I need that mouse to occur each time there is a new enemy spawn and with it being outside the loop, it doesn't run after the first time the program is initiated.

Is there a way to have the code for the mouse click in the loop but only have it click once per loop run?
Please show what you have tried to solve this problem.