Oct-03-2016, 12:14 PM
Here how I handle it
scene.py
example.py
scene.py
import pygame_sdl2 as pygame # internal working of my scene. class Scene(object): # scene loading before seen def has_focus(self): pass # scene dumping before switching def lost_focus(self): pass # scene drawing def blit(self, surface): pass # scene time updates def update(self, tick): pass # scene events def event(self, event): pass # scene switching class Handler(object): # for passing variables without passing object scenes = {} fps = None running = False set_scene = None size = None # set caption and window def __init__(self, caption, size, flags=0, depth=0): Handler.size = size self.current_scene = Scene() self.screen = pygame.display.set_mode(size, flags, depth) pygame.display.set_caption(caption) self.clock = pygame.time.Clock() # main loop def loop(self, start_scene=None, fps=60): Handler.fps = fps Handler.running = True Handler.set_scene = start_scene while Handler.running: if Handler.set_scene: self.current_scene.lost_focus() self.current_scene = Handler.scenes[Handler.set_scene] self.current_scene.has_focus() Handler.set_scene = None for event in pygame.event.get(): self.current_scene.event(event) self.current_scene.blit(self.screen) tick = pygame.time.get_ticks() self.current_scene.update(tick) pygame.display.flip() self.clock.tick(Handler.fps) # function for quick access to variables. def add_scene(scene_name, scene): Handler.scenes[scene_name] = scene def set_scene(scene_name): Handler.set_scene = scene_name def quit_scene(): Handler.running = False def get_screensize(): return Handler.size def set_fps(fps): Handler.fps = fpsI just import my boilerplate
example.py
import pygame_sdl2 as pygame import scene pygame.init() class Example(scene.Scene): def __init__(self): scene.Scene.__init__(self) # scene variables here def blit(self, surface): surface.fill((0,0,120)) # draw code here def event(self, event): # event code here if event.type == pygame.QUIT: scene.quit_scene() if __name__ == '__main__': handler = scene.Handler('Example', (800,600)) E = Example() scene.add_scene('Example', E) handler.loop('Example') pygame.quit()
99 percent of computer problems exists between chair and keyboard.