Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import os
- class Scene:
- # SceneManager Interface
- def scene_draw(self, surface):
- self.on_draw(surface)
- def scene_drop(self):
- self.on_drop()
- def scene_event(self):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.on_quit()
- else:
- self.on_event(event)
- def scene_focus(self, *args, **kwargs):
- self.on_focus(*args, **kwargs)
- def scene_new(self,*args, **kwargs):
- self.on_new(*args, **kwargs)
- def scene_update(self, delta):
- self.on_update(delta)
- def flip(self, scene, *args, **kwargs):
- SceneManager.flip(scene, *args, **kwargs)
- def flip_new(self, scene, *args, **kwargs):
- SceneManager.flip_new(scene, *args, **kwargs)
- def on_quit(self):
- SceneManager.quit()
- # Scene Interface
- def on_draw(self, surface): pass
- def on_drop(self): pass
- def on_event(self, event): pass
- def on_focus(self): pass
- def on_new(self): pass
- def on_update(self, delta): pass
- class SceneManager:
- @classmethod
- def create(cls, title, width, height, center=False):
- if center is True:
- os.environ['SDL_VIDEO_CENTERED'] = '1'
- elif center:
- os.environ['SDL_VIDEO_WINDOW_POS'] = '{0}, {1}'.format(*center)
- # Basic pygame setup
- pygame.display.set_caption(title)
- cls.surface = pygame.display.set_mode((width, height))
- cls.rect = cls.surface.get_rect()
- cls.clock = pygame.time.Clock()
- cls.running = False
- cls.delta = 0
- cls.fps = 60
- # Scene handling
- cls._scene = Scene()
- cls.scenes = {}
- cls.extension = []
- @classmethod
- def add_scene(cls, scene, name=None):
- if name is None:
- name = scene.__class__.__name__
- cls.scenes[name] = scene
- @classmethod
- def _flip(cls, scene):
- cls._scene.scene_drop()
- if isinstance(scene, Scene):
- cls._scene = scene
- else:
- # String: Load live scene
- cls._scene = cls.scenes[scene]
- @classmethod
- def flip(cls, scene, *args, **kwargs):
- cls._flip(scene)
- cls._scene.scene_focus(*args, **kwargs)
- @classmethod
- def flip_new(cls, scene, *args, **kwargs):
- cls._flip(scene)
- cls._scene.scene_new(*args, **kwargs)
- @classmethod
- def mainloop(cls):
- cls.running = True
- while cls.running:
- cls._scene.scene_event()
- cls._scene.scene_update(cls.delta)
- cls._scene.scene_draw(cls.surface)
- for extension in cls.extension:
- extension(cls)
- pygame.display.flip()
- cls.delta = cls.clock.tick(cls.fps)
- @classmethod
- def quit(cls):
- cls.running = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement