One of my initial python courses automates a simple cookie clicking game by using pyautogui.click at specific cordinates. I am trying to take this further by using the locateonscreen image functions and the random module to locate images and then randomly click within the images as I think this is more practical for my learning and more human-like.
When the images are found - everything works. When the images are not found - I get an AttributeError: 'NoneType' object has no attribute 'left' because my box does not exist in that case. Im looking for help programming the logic to try to find and imagine and if it finds it randomly click it, otherwise try to find the next image.
Here is what I have working when images exist: The while coordinates are to click a static location, then after the counter reaches a certain point look for and randomly click the images. Then return to the static location to continue clicking and loop.
from ast import While
from tkinter import TRUE
import pyautogui as gui
import random
gui.PAUSE = 0.01
gui.moveTo(x=383,y=576)
counter = 1
while gui.position() == (383,576):
gui.click()
counter += 1
if counter % 300 == 0:
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Bank = gui.locateOnScreen('Bank.png')
gui.moveTo(randomClick(Bank))
gui.click()
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Factory = gui.locateOnScreen('Factory.png')
gui.moveTo(randomClick(Factory))
gui.click()
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Mine = gui.locateOnScreen('Mine.png')
gui.moveTo(randomClick(Mine))
gui.click()
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Farm = gui.locateOnScreen('Farm.png')
gui.moveTo(randomClick(Farm))
gui.click()
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Grandma = gui.locateOnScreen('Grandma.png')
gui.moveTo(randomClick(Grandma))
gui.click()
def randomClick(box):
x_click = int(random.uniform(box.left, box.left+box.width))
y_click = int(random.uniform(box.top, box.top+box.height))
return (x_click, y_click)
Cursor = gui.locateOnScreen('Cursor.png')
gui.moveTo(randomClick(Cursor))
gui.click()
gui.moveTo(x=383,y=576)
Aucun commentaire:
Enregistrer un commentaire