def handle_responses(): """Read in server responses from the network and process.""" res = Network.get_response() while res: # Load level map if 'map' in res: CS.map = ClientMap.unserialize(res['map']) new_dlevel = True else: new_dlevel = False # Update log messages if 'log' in res: for msg in res['log']: CS.msgs.append(msg) CS.log_updated = True # Update monsters if 'm' in res: for oid, m_str in res['m'].iteritems(): if oid in ClientObject.obj_dict: ClientObject.obj_dict[oid].update_from_string(m_str) else: mon = ClientMonster.unserialize(m_str) mon.place_on_map() # Delete monsters if 'm_del' in res: for oid, flags in res['m_del']: ClientObject.obj_dict[oid].delete(flags) # Update items if 'i' in res: for oid, i_str in res['i'].iteritems(): if oid in ClientObject.obj_dict: ClientObject.obj_dict[oid].update_from_string(i_str) else: item = ClientItem.unserialize(i_str) item.place_on_map() # Delete items if 'i_del' in res: for oid, flags in res['i_del']: ClientObject.obj_dict[oid].delete(flags) # Update the player object if 'u' in res: if CS.u is not None: CS.u.update_from_string(res['u']) else: CS.u = ClientPlayer.unserialize(res['u']) # If we're changing dungeon levels, the player needs a new FOV map if new_dlevel: CS.u.set_fov_map(CS.map.grid) CS.u.fov_map.do_fov(CS.u.x, CS.u.y, CS.u.fov_radius) gui.center_map() res = Network.get_response()
def client_init(): """Initialiaze client state.""" # Set up signal handlers signal.signal(signal.SIGINT, quit_game) signal.signal(signal.SIGTERM, quit_game) # Initializing these modules separately instead of calling # pygame.init() is WAY faster. pygame.display.init() pygame.font.init() # Wait 200 ms before repeating a key that's held down, and send them # as fast as possible. The repeat delay is therefore limited by the # frame rate, not by set_repeat() pygame.key.set_repeat(200, 1) pygame.display.set_caption('{0} {1}'.format(cfg.GAME_NAME, cfg.VERSION)) uname = 'Taimor' usex = 'Male' urace = 'Human' urole = 'Wizard' pygame.display.set_caption('{0} - {1} the {2} {3} {4}'.format( cfg.GAME_TITLE, uname, usex, urace, urole)) CS.clock = pygame.time.Clock() CS.font = pygame.font.SysFont('Arial', cfg.FONT_SIZE) CS.font_h = CS.font.get_height() CS.font_w = CS.font.size('X')[0] # Size of the map surface CS.map_rect.w = (cfg.MAP_W + 2) * cfg.TILE_W CS.map_rect.h = (cfg.MAP_H + 2) * cfg.TILE_H # Size of the status panel CS.status_rect.w = cfg.STATUS_W * CS.font_w CS.status_rect.h = cfg.STATUS_H * CS.font_h # Size of the equipment panel CS.eq_rect.w = cfg.EQ_W * cfg.TILE_W CS.eq_rect.h = CS.status_rect.h # Size of the full game screen CS.screen_rect.w = CS.eq_rect.w + CS.status_rect.w + cfg.MIN_LOG_W if CS.screen_rect.w < cfg.INIT_SCREEN_W: CS.screen_rect.w = cfg.INIT_SCREEN_W CS.screen_rect.h = CS.status_rect.h + cfg.MIN_MAPVIEW_H if CS.screen_rect.h < cfg.INIT_SCREEN_H: CS.screen_rect.h = cfg.INIT_SCREEN_H # Size of the log surface CS.log_rect.w = CS.screen_rect.w - ( CS.eq_rect.w + CS.status_rect.w) - cfg.SCROLLBAR_W - 50 CS.log_rect.h = CS.status_rect.h CS.logview_rect.w, CS.logview_rect.h = CS.log_rect.w, CS.log_rect.h # The mapview size. May be smaller or larger than the actual map size. # This is the location on the screen where the map or a piece thereof # is drawn. It's not an actual surface, but a logical rectangle. CS.mapview_rect.w = CS.screen_rect.w - cfg.SCROLLBAR_W CS.mapview_rect.h = ( CS.screen_rect.h - CS.status_rect.h - cfg.SCROLLBAR_W) # Locations to blit equipment on the equipment panel eq_cent = (int(CS.eq_rect.w / 2.0 - cfg.TILE_W / 2), int(CS.eq_rect.h / 2.0 - cfg.TILE_W / 2)) CS.eq_hands = (eq_cent[0], CS.eq_rect.y + cfg.TILE_H / 2 + 3 * cfg.TILE_H) CS.eq_rweap = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1]) CS.eq_lweap = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1]) CS.eq_rring = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] + cfg.TILE_H) CS.eq_lring = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1] + cfg.TILE_H) CS.eq_boots = (CS.eq_hands[0], CS.eq_hands[1] + cfg.TILE_H * 2) CS.eq_armor = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H) CS.eq_shirt = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H) CS.eq_cloak = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H) CS.eq_neck = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H * 2) CS.eq_eyes = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H * 2) CS.eq_quiver = (CS.eq_hands[0] + cfg.TILE_W * 2, CS.eq_hands[1] - cfg.TILE_H * 3) CS.eq_light = (CS.eq_hands[0] - cfg.TILE_W * 2, CS.eq_hands[1] - cfg.TILE_H * 3) CS.eq_head = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H * 3) CS.screen = pygame.display.set_mode( (CS.screen_rect.w, CS.screen_rect.h), pygame.RESIZABLE) # pylint: disable=E1121 CS.eq_surf = Surface((CS.eq_rect.w, CS.eq_rect.h)).convert() CS.status_surf = Surface((CS.status_rect.w, CS.status_rect.h)).convert() CS.map_surf = Surface((CS.map_rect.w, CS.map_rect.h)).convert() # pylint: enable=E1121 # Set the system icon system_icon = image.load_image('icon.xpm') pygame.display.set_icon(system_icon) CS.tiles_img = image.load_image('tiles16.xpm') CS.gray_tiles_img = image.load_image('tiles16_gray.xpm') CS.menu_bg_img = image.load_image('parchment.jpg') CS.tile_dict = image.create_tile_dict() CS.blank_tile = CS.tile_dict['cmap, wall, dark'] CS.x_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 0, CS.map_rect, CS.mapview_rect, always_show=False) CS.y_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 1, CS.map_rect, CS.mapview_rect, always_show=False) CS.log_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 1, CS.log_rect, CS.logview_rect, always_show=False) # Process any initial setup data we've gotten from the server. This # should include the first dungeon level and the Player object. handle_responses() # Set up keystroke actions keys.attach_key_actions() # Make sure everything is aligned correctly gui.center_map() gui.handle_resize(CS.screen_rect.w, CS.screen_rect.h)
def client_init(): """Initialiaze client state.""" # Set up signal handlers signal.signal(signal.SIGINT, quit_game) signal.signal(signal.SIGTERM, quit_game) # Initializing these modules separately instead of calling # pygame.init() is WAY faster. pygame.display.init() pygame.font.init() # Wait 200 ms before repeating a key that's held down, and send them # as fast as possible. The repeat delay is therefore limited by the # frame rate, not by set_repeat() pygame.key.set_repeat(200, 1) pygame.display.set_caption('{0} {1}'.format(cfg.GAME_NAME, cfg.VERSION)) uname = 'Taimor' usex = 'Male' urace = 'Human' urole = 'Wizard' pygame.display.set_caption('{0} - {1} the {2} {3} {4}'.format( cfg.GAME_TITLE, uname, usex, urace, urole)) CS.clock = pygame.time.Clock() CS.font = pygame.font.SysFont('Arial', cfg.FONT_SIZE) CS.font_h = CS.font.get_height() CS.font_w = CS.font.size('X')[0] # Size of the map surface CS.map_rect.w = (cfg.MAP_W + 2) * cfg.TILE_W CS.map_rect.h = (cfg.MAP_H + 2) * cfg.TILE_H # Size of the status panel CS.status_rect.w = cfg.STATUS_W * CS.font_w CS.status_rect.h = cfg.STATUS_H * CS.font_h # Size of the equipment panel CS.eq_rect.w = cfg.EQ_W * cfg.TILE_W CS.eq_rect.h = CS.status_rect.h # Size of the full game screen CS.screen_rect.w = CS.eq_rect.w + CS.status_rect.w + cfg.MIN_LOG_W if CS.screen_rect.w < cfg.INIT_SCREEN_W: CS.screen_rect.w = cfg.INIT_SCREEN_W CS.screen_rect.h = CS.status_rect.h + cfg.MIN_MAPVIEW_H if CS.screen_rect.h < cfg.INIT_SCREEN_H: CS.screen_rect.h = cfg.INIT_SCREEN_H # Size of the log surface CS.log_rect.w = CS.screen_rect.w - ( CS.eq_rect.w + CS.status_rect.w) - cfg.SCROLLBAR_W - 50 CS.log_rect.h = CS.status_rect.h CS.logview_rect.w, CS.logview_rect.h = CS.log_rect.w, CS.log_rect.h # The mapview size. May be smaller or larger than the actual map size. # This is the location on the screen where the map or a piece thereof # is drawn. It's not an actual surface, but a logical rectangle. CS.mapview_rect.w = CS.screen_rect.w - cfg.SCROLLBAR_W CS.mapview_rect.h = (CS.screen_rect.h - CS.status_rect.h - cfg.SCROLLBAR_W) # Locations to blit equipment on the equipment panel eq_cent = (int(CS.eq_rect.w / 2.0 - cfg.TILE_W / 2), int(CS.eq_rect.h / 2.0 - cfg.TILE_W / 2)) CS.eq_hands = (eq_cent[0], CS.eq_rect.y + cfg.TILE_H / 2 + 3 * cfg.TILE_H) CS.eq_rweap = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1]) CS.eq_lweap = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1]) CS.eq_rring = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] + cfg.TILE_H) CS.eq_lring = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1] + cfg.TILE_H) CS.eq_boots = (CS.eq_hands[0], CS.eq_hands[1] + cfg.TILE_H * 2) CS.eq_armor = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H) CS.eq_shirt = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H) CS.eq_cloak = (CS.eq_hands[0] + cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H) CS.eq_neck = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H * 2) CS.eq_eyes = (CS.eq_hands[0] - cfg.TILE_W, CS.eq_hands[1] - cfg.TILE_H * 2) CS.eq_quiver = (CS.eq_hands[0] + cfg.TILE_W * 2, CS.eq_hands[1] - cfg.TILE_H * 3) CS.eq_light = (CS.eq_hands[0] - cfg.TILE_W * 2, CS.eq_hands[1] - cfg.TILE_H * 3) CS.eq_head = (CS.eq_hands[0], CS.eq_hands[1] - cfg.TILE_H * 3) CS.screen = pygame.display.set_mode((CS.screen_rect.w, CS.screen_rect.h), pygame.RESIZABLE) # pylint: disable=E1121 CS.eq_surf = Surface((CS.eq_rect.w, CS.eq_rect.h)).convert() CS.status_surf = Surface((CS.status_rect.w, CS.status_rect.h)).convert() CS.map_surf = Surface((CS.map_rect.w, CS.map_rect.h)).convert() # pylint: enable=E1121 # Set the system icon system_icon = image.load_image('icon.xpm') pygame.display.set_icon(system_icon) CS.tiles_img = image.load_image('tiles16.xpm') CS.gray_tiles_img = image.load_image('tiles16_gray.xpm') CS.menu_bg_img = image.load_image('parchment.jpg') CS.tile_dict = image.create_tile_dict() CS.blank_tile = CS.tile_dict['cmap, wall, dark'] CS.x_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 0, CS.map_rect, CS.mapview_rect, always_show=False) CS.y_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 1, CS.map_rect, CS.mapview_rect, always_show=False) CS.log_scrollbar = ScrollBar(cfg.SCROLLBAR_W, 1, CS.log_rect, CS.logview_rect, always_show=False) # Process any initial setup data we've gotten from the server. This # should include the first dungeon level and the Player object. handle_responses() # Set up keystroke actions keys.attach_key_actions() # Make sure everything is aligned correctly gui.center_map() gui.handle_resize(CS.screen_rect.w, CS.screen_rect.h)