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

标签 python python-3.x random bots pynput

信息。

嘿,:)
我现在学习 Python 几个星期了,刚刚开始一些小项目。现在我正在构建一个脚本来自动化网络浏览器游戏。脚本发出了一些“探险”,这给了我更多的游戏资源。该脚本已经可以运行,但我想改进它。如果您有任何建议,我很想听听。

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

例如单击这些位置之间的随机位置: 鼠标位置 (2000, 500) 鼠标位置(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))

关于python - 使用 pynput 在随机位置自动单击鼠标(对于网页浏览器游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61636521/

相关文章:

python - 如何找出 "WindowDeactivate"事件的焦点传递到哪个窗口?

python - Pandas json_normalize 无法在 Python 中处理大型 JSON 文件

python-3.x - 使用 Python 只读取大型 wav 文件的一部分

ruby - 如何使用 MongoMapper 以随机顺序获取文档?

c++ - 二维正态分布 C++

java - 在 1-49 范围内生成 5 组 6 个随机数,确保每组中没有重复项?

python - requests 无法从某些网站检索 html 内容

python - 完整更新 pip 包时出错

python - 我的 django View 总是抛出“意外的关键字参数错误”

python - 为什么一些在线 Python 编译器不允许我输入输入?