使用 pynput 在随机位置自动单击鼠标(用于网络浏览器游戏)

Automatically Click Mouse at Random location using pynput (for a web browser game)

信息。

你好:)
我正在学习 Python 几个星期了,我刚开始做一些小项目。现在我正在构建一个脚本来自动化网络浏览器游戏。脚本发出了一些"expeditions",这让我在游戏中有更多的资源。该脚本已经在运行,但我想对其进行改进。如果您有任何建议,我很乐意听取。

问题。
我正在使用 pynput 和 mouse.position = () 来获取要点击的确切位置。有没有办法让点击在某个区域内随机? 因为正常人不会总是在同一个位置点击。

喜欢点击这些位置之间的随机位置: mouse.position (2000, 500) mouse.position (3000, 1000)

我的脚本。

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()

感谢您的宝贵时间,祝您有愉快的一天!

您可以使用 random.randint 从一个范围中抽取一个值。只需执行两次,一次用于您的 X 值,一次用于 Y

>>> import random
>>> random.randint(2000, 3000)
2786
>>> random.randint(500, 1000)
838

所以在你的代码中你可以这样做

from random import randint
mouse.position = (randint(2000, 3000), randint(500, 1000))