Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Non repetitive mouse click
#1
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)
Reply
#2
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.
Sartre likes this post
Reply
#3
(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.
Reply
#4
I got it. I stupidly had it still under the "while loop" line, I needed to bring it above that code.
Reply
#5
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?
Reply
#6
Please show what you have tried to solve this problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to mouse click a specific item in pygame? Frankduc 5 1,735 May-03-2022, 06:22 PM
Last Post: Frankduc
  How to script repetitive tasks in dynaform using python BenneGrus 0 1,348 Dec-22-2021, 08:36 AM
Last Post: BenneGrus
  Move mouse and click in particular position biprabu 3 2,501 Sep-01-2020, 08:23 PM
Last Post: deanhystad
  Slide show with mouse click pausing aantono 1 2,218 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 2,540 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Why is left mouse click not working? yeto 3 6,214 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  How to get mouse coordinates on click and release OhNoSegFaultAgain 1 3,005 May-17-2019, 06:56 PM
Last Post: buran
  Python catch mouse click in pure text, no graphics samtal 7 7,282 Sep-10-2018, 03:02 PM
Last Post: samtal
  Mouse click movements? [UPDATED] minnaadel 3 3,565 Mar-31-2018, 05:15 PM
Last Post: woooee
  How to trigger a function by clicking the left mouse click? HelloBoyz 1 9,423 Jun-29-2017, 09:43 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020