Reputation: 23
Info.
Hey there :)
I'm learning Python for a few weeks now, and I just started with some small projects. Now I'm building a script to automate an webbrowser game. The script sends out a few "expeditions" which give me more rescources inside the game. The script is already working but i would like to improve it. If you have any tips i would love to hear them.
Question.
I'm using pynput and mouse.position = () for an exact location to click. Is there a way to make the click random inside a certain area? Because normal person woudn't always click in the same location.
like click at a random location between these positions: mouse.position (2000, 500) mouse.position (3000, 1000)
My script.
import pynput, time, random, sys
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
timeDelay = random.randrange(2, 4)
def locateogame():
#---------------------------------------------> Getting to Ogame.nl
mouse = MouseController()
keyboard = KeyboardController()
mouse.position = (2392, 48)
mouse.click(Button.left, 1)
time.sleep(timeDelay)
keyboard.type("Ogame.nl")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
#login to account and universe.
time.sleep(timeDelay)
mouse.position = (2343, 564)
mouse.click(Button.left, 1)
def p1():
#---------------------------------------------> Locate to planet 1
mouse = MouseController()
time.sleep(timeDelay)
mouse.position = (3054, 298)
mouse.click(Button.left, 1)
def p2():
#---------------------------------------------> Locate to planet 2
#Locate to 5:352:8
mouse = MouseController()
time.sleep(timeDelay)
mouse.position = (3060, 382)
mouse.click(Button.left, 1)
def p3():
#---------------------------------------------> Locate to planet 3
#Locate to 5:353:7
mouse = MouseController()
time.sleep(timeDelay)
mouse.position = (3074, 438)
mouse.click(Button.left, 1)
def p4():
#---------------------------------------------> Locate to planet 4
#Locate to 5:353:8
mouse = MouseController()
time.sleep(timeDelay)
mouse.position = (3073, 481)
mouse.click(Button.left, 1)
def p5():
#---------------------------------------------> Locate to planet 5
#Locate to 4:32:8
mouse = MouseController()
time.sleep(timeDelay)
mouse.position = (3099, 538)
mouse.click(Button.left, 1)
def Sending():
#---------------------------------------------> This will do all the clicking to send my ships
mouse = MouseController()
keyboard = KeyboardController()
#Select Fleet from menu
time.sleep(timeDelay)
mouse.position = (2254, 493)
mouse.click(Button.left, 1)
#select "Expeditie" Fleet
time.sleep(timeDelay)
mouse.position = (2670, 694)
mouse.click(Button.left, 1)
#Expedition
time.sleep(timeDelay)
mouse.position = (2598, 743)
mouse.click(Button.left, 1)
time.sleep(timeDelay)
mouse.position = (2954, 678)
mouse.click(Button.left, 1)
#select slot 16
time.sleep(timeDelay)
mouse.position = (2796, 434)
mouse.click(Button.left, 1)
keyboard.type("16")
keyboard.press(Key.enter)
keyboard.release(Key.enter)
#expeditie button
time.sleep(timeDelay)
mouse.position = (2408, 378)
mouse.click(Button.left, 1)
#send Fleet
time.sleep(timeDelay)
mouse.position = (2862, 711)
mouse.click(Button.left, 1)
##---------------------------------------------> Start of the script.
locateogame()
fns = [p1, p2, p3, p4, p5]
from random import choice
choice(fns)()
Sending()
Thanks for your time and have a nice day!
Upvotes: 2
Views: 1066
Reputation: 117981
You can use random.randint
to sample a value from a range. Just do this twice, once for your X value and again for Y
>>> import random
>>> random.randint(2000, 3000)
2786
>>> random.randint(500, 1000)
838
So in your code you could do
from random import randint
mouse.position = (randint(2000, 3000), randint(500, 1000))
Upvotes: 3