def main(): parser = argparse.ArgumentParser() parser.add_argument("--port", dest="port", help="The port to publish to.", default=5556, type=int) arguments = parser.parse_args() port = arguments.port context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind("tcp://127.0.0.1:%i" % port) messengers = [] descriptor = tanks.TankDescriptor(0) #robot = tanks.Tank(descriptor) robot_event_handler = communications.TankEventHandler(descriptor) messengers.append(robot_event_handler) messengers.append(Quitter()) pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption('Message shooter') pygame.mouse.set_visible(False) clock = pygame.time.Clock() previous_str = "" while (True): events = pygame.event.get() for messenger in messengers: messenger.handle_events(events) for message in messenger.get_messages(): new_str = str(message) if (previous_str != new_str): print "send message:", new_str previous_str = new_str socket.send(message) #clock.tick(1 / 0.05) clock.tick(40)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--port", dest="port", help="The port to subscribe to.", default=5556, type=int) parser.add_argument("--address", dest="address", help="The address used to bind.", default="127.0.0.1", type=str) arguments = parser.parse_args() address = arguments.address port = arguments.port context = zmq.Context() socket = context.socket(zmq.SUB) socket.setsockopt(zmq.LINGER, 0) socket.setsockopt(zmq.SUBSCRIBE, "") url = "tcp://%s:%i" % (address, port) print url socket.connect(url) descriptor = tanks.TankDescriptor(0) broadcaster = communications.CombinedDispatcher(socket) test_world = world.World(broadcaster, draw_helpers=True) robot = tanks.Tank(descriptor) robot_event_handler = communications.TankEventHandler(descriptor) test_world.add(robot) broadcaster.register_event_handler(Quitter()) broadcaster.register_event_handler(robot_event_handler) test_world.camera_giver = robot broadcaster.register_listener(robot) broadcaster.register_listener(test_world) test_world.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument("--port", dest="port", help="The port to read from.", default=5556, type=int) arguments = parser.parse_args() port = arguments.port context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect("tcp://127.0.0.1:%i" % port) descriptor = tanks.TankDescriptor(0) socket.setsockopt(zmq.SUBSCRIBE, descriptor.recipient) broadcaster = communications.CombinedDispatcher(socket) test_world = world.World(broadcaster, draw_helpers=True) robot = tanks.Tank(descriptor) robot_event_handler = communications.TankEventHandler(descriptor) test_world.add(robot) broadcaster.register_event_handler(Quitter()) broadcaster.register_event_handler(robot_event_handler) test_world.camera_giver = robot broadcaster.register_listener(robot) broadcaster.register_listener(test_world) test_world.run() if (False): messengers = [] messengers.append(Quitter()) pygame.init() #screen = pygame.display.set_mode((640, 480)) screen = pygame.display.set_mode((320, 240)) pygame.display.set_caption('Message receiver') pygame.mouse.set_visible(False) clock = pygame.time.Clock() previous_str = "" has_received = False is_receiving = False while ((not has_received) or (has_received) and (is_receiving)): events = pygame.event.get() for messenger in messengers: messenger.handle_events(events) try: new_str = socket.recv(flags=zmq.DONTWAIT) has_received = True is_receiving = True if (previous_str != new_str): print "receive message:", new_str previous_str = new_str except Exception as e: print e is_receiving = False clock.tick(1 / 0.05)
def main(): broadcaster = communications.LocalEventDispatcher() test_world = world.World(broadcaster, draw_helpers=True) descriptor = tanks.TankDescriptor(0) robot = tanks.Tank(descriptor) robot_event_handler = communications.TankEventHandler(descriptor) test_world.add(robot) broadcaster.register_event_handler(robot_event_handler) test_world.camera_giver = robot broadcaster.register_listener(robot) broadcaster.register_listener(test_world) test_world.run()