def __init__(self, items): platforms = [Platform([400 + 260 * i, 390], s[0], i) for i, s in enumerate(items)] credits = Credits() self._score = TopScores() sprites = RenderUpdates([credits, self._score]) sprites.add(platforms) pos = 0 em = EventManager() em.clear() screen = pygame.display.get_surface() em.get() quit = False screen.blit(Menu.bg, [0, 0]) sprites.draw(screen) pygame.display.update() while not quit: for ev in em.get(): if ev.type == PLAYER: if ev.key == LEFT: if pos != 0: pos -= 1 for p in platforms: p.left() elif ev.key == RIGHT: if pos != len(items) - 1: pos += 1 for p in platforms: p.right() else: try: r = items[pos][1][ev.key](self, platforms[pos], pos, ev.key) if r: # If the callback returns true, then we # need to redraw the whole screen and # reread our scores. (If it returns false, # it probably means it just modified some # cart text). self._score.read_scores() screen.blit(Menu.bg, [0, 0]) pygame.display.update() except KeyError: pass elif ev.type == QUIT: quit = True sprites.update(pygame.time.get_ticks()) pygame.display.update(sprites.draw(screen)) sprites.clear(screen, Menu.bg) pygame.time.clock.tick(60)
class Mode: """ A mode has its own clock, event manager, and components. A mode is a state activated and deactivated by the mode state machine. """ def __repr__(self): return self.__class__.__name__ def __str__(self): return self.__repr__() def __init__(self): """ When a mode is created, it only creates an event manager. The mode's components and clock are instantiated when the mode is activated. The mode manager/MSM has to register for transition or quit callbacks through set_X_callback. """ # hold instances of the mode's components when the mode is activated self.active_components = [] self.clock = None # instantiated by MSM self.em = EventManager() # each mode has its own EM def set_transition_callback(self, trans_evt_classes, msm_transition_cb): """ Set the callback to the mode state machine when transition events are fired by the current mode. """ for ev_class in trans_evt_classes: self.em.subscribe(ev_class, msm_transition_cb) def set_quit_callback(self, msm_quit_cb): """ Set the MSM callback to call when the mode fires a QuitEvent. """ self.em.subscribe(QuitEvent, msm_quit_cb) def activate(self, ev): """ Instantiate the components and clock for the mode. The event passed in argument contains data sent by the previous mode. For example, the LevelTransitionMode sends a GameStart event containing the next level number to the GameMode. This way, the GameMode knows which level to instantiate. The GameMode sends a GameWon event containing the score to the LevelTransitionMode so that it can be displayed. Small drawback: to keep the code generic, ALL the components are given in argument the mode's EM and the event. Some components may not need the event, but we can't know in advance, so we pass it anyway. """ em = self.em self.active_components = [comp(em, ev) for comp in self.components] self.clock = Clock(em) self.clock.start() # returns when the clock is turned off, ie transition or quit self.em.clear() # this removes the clock and MSM from the EM subscribers self.active_components = [] # last ref to component instances is lost self.clock = None def deactivate(self): """ Tell the clock to stop. The mode's components will be garbage collected when the clock returns the hand back to the MSM. """ self.clock.stop()