def mouse_routine(): mouse = Mouse(sleep=1) mouse.get_position() mouse.move(10, 10) mouse.double_click() mouse.set_position(200, 400) mouse.click()
def main(): sio = serial.Serial('COM10', baudrate=9600) kb = Keyboard(sio) m = Mouse(sio) while True: m.click() m.release() kb.press(KEY_RETURN) kb.release(KEY_RETURN) kb.press(KEY_RETURN) kb.release(KEY_RETURN) time.sleep(0.8)
class Window: def __init__(self, hwnd): self.hwnd = hwnd self.mouse = Mouse(hwnd) self.imp = ImageProcess(hwnd) def click(self, pos): self.mouse.click(pos) def find_img(self, img_name, accuracy=0.9): return self.imp.find_img(img_name, accuracy) def find_all_imgs(self, img_name, accuracy=0.9): return self.imp.find_all_imgs(img_name, accuracy)
class Location(object): def __init__(self, x, y, title="New Location"): self.title = title self.mouse = Mouse() self.keyboard = Keyboard() try: self.x = int(x) self.y = int(y) self._is_mouse_down = False logger.debug( 'New Location with name "{name}" created ({x}, {y})'.format( name=self.title, x=self.x, y=self.y)) except: raise FailExit('Incorect Location class constructor call:' '\n\tx = {x}\n\ty = {y}\n\ttitle= %{title}'.format( x=x, y=y, title=title)) def __str__(self): return 'Location ({x}, {y})'.format(x=self.x, y=self.y) @property def coordinates(self): return self.x, self.y def mouse_move(self, delay=0): self.mouse.move(self.x, self.y, delay) logger.debug('Mouse moved to ({x}, {y})'.format(x=self.x, y=self.y)) def offset(self, dx, dy): if isinstance(dx, int) and isinstance(dy, int): return Location(self.x + dx, self.y + dy) else: raise FailExit('Location.offset: incorrect offset values') def above(self, dy): if isinstance(dy, int) and dy >= 0: return Location(self.x, self.y - dy) else: raise FailExit('Location.above: incorrect value') def below(self, dy): if isinstance(dy, int) and dy >= 0: return Location(self.x, self.y + dy) else: raise FailExit('Location.below: incorrect value') def left(self, dx): if isinstance(dx, int) and dx >= 0: return Location(self.x - dx, self.y) else: raise FailExit('Location.left: incorrect value') def right(self, dx): if isinstance(dx, int) and dx >= 0: return Location(self.x + dx, self.y) else: raise FailExit('Location.right: incorrect value') def click(self, after_click_delay=0): self.mouse.click(self.x, self.y, after_click_delay) logger.debug('mouse left click in ({x}, {y})'.format(x=self.x, y=self.y)) def mouse_down(self): self.mouse.key_down(self.x, self.y) logger.debug('mouse down in ({x}, {y})'.format(x=self.x, y=self.y)) def mouse_up(self): self.mouse.key_up(self.x, self.y) logger.debug('mouse up in ({x}, {y})'.format(x=self.x, y=self.y)) def right_click(self, after_click_delay=0): self.mouse.right_click(self.x, self.y, after_click_delay) logger.debug('mouse right click in ({x}, {y})'.format(x=self.x, y=self.y)) def double_click(self, after_click_delay=0): self.mouse.double_click(self.x, self.y, after_click_delay) logger.debug('mouse double click in ({x}, {y})'.format(x=self.x, y=self.y)) def scroll(self, direction=1, count=1, click=True): # direction: # 1 - forward # -1 - backward for _ in range(0, int(count)): self.mouse.scroll(self.x, self.y, direction, click) logger.debug( 'scroll in ({x}, {y}) {count} times, {dir_} direction'.format( x=self.x, y=self.y, count=count, dir_='forward' if direction == 1 else 'backward')) def drag_to(self, *dest_location): delay = DRAGnDROP_MOVE_DELAY if len(dest_location) == 1 and isinstance(dest_location[0], Location): (dest_x, dest_y) = (dest_location[0].x, dest_location[0].y) elif len(dest_location) == 2: try: (dest_x, dest_y) = (int(dest_location[0]), int(dest_location[1])) except ValueError: raise FailExit('Location.drag_to: incorrect parameters') elif len(dest_location) == 3: try: (dest_x, dest_y) = (int(dest_location[0]), int(dest_location[1])) except ValueError: raise FailExit('Location.drag_to: incorrect parameters') delay = float(dest_location[2]) else: raise FailExit('') self.mouse.drag_to(self.x, self.y, dest_x, dest_y, delay) logger.debug('Mouse drag from (%i, %i) to (%i, %i)' % (self.x, self.y, dest_x, dest_y)) return self def drop(self): self.mouse.drop() logger.debug('Mouse drop') def dragndrop(self, *dest_location): self.drag_to(*dest_location) self.drop() return self def type(self, text, modifiers=None, click=True, click_type_delay=DELAY_BETWEEN_CLICK_AND_TYPE): log = 'Typed "{}"'.format(text) log += ' with modifiers "{}"'.format( modifiers) if modifiers is not None else '' if click: self.click(after_click_delay=click_type_delay) self.keyboard.type_text(str(text), modifiers) logger.info(log) def enter_text(self, text, modifiers=None, click=True, click_type_delay=DELAY_BETWEEN_CLICK_AND_TYPE): if click: self.click(after_click_delay=click_type_delay) self.keyboard.type_text('a', 'CTRL') time.sleep(click_type_delay) self.keyboard.type_text(str(text), modifiers)
class MiningBot: def __init__(self): self.mouse = Mouse() self.keyboard = Keyboard() self.screen = Screen() def start(self): while True: self.warp_to_system(self.get_system()) if not self.belts_exist(): continue self.warp_to_belt() while True: if not self.asteroids_exist('ore'): break if self.mine() == 'full': self.unload() break def warp_to_system(self, name): self.set_destination(name) self.toggle('autopilot') while self.is_in_autopilot(): time.sleep(10) def warp_to_belt(self): self.select_hotspot() self.select_image('warp_button') time.sleep(60) def mine(self): self.approach() self.lock() self.toggle('miners') while True: time.sleep(10) if not self.is_locked(): return 'depleted' if self.is_full(): return 'full' def unload(self): self.warp_to_system('home') time.sleep(10) for ore in ['veldspar', 'dense_veldspar', 'concentrated_veldspar']: rect = self.screen.locate(ore, 0.9) if (rect != None): self.mouse.move_to_rect(rect) x, y, w, h = rect x -= 400 rect = x, y, w, h self.mouse.drag_to_rect(rect, 'left') self.undock() def approach(self): self.select_hotspot() self.select_image('approach_button') time.sleep(60) def undock(self): self.select_image('undock_button') while self.is_in_station(): time.sleep(10) def lock(self): self.select_image('lock_button') time.sleep(10) def set_destination(self, name): rect = self.screen.locate(name + '_system', 0.9) if rect == None: raise ValueError('Invalid destination: ' + name) self.mouse.click_rect(rect, 'right') self.mouse.move(30, 42) self.mouse.click('left') def change_overview(self, tab): rect = self.screen.locate(tab + '_overview') self.mouse.click_rect(rect, 'left') def toggle(self, module): if module == 'autopilot': self.select_image('autopilot_off') elif module == 'miners': for index in [1, 2]: self.keyboard.press('f' + str(index)) else: raise ValueError('Invalid module: ' + module) def select_hotspot(self): self.mouse.click_point(1597, 195, 'left') time.sleep(1) def select_image(self, name): rect = self.screen.locate(name) self.mouse.click_rect(rect, 'left') time.sleep(1) self.mouse.move_to_point(1000, 500) time.sleep(1) def belts_exist(self): self.change_overview('belts') rect = self.screen.locate('asteroid_belt', 0.85) if rect == None: return False return True def asteroids_exist(self, type): self.change_overview('asteroids') if type not in ['ore', 'ice']: raise ValueError('Invalid asteroid type: ' + type) for size in ['large', 'medium', 'small']: rect = self.screen.locate(size + '_' + type + '_asteroid', 0.9) if rect != None: return True return False def get_system(self): return 'mine' def is_in_space(self): if self.screen.locate('autopilot_off', 0.9) == None: return False return True def is_in_station(self): return not self.is_in_space() def is_in_autopilot(self): if self.screen.locate('autopilot_on', 0.98) == None: return False return True def is_locked(self): if self.screen.locate('lock', 0.9) == None: return False return True def is_full(self): if self.screen.locate('cargo_full', 0.98) == None: return False return True