Trying to make an online slots click bot

Question:

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
Asked By: Sarah N

||

Answers:

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
Answered By: Emanuel P
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.