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

2024-05-19 21:14:30 发布

您现在位置:Python中文网/ 问答频道 /正文

信息。

你好:)
我现在正在学习Python几个星期,我刚刚开始了一些小项目。现在,我正在构建一个脚本来自动化webbrowser游戏。该脚本发出了一些“探险”,让我在游戏中有更多的资源。脚本已经在工作,但我想改进它。如果你有什么建议,我很想听听

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

类似于在这些位置之间的随机位置单击: 鼠标位置(2000500) 鼠标位置(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()

谢谢你抽出时间,祝你今天愉快


Tags: tokeytimedefpositionbuttonsleepleft
1条回答
网友
1楼 · 发布于 2024-05-19 21:14:30

您可以使用^{}从一个范围中对一个值进行采样。只需执行两次,一次用于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))

相关问题 更多 >