Search code examples
pythonloopspyautogui

Trying to make an online slots click bot


I'm trying to code a bot in Python that will play a demo slot game for me. I want the bot to click a button when it appears on screen, a certain number of times. So for example, at the start of the game, the bot will click the spin button. The reels will spin and the spin button will disappear. When the reels are done spinning, the spin button will reappear, and the bot will click the spin button again.

I got the code to work, and the button is clicked infinitely by using a while True: loop (in place of the for i in range(5):). But now I'm trying to only get it to run a certain number of times, and it doesn't work. It only clicks the button once, then the program is over.

import pyautogui

def main():
    findButton()
    clickButton()


def findButton():
    global buttonPoint 
    buttonLocation = pyautogui.locateOnScreen('kronosunleashed.png', confidence=0.40) #Locates button on screen
    buttonPoint = pyautogui.center(buttonLocation) #Finds center of button


def clickButton():
    for i in range(5):
        buttonX, buttonY = buttonPoint
        pyautogui.click(buttonX, buttonY) #Clicks center of button

Solution

  • Perhaps it goes too fast for the program to notice. Try adding a sleep in the loop

    import time
    
    def clickButton():
        for i in range(5):
            buttonX, buttonY = buttonPoint
            pyautogui.click(buttonX, buttonY) #Clicks center of button
            time.sleep(1) # waits a second