Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Still working on this.
- import pygame.display
- from pygame.time import Clock
- class Scene:
- def __init__(self, control):
- self.control = control
- def on_focus_gain(self): pass
- def on_focus_lost(self): pass
- def on_draw(self, surface): pass
- def on_event(self, event): pass
- def on_update(self, delta): pass
- # Interface, Control Display Handler
- def clear_stack(self):
- self.control.handler.stack_clear()
- def pop_scene(self):
- self.control.handler.stack_pop()
- def push_scene(self):
- self.control.handler.stack_push()
- def set_scene(self, scene):
- self.control.handler.set_scene(scene)
- def on_quit(self):
- self.control.quit()
- class SceneHandler:
- def __init__(self):
- self.current_scene = None
- self.next_scene = None
- self.scenes = {}
- self.stack = []
- def set_scene(self, scene):
- if isinstance(scene, Scene):
- push_scene = scene
- else:
- push_scene = self.scenes[scene]
- if push_scene:
- if self.current_scene is None:
- self.current_scene = push_scene
- else:
- self.next_scene = push_scene
- def stack_clear(self):
- self.stack = []
- def stack_empty(self):
- return len(self.stack == 0)
- def stack_pop(self):
- if len(self.stack) > 0:
- self.next_scene = self.stack[-1]
- self.stack = self.stack[:-1]
- def stack_push(self):
- if self.current_scene:
- self.stack.append(self.current_scene)
- def update(self):
- if self.next_scene:
- if self.current_scene:
- self.current_scene.on_focus_lost()
- self.current_scene = self.next_scene
- self.current_scene.on_focus_gain()
- self.next_scene = None
- return self.current_scene
- class Display:
- def __init__(self, caption, width, height, flags=0, fps=60):
- pygame.display.set_caption(caption)
- self.display = pygame.display.set_mode((width, height), flags)
- self.rect = self.display.get_rect()
- self.clock = Clock()
- self.running = True
- self.delta = 0
- self.fps = fps
- self.handler = SceneHandler()
- def loop(self):
- while (self.running):
- scene = self.handler.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- scene.on_quit()
- else:
- scene.on_event(event)
- scene.on_update(self.delta)
- scene.on_draw(self.display)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps) / 1000
- class SceneControl:
- def __init__(self, caption, width, height, flags=0, fps=60):
- self.display = Display(caption, width, height, flags, fps)
- self.handler = self.display.handler
- def add_scene(self, key, scene):
- self.handler.scenes[key] = scene
- def run(self, scene=None):
- if scene is None:
- scene = Scene(self)
- self.handler.set_scene(scene)
- self.display.loop()
- def quit(self):
- self.display.running = False
Advertisement
Add Comment
Please, Sign In to add comment