Exemplo n.º 1
0
class GameManager(object):
    def __init__(self):
        random.seed()
        pygame.display.set_caption('INVADER')
        self.screen = pygame.display.set_mode(SCREEN_RECT.size)
        self.clock = pygame.time.Clock()
        self.scene = SceneTitle(self)
        self.running = True

    def run(self):
        while self.running:
            pressed_keys = pygame.key.get_pressed()
            filtered_events = self.handle_events(pressed_keys)
            if filtered_events is None:
                self.stop_running()
            else:
                self.update(filtered_events, pressed_keys)
                self.draw()
                self.clock.tick(FPS)

    def update(self, filtered_events, pressed_keys):
        self.scene.update(filtered_events, pressed_keys)

    def draw(self):
        self.screen.fill((0, 0, 0))
        self.scene.draw(self.screen)
        pygame.display.update()

    def change_scene(self, next_scene):
        self.scene = next_scene

    def stop_running(self):
        self.running = False

    def handle_events(self, pressed_keys):
        filtered_events = []
        for event in pygame.event.get():
            if event.type == QUIT:
                return None
            elif event.type == KEYDOWN:
                if event.key == K_F4 and (pressed_keys[K_LALT] or pressed_keys[K_RALT]):
                    return None
                elif event.key == K_ESCAPE:
                    return None
            filtered_events.append(event)
        return filtered_events