Waypoints follower从类实例中获取空的waypoint集合

问题描述 投票:0回答:1

我正在写简单的waypoits追随者,我遇到了一个问题。一切顺利(这意味着添加新的航点并显示它们的工作),直到我想“开始跟随”。如果我想这样做,那么航点列表的行为就好像它是空的(“没有航路点”),但如果我想显示它们,一切看起来都很好。我不知道该怎么做。

import pyautogui
from pynput import mouse, keyboard
import os


class Mouse_Part:
    def __init__(self):
        self.waypoints = []

    def on_click(self, x, y, button, pressed):
        if button == button.left and (x, y) not in self.waypoints:
            self.waypoints.append((x, y))
            print('{}, {} has been added'.format(x, y))

    def show_waypoints(self):
        print(self.waypoints)


class Keyboard_Part:
    def __init__(self):
        self.mouse_part = Mouse_Part()
        self.mouse_listener = 
        mouse.Listener(on_click=self.mouse_part.on_click)
        self.bot = Bot()

    def on_press(self, key):
        if key == keyboard.KeyCode.from_char('a'):
            os.system('clear')
            menu_print()
            self.mouse_listener.start()
        if key == keyboard.KeyCode.from_char('s'):
            os.system('clear')
            menu_print()
            self.mouse_listener.stop()
        if key == keyboard.KeyCode.from_char('d'):
            os.system('clear')
            menu_print()
            self.mouse_part.show_waypoints()
        if key == keyboard.KeyCode.from_char('f'):
            os.system('clear')
            menu_print()
            self.bot.start()


class Bot:
    def __init__(self):
        self.mouse_part = None

    def start(self):
        self.mouse_part = Mouse_Part()
        if len(self.mouse_part.waypoints) > 0:
            for x in self.mouse_part.waypoints:
                pyautogui.moveTo(x, duration=0.25)
        else:
            print('there are no waypoints')

def menu_print():
    print('1.To add new waypoints click \'a\' then click at desired 
    position on the screen\n2.To stop adding new waypoints click 
    \'s\'\n3.To print waypoints click \'d\'\n4.To start follow 
    waypoints 
    click\'f\'\n')


if __name__ == '__main__':

    menu_print()

    keyboard_part = Keyboard_Part()

    with keyboard.Listener(on_press=keyboard_part.on_press) as 
    listener:
        listener.join()
python python-3.x pyautogui pynput
1个回答
1
投票

您的航点是实例变量 - 它们不属于类本身,而属于实例。在Bot.start(..),你正在为你的机器人Mouse_Part()变量分配一个新的self.mouse_part实例和空路点。

固定:

 Bot:
    def __init__(self):
        self.mouse_part = None

    def start(self, mouseParts):
        # this creates a new Mouse_Part() instance that is empty
        #     self.mouse_part = Mouse_Part() - replace with given one
        self.mouse_part = mouseParts

        if len(self.mouse_part.waypoints) > 0:
            for x in self.mouse_part.waypoints:
                pyautogui.moveTo(x, duration=0.25)
        else:
            print('there are no waypoints')

keyboard_part = Keyboard_Part()

with keyboard.Listener(on_press=keyboard_part.on_press) as 
listener:
    listener.join()

完成记录航点时:

b = Bot()

# provide the instance of Keyboard_part.mouse_part that was filled
# before to your bot:
b.start(keyboard_part.mouse_part)
© www.soinside.com 2019 - 2024. All rights reserved.