def load_roomlayout(): md = MapDS() # empty landingpad with open(url + "room_layout.dat", "rb") as f: byte = f.read(1) data = [] #count = 0 while byte: data.append(struct.unpack('B', byte)[0]) if len(data) == 4: md.add_room(data[:2], data[2:]) data = [] byte = f.read(1) return md
def load_roomlayout(): md = MapDS() # empty landingpad with open(url + "room_layout.dat", "rb") as f: byte = f.read(1) data = [] # count = 0 while byte: data.append(struct.unpack("B", byte)[0]) if len(data) == 4: md.add_room(data[:2], data[2:]) data = [] byte = f.read(1) return md
def __init__(self): threading.Thread.__init__(self) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(ADDR) self.done = False self.slots = [UserSlot() for _ in xrange(MAX_PLAYERS)] self.input_handlers = {"MSG": self.cmd_msg} print "Generating map...", self.map = MapDS() self.map.expand_room() print " done."
def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (600, 450) # self.ui.screen_size() pygame.display.set_caption("Mapper") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.map = MapDS() self.map.expand_room() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.last = None self.clicked = 0 self.client = Client(self) self.ui = DungeonUI(self, None)
class Main: def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (600, 450) # self.ui.screen_size() pygame.display.set_caption("Mapper") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.map = MapDS() self.map.expand_room() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.last = None self.clicked = 0 self.client = Client(self) self.ui = DungeonUI(self, None) def ui_push(self, cls): self.ui = cls(self, self.ui) def ui_pop(self): self.ui = self.ui.parent def ui_swap(self, cls): self.ui = cls(self, self.ui.parent) def stop(self): self.done = True def run(self): while not self.done: self.clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: self.stop() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q and pygame.key.get_mods( ) & pygame.KMOD_CTRL: self.stop() self.keys.add(event.key) self.ui.handle_key(event) elif event.type == pygame.KEYUP: self.keys.discard(event.key) self.ui.handle_key_up(event) elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.clicked = True self.ui.handle_click(event) elif event.button in (4, 5): # scrollin! event.scroll_dir = event.button * 2 - 9 self.ui.handle_scroll(event) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: self.clicked = False self.ui.handle_click_up(event) elif event.type == pygame.MOUSEMOTION: if event.buttons[0]: self.ui.handle_drag(event) else: self.ui.handle_motion(event) self.screen.fill((0, ) * 3) self.ui.update() self.ui.reblit(self.screen, self.clock.get_time()) pygame.display.flip() pygame.quit()
def load_room((rx, ry)): room_size = md.get_at((rx, ry)) (x, y), (w, h) = room_size.get_rect() with open(room_url(x, y), "rb") as f: byte = f.read(1) room_type = ALL_ROOM_TYPES[byte] map_data = [] for row in xrange(h): map_data.append([]) for col in xrange(w): byte1 = struct.unpack('B', f.read(1))[0] byte2 = struct.unpack('B', f.read(1))[0] map_data[-1].append((byte1, byte2)) return room_type((map_data, set())) if __name__ == "__main__": print "'l' to test loading a room\n's' to test saving a room" do = raw_input(">") if do == "s": md = MapDS() md.expand_room() save_roomlayout(md) elif do == "l": md = load_roomlayout() else: exit() main = Main() main.map = md main.run()
def load_room((rx, ry)): room_size = md.get_at((rx, ry)) (x, y), (w, h) = room_size.get_rect() with open(room_url(x, y), "rb") as f: byte = f.read(1) room_type = ALL_ROOM_TYPES[byte] map_data = [] for row in xrange(h): map_data.append([]) for col in xrange(w): byte1 = struct.unpack("B", f.read(1))[0] byte2 = struct.unpack("B", f.read(1))[0] map_data[-1].append((byte1, byte2)) return room_type((map_data, set())) if __name__ == "__main__": print "'l' to test loading a room\n's' to test saving a room" do = raw_input(">") if do == "s": md = MapDS() md.expand_room() save_roomlayout(md) elif do == "l": md = load_roomlayout() else: exit() main = Main() main.map = md main.run()
class Server(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(ADDR) self.done = False self.slots = [UserSlot() for _ in xrange(MAX_PLAYERS)] self.input_handlers = {"MSG": self.cmd_msg} print "Generating map...", self.map = MapDS() self.map.expand_room() print " done." def cmd_restart(self): self.broadcast("RESTART") def run(self): self.sock.listen(MAX_PLAYERS) while not self.done: inputs = [x.conn for x in self.slots if not x.is_open()] + [self.sock] # blocks until someone connects or a client sends a message ready, _, _ = select.select(inputs, [], [], 0.5) # filters ready connections for c in ready: if c == self.sock: # new player conn, _ = c.accept() ind = self.get_next_slot() if ind is None: # out of slots, have to reject print "Rejected a connection" continue self.slots[ind].set_connection(conn) print "Connected" else: # returning player's command sender = self.get_sender(c) message = c.recv(MAX_PACKET_LENGTH) # am I stupid or is this just as good if not message: # close connection c.close() self.slots[sender].set_open() else: # a real command sender = self.get_sender(c) self.slots[sender].buffer += message messages = self.slots[sender].parse_buffer() for msg in messages: self.handle_input(sender, msg) def handle_input(self, slot, message): msg = message.split(" ") if msg[0] not in self.input_handlers: print "Unknown or malformed command:", msg[0] return self.input_handlers[msg[0]](slot, msg) def cmd_msg(self, slot, argv): if argv[1][:1] == "/": command = argv[1][1:] self.broadcast(*argv[1:]) def broadcast(self, *msg): for x in self.slots: if not x.is_open(): x.send(*msg) def process_received(self, msg): pass def stop(self): self.done = True def get_sender(self, c): for i, x in enumerate(self.slots): if not x.is_open(): if x.conn == c: return i print "Server.get_sender: I couldn't find the guy you're looking for, this is really bad." def get_next_slot(self): for i, x in enumerate(self.slots): if x.is_open(): return i return None
class Main: def __init__(self): pygame.init() d = pygame.display.Info() self.desktop_size = (d.current_w, d.current_h) self.size = (600, 450) # self.ui.screen_size() pygame.display.set_caption("Mapper") self.done = False self.clock = pygame.time.Clock() self.keys = set() self.map = MapDS() self.map.expand_room() self.screen = pygame.display.set_mode(self.size) self.screen.fill((0xFF, ) * 3) self.last = None self.clicked = 0 self.client = Client(self) self.ui = DungeonUI(self, None) def ui_push(self, cls): self.ui = cls(self, self.ui) def ui_pop(self): self.ui = self.ui.parent def ui_swap(self, cls): self.ui = cls(self, self.ui.parent) def stop(self): self.done = True def run(self): while not self.done: self.clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: self.stop() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q and pygame.key.get_mods() & pygame.KMOD_CTRL: self.stop() self.keys.add(event.key) self.ui.handle_key(event) elif event.type == pygame.KEYUP: self.keys.discard(event.key) self.ui.handle_key_up(event) elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.clicked = True self.ui.handle_click(event) elif event.button in (4, 5): # scrollin! event.scroll_dir = event.button * 2 - 9 self.ui.handle_scroll(event) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: self.clicked = False self.ui.handle_click_up(event) elif event.type == pygame.MOUSEMOTION: if event.buttons[0]: self.ui.handle_drag(event) else: self.ui.handle_motion(event) self.screen.fill((0, ) * 3) self.ui.update() self.ui.reblit(self.screen, self.clock.get_time()) pygame.display.flip() pygame.quit()