def __init__(self, evManager): Listener.__init__(self, evManager) # Set-up event handler self.handler = None # Setup the screen pygame.display.set_icon(pygame.image.load(\ os.path.join(PATH_GRAPHICS, "_appicon.ico"))) self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.fullscreen = False #self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN|pygame.HWSURFACE) pygame.display.set_caption(APP_NAME) # Init the text rendering module available to anyone in the GlobalServices module self.textRenderer = TextRenderer(self.screen) GlobalServices.setTextRenderer(self.textRenderer) # Keep track of this object's state self.state = None # Hold a Gameview instance self.gameview = None
class View(Listener, ViewInterface): # Constructor # Parameter: # evManager : Event Manager def __init__(self, evManager): Listener.__init__(self, evManager) # Set-up event handler self.handler = None # Setup the screen pygame.display.set_icon(pygame.image.load(\ os.path.join(PATH_GRAPHICS, "_appicon.ico"))) self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.fullscreen = False #self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN|pygame.HWSURFACE) pygame.display.set_caption(APP_NAME) # Init the text rendering module available to anyone in the GlobalServices module self.textRenderer = TextRenderer(self.screen) GlobalServices.setTextRenderer(self.textRenderer) # Keep track of this object's state self.state = None # Hold a Gameview instance self.gameview = None # Listener interface implementation # Parameter: # event : The event that the event manager has received def notify(self, event): if isinstance(event, GameStateChangedEvent): self.changeHandler(event.object) else: self.handler.handle(event) # Handler change method. Any time the view controller is notified of a game state change event, # this method changes the controller's handler reference. This controller also changes its # currently displayed view to whatever the new game state requires. If the Game object tells # everyone that the state is now "GAME_RUNNING", this controller changes its current view to a GameView. # Parameter: # state : The new state to which the handler is going to be set def changeHandler(self, state): # Clean-up pending text renderer messages self.textRenderer.deleteAll() # Now, change views and handler if state == STATE_MAIN_MENU: self.handler = MainMenuViewHandler(self) self.currentView = MainMenuView.MainMenuView() elif state == STATE_GAME_MAP_LOADING: self.handler = MapLoadingViewHandler(self) self.currentView = MapLoadingView.MapLoadingView() elif state == STATE_GAME_RUNNING: if not self.state in [STATE_GAME_INVENTORY, STATE_GAME_MENU]: if self.gameview is None: self.gameview = GameView.GameView() self.currentView = self.gameview self.currentView.setHighlightedInventoryItem(None) self.handler = GameViewHandler(self) self.currentView.setInventoryDisplay(False) self.currentView.setGameMenuDisplay(False) elif state == STATE_GAME_MENU: self.handler = GameMenuViewHandler(self) self.currentView.setGameMenuDisplay(True) elif state == STATE_GAME_INVENTORY: self.handler = InventoryViewHandler(self) self.currentView.setHighlightedObject(None) self.currentView.setInventoryDisplay(True) # Keep track of the new state self.state = state # ViewInterface implementation # Parameter: # screen : The surface to be drawn onto def render(self, screen): # Fill the screen with black screen.fill(COLOR_BLACK) # Tell the current view to draw itself on top of it if self.currentView is not None: self.currentView.render(screen) # Lastly, tell the text rendering module to render any pending text messages self.textRenderer.render() # Make the rendered image visible to the user pygame.display.flip() # This method toggles the pygame display # between Windowed and Fullscreen display def toggleFullscreen(self): # Flip the switch self.fullscreen = not self.fullscreen # Preserve old contents of the screen content = self.screen.convert() if self.fullscreen: self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN|pygame.HWSURFACE) else: self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Blit old content to the new surface self.screen.blit(content, (0,0))