Windspar

My Pygame Scene Handler.

Jun 12th, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | Gaming | 0 0
  1. # Still working on this.
  2. import pygame.display
  3. from pygame.time import Clock
  4.  
  5. class Scene:
  6.     def __init__(self, control):
  7.         self.control = control
  8.  
  9.     def on_focus_gain(self): pass
  10.     def on_focus_lost(self): pass
  11.  
  12.     def on_draw(self, surface): pass
  13.     def on_event(self, event): pass
  14.     def on_update(self, delta): pass
  15.  
  16.     # Interface, Control Display Handler
  17.     def clear_stack(self):
  18.         self.control.handler.stack_clear()
  19.  
  20.     def pop_scene(self):
  21.         self.control.handler.stack_pop()
  22.  
  23.     def push_scene(self):
  24.         self.control.handler.stack_push()
  25.  
  26.     def set_scene(self, scene):
  27.         self.control.handler.set_scene(scene)
  28.  
  29.     def on_quit(self):
  30.         self.control.quit()
  31.  
  32. class SceneHandler:
  33.     def __init__(self):
  34.         self.current_scene = None
  35.         self.next_scene = None
  36.         self.scenes = {}
  37.         self.stack = []
  38.  
  39.     def set_scene(self, scene):
  40.         if isinstance(scene, Scene):
  41.             push_scene = scene
  42.         else:
  43.             push_scene = self.scenes[scene]
  44.  
  45.         if push_scene:
  46.             if self.current_scene is None:
  47.                 self.current_scene = push_scene
  48.             else:
  49.                 self.next_scene = push_scene
  50.  
  51.     def stack_clear(self):
  52.         self.stack = []
  53.  
  54.     def stack_empty(self):
  55.         return len(self.stack == 0)
  56.  
  57.     def stack_pop(self):
  58.         if len(self.stack) > 0:
  59.             self.next_scene = self.stack[-1]
  60.             self.stack = self.stack[:-1]
  61.  
  62.     def stack_push(self):
  63.         if self.current_scene:
  64.             self.stack.append(self.current_scene)
  65.  
  66.     def update(self):
  67.         if self.next_scene:
  68.             if self.current_scene:
  69.                 self.current_scene.on_focus_lost()
  70.  
  71.             self.current_scene = self.next_scene
  72.             self.current_scene.on_focus_gain()
  73.             self.next_scene = None
  74.  
  75.         return self.current_scene
  76.  
  77. class Display:
  78.     def __init__(self, caption, width, height, flags=0, fps=60):
  79.         pygame.display.set_caption(caption)
  80.         self.display = pygame.display.set_mode((width, height), flags)
  81.         self.rect = self.display.get_rect()
  82.         self.clock = Clock()
  83.         self.running = True
  84.         self.delta = 0
  85.         self.fps = fps
  86.  
  87.         self.handler = SceneHandler()
  88.  
  89.     def loop(self):
  90.         while (self.running):
  91.             scene = self.handler.update()
  92.             for event in pygame.event.get():
  93.                 if event.type == pygame.QUIT:
  94.                     scene.on_quit()
  95.                 else:
  96.                     scene.on_event(event)
  97.  
  98.             scene.on_update(self.delta)
  99.             scene.on_draw(self.display)
  100.             pygame.display.flip()
  101.             self.delta = self.clock.tick(self.fps) / 1000
  102.  
  103. class SceneControl:
  104.     def __init__(self, caption, width, height, flags=0, fps=60):
  105.         self.display = Display(caption, width, height, flags, fps)
  106.         self.handler = self.display.handler
  107.  
  108.     def add_scene(self, key, scene):
  109.         self.handler.scenes[key] = scene
  110.  
  111.     def run(self, scene=None):
  112.         if scene is None:
  113.             scene = Scene(self)
  114.  
  115.         self.handler.set_scene(scene)
  116.         self.display.loop()
  117.  
  118.     def quit(self):
  119.         self.display.running = False
  120.  
Advertisement
Add Comment
Please, Sign In to add comment