【问题标题】:Trying to using python for controlling games尝试使用python控制游戏
【发布时间】:2019-12-29 06:01:51
【问题描述】:

我正在尝试使用带有 win32api 的 Python 来控制游戏(Maplestory)。我的脚本可以在很多软件(如记事本)上完美运行,但游戏除外。

我想也许游戏直接与硬件通信,所以我无法使用 win32api 来控制游戏。但我不知道如何使用 Python 脚本与硬件进行通信。而且我也不知道谁写了一个可以与键盘硬件通信的轮子。

这是我的脚本:

import time,random,win32con,win32api

WAIT_TIME = 5

key_map = {
    "0": 49, "1": 50, "2": 51, "3": 52, "4": 53, "5": 54, "6": 55, "7": 56, "8": 57, "9": 58,
    "A": 65, "B": 66, "C": 67, "D": 68, "E": 69, "F": 70, "G": 71, "H": 72, "I": 73, "J": 74,
    "K": 75, "L": 76, "M": 77, "N": 78, "O": 79, "P": 80, "Q": 81, "R": 82, "S": 83, "T": 84,
    "U": 85, "V": 86, "W": 87, "X": 88, "Y": 89, "Z": 90
}

class Press:
    def __init__(self):
        print("The program will start in %d seconds" % WAIT_TIME)
        time.sleep(WAIT_TIME)
        self.time = time.time()
        pass

    def key_down(self, key):
        key = key.upper()
        vk_code = key_map[key]
        win32api.keybd_event(vk_code,win32api.MapVirtualKey(vk_code,0),0,0)


    def key_up(self, key):
        key = key.upper()
        vk_code = key_map[key]
        win32api.keybd_event(vk_code, win32api.MapVirtualKey(vk_code, 0), win32con.KEYEVENTF_KEYUP, 0)


    def key_press(self, key):
        self.key_down(key)
        time.sleep(0.02)
        self.key_up(key)
        print(key)



class Illium(Press):
    def __init__(self):
        super().__init__()
        self.spear = "A"
        self.bullet = "Q"
        self.buff1 = "3"
        self.buff2 = "4"

    def add_buff(self):
        if time.time() - self.time > 60:
            self.key_press(self.buff1)
            time.sleep(0.5)
            self.key_press(self.buff2)
            time.sleep(random.uniform(0.5,1))
            self.key_press("1")   # 
            self.time = time.time()

    def auto_attack(self):
        self.add_buff()
        time.sleep(random.uniform(0.5,1))  
        self.key_press(self.spear)
        time.sleep(random.uniform(1,1.5))
        self.key_press(self.bullet)
        time.sleep(random.uniform(0.5,1))




illium = Illium()
while True:
    illium.auto_attack()
    time.sleep(random.uniform(0.5,1))


print("ENd")

【问题讨论】:

    标签: python python-3.x keypress simulate


    【解决方案1】:

    有些游戏会采取一些措施来阻止脚本运行(防止作弊)。 也许使用python脚本不是一个好主意。

    我不玩这个游戏。如果你真的想用python,也许你可以尝试其他模块来控制游戏(如pynput,pykeyboard等)。

    【讨论】:

    • 你认为切换到pykeyboard等其他库可以解决这些问题吗?
    • @Qfrost 试试看。不过我建议你用其他工具来控制游戏。你可以用autohotkey。我以前可以用autohotkey来控制守望先锋。不知道能不能用在你玩的游戏中。
    • 好的,我今晚试试,谢谢。但是我还是想用python找到解决办法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2018-07-02
    • 2022-06-23
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多