def __init__(self): """Called when the the GameWindow object is initialized. Initializes pygame and sets up our pygame window and other pygame tools.""" utils.load_configs() LOGGER.debug('Initializing window') # load and set up pygame pygame.init() # detect display resolution and shrink resolution if needed info = pygame.display.Info() displ_w, displ_h = info.current_w, info.current_h wnd_w, wnd_h = utils.SCREEN_SIZE if displ_w < wnd_w or displ_h < wnd_h: wnd_w, wnd_h = displ_w - 50, displ_h - 50 utils.SCREEN_SIZE = wnd_w, wnd_h # initial window position at the center of the screen center_x, center_y = (displ_w - wnd_w) / 2, (displ_h - wnd_h) / 2 os.environ['SDL_VIDEO_WINDOW_POS'] = str(center_x) + ',' + str(center_y) # create our window self.window = pygame.display.set_mode(utils.SCREEN_SIZE) # clock for ticking self.clock = pygame.time.Clock() # set the window title pygame.display.set_caption("Ancient Empires") # disable mouse pygame.mouse.set_visible(1) # tell pygame to only pay attention to certain events # we want to know if the user hits the X on the window, and we # want keys so we can close the window with the esc key #pygame.event.set_allowed([]) self._game_state = GameStateManager()
class Window(object): '''Manages general game initialization and running game loop. Delegates updates and event handling to gamestatemanager object. ''' def __init__(self): """Called when the the GameWindow object is initialized. Initializes pygame and sets up our pygame window and other pygame tools.""" utils.load_configs() LOGGER.debug('Initializing window') # load and set up pygame pygame.init() # detect display resolution and shrink resolution if needed info = pygame.display.Info() displ_w, displ_h = info.current_w, info.current_h wnd_w, wnd_h = utils.SCREEN_SIZE if displ_w < wnd_w or displ_h < wnd_h: wnd_w, wnd_h = displ_w - 50, displ_h - 50 utils.SCREEN_SIZE = wnd_w, wnd_h # initial window position at the center of the screen center_x, center_y = (displ_w - wnd_w) / 2, (displ_h - wnd_h) / 2 os.environ['SDL_VIDEO_WINDOW_POS'] = str(center_x) + ',' + str(center_y) # create our window self.window = pygame.display.set_mode(utils.SCREEN_SIZE) # clock for ticking self.clock = pygame.time.Clock() # set the window title pygame.display.set_caption("Ancient Empires") # disable mouse pygame.mouse.set_visible(1) # tell pygame to only pay attention to certain events # we want to know if the user hits the X on the window, and we # want keys so we can close the window with the esc key #pygame.event.set_allowed([]) self._game_state = GameStateManager() def run(self): """Runs the game. Contains the game loop that computes and renders each frame.""" LOGGER.debug('Game loop started') running = True # run until something tells us to stop while running: # tick pygame clock # you can limit the fps by passing the desired frames per # second to tick() self.clock.tick(utils.FPS) # update the title bar with our frames per second pygame.display.set_caption('Ancient Empires at %d fps' % self.clock.get_fps()) # standard game loop running = self._game_state.handle_events(pygame.event.get()) image = self._game_state.get_rendered_screen() # blit and render the screen self.window.blit(image, (0, 0)) pygame.display.flip() LOGGER.debug('Game loop finished')