def setup(self, conn, display, player): Screen.setup(self, display) self.conn = conn self.player = player self.spritelist.empty() self.spritelist.add(self.player) self.spritelist.change_layer(self.player, 1) self.resize_screen(self.display.get_width(), self.display.get_height())
def __init__(self): Screen.__init__(self) self.conn = None # GUI variables self.gui = GameGUI((1, 1)) # GameGUI object, loaded on setup self.gui_color = pygame.Color(20, 20, 20) self.gui_alpha = 255 self.gui_font = pygame.font.Font("assets/fonts/monobit.ttf", 32) self.gui_font_small = pygame.font.Font("assets/fonts/monobit.ttf", 20) # Stats area self.gui_stats_width = 0 self.gui_stats_x = 0 # Chatlog self.chatlog = ChatLog(self.gui_font) self.chatlog.surface_col = self.gui_color self.chatting = False self.chat_input = "" self.game_surface = pygame.Surface((1, 1)) self.entity_surface = pygame.Surface((1, 1)) self.entity_surface.set_colorkey((255, 0, 255)) self.player = None self.map_updated = False self.map = GameMap() self.camera_pos = Vector2(0, 0) self.keys_held = [] self.playerlist = {} self.acc_name = "" self.acc_pw = "" self.fps_avg = 0 # Timers self.tickers = { "ping": [0, 300], # Ping (keeps connection alive) "anim": [0, 30] # Tile animations } self.ping_timeout = 0 # Map drawing layers self.updatelayers = False self.vis_tiles = pygame.sprite.LayeredDirty() self.spritelist = pygame.sprite.LayeredDirty() # Used for "press enter to continue". 1 when waiting for press, 2 when about to return to menu self.return_mode = 0
def __init__(self): Screen.__init__(self) self.font = pygame.font.Font("assets/fonts/monobit.ttf", 32) self.fontcol_normal = pygame.Color(230, 230, 230) self.fontcol_sel = pygame.Color(50, 200, 200) self.fontcol_input = pygame.Color(50, 50, 200) self.menu_buttons = [] self.menu_selected = 0 self.inputting = False self.backg_rect = (0, 0, 0, 0)
def handle_event(self, event): if event.type == pygame.KEYDOWN: if not self.chatting: if self.keys_held[pygame.K_LSHIFT]: # Shift + key if event.key == pygame.K_q: self.return_mode = 2 else: if event.key == pygame.K_SPACE: # Space - transfer level newlevel_data = self.map.transfer_level( self.player.x, self.player.y) if newlevel_data: self.conn.send("xfer_map|" + newlevel_data[1]) self.load_map(newlevel_data[1]) self.player.set_pos(int(newlevel_data[2]), int(newlevel_data[3])) self.update_camera_plpos() elif event.key == pygame.K_PAGEUP: # PageUp - scroll chatlog self.chatlog.scroll(4) elif event.key == pygame.K_PAGEDOWN: # PageDown - scroll chatlog self.chatlog.scroll(-4) elif event.key == pygame.K_RETURN: # Enter - chat/return to menu if self.return_mode == 0: self.chat_toggle(True) else: self.return_mode = 2 else: # Player chat if event.key == pygame.K_RETURN: # Enter - send message tmp_chat = self.chat_input.strip() if tmp_chat: self.conn.send("pl_chat|" + tmp_chat) self.chat_toggle(False) elif event.key == pygame.K_BACKSPACE: self.chat_input = self.chat_input[:-1] elif event.key == pygame.K_ESCAPE: self.chat_toggle(False) else: self.chat_input += event.unicode self.chatlog.msg_input = self.chat_input return Screen.handle_event(self, event)
def loop(self, framerate): for event in pygame.event.get(): if not self.handle_event(event): return False pygame.draw.rect(self.display, (0, 0, 0), self.backg_rect) pygame.draw.rect(self.display, (255, 255, 255), self.backg_rect, 2) for i, button in enumerate(self.menu_buttons): tmp_color = self.fontcol_normal if i == self.menu_selected: if self.inputting and button.input: tmp_color = self.fontcol_input else: tmp_color = self.fontcol_sel button.render(self.font, self.display, tmp_color) return Screen.loop(self, framerate)
def handle_event(self, event): if event.type == pygame.KEYDOWN: if not self.inputting: if event.key == pygame.K_w or event.key == pygame.K_UP: self.menu_selected -= 1 if self.menu_selected < 0: self.menu_selected = len(self.menu_buttons) - 1 elif event.key == pygame.K_s or event.key == pygame.K_DOWN: self.menu_selected += 1 if self.menu_selected > len(self.menu_buttons) - 1: self.menu_selected = 0 else: # Currently selected button sel_button = self.menu_buttons[self.menu_selected] if not event.key == pygame.K_RETURN: if event.key == pygame.K_BACKSPACE: sel_button.input_text = sel_button.input_text[:-1] self.update_backg_size() elif event.key == pygame.K_ESCAPE: self.inputting = False elif len(sel_button.input_text) < sel_button.input_maxlen: if not sel_button.input_number: sel_button.input_text += event.unicode else: try: sel_button.input_text += str(int( event.unicode)) except ValueError: pass self.update_backg_size() if event.key == pygame.K_RETURN: self.button_press() return Screen.handle_event(self, event)
def reset(self): Screen.reset(self) self.inputting = False self.menu_selected = 0
def reset(self): Screen.reset(self) self.return_mode = 0 self.chat_input = "" self.chatting = False self.chatlog.msg_log = []
def loop(self, framerate): if not self.chatting: self.keys_held = pygame.key.get_pressed() for event in pygame.event.get(): if not self.handle_event(event): return False if self.return_mode == 1: self.update_drawchatlog() # Only update chat return True elif self.return_mode == 2: if self.conn.is_connected(): self.map_updated = False self.conn.send("disconnect") self.conn.disconnect() return "menu_main" if not self.conn.is_connected(): self.chat_log("Could not connect to server.") self.return_mode = 1 return True self.tickers["ping"][1] = 300 * framerate / 60 self.tickers["anim"][1] = 30 * framerate / 60 self.server_listener() # Don't draw until map is in sync with server if not self.map_updated: return Screen.loop(self) self.player_loop() self.entity_surface.fill((255, 0, 255)) # Tile animations if self.tickers["anim"][0] < self.tickers["anim"][1]: self.tickers["anim"][0] += 1 else: self.map.anim_tiles() self.tickers["anim"][0] = 0 if self.updatelayers: self.update_drawlayers() self.updatelayers = False self.update_drawplayers() self.vis_tiles.draw(self.game_surface) if self.map.loaded: # Draw game surface (tiles) self.display.blit(self.game_surface, self.camera_pos) # Draw sprites self.spritelist.update(framerate) self.spritelist.draw(self.entity_surface) self.display.blit(self.entity_surface, self.camera_pos) # Draw GUI self.update_drawGUI(self.map.loaded) # Keep connection alive if self.tickers["ping"][0] < self.tickers["ping"][1]: self.tickers["ping"][0] += 1 else: self.ping_timeout += 1 if self.ping_timeout >= 2: self.chat_log("Lost connection to server.") self.ping_timeout = 0 self.return_mode = 1 return True else: self.conn.send("ping") self.tickers["ping"][0] = 0 return Screen.loop(self)