Oct-16-2021, 03:59 PM
I say everything in a class unless it make sense to use function. Avoid global variables like the plague. Keep it simple. Refactor to code to smallest component. Have it do one job. Have it do it well.
You can always use class to store data then pass it to state.
I been playing with the idea of GameManager. You just pass it to state/scene.
My State/Scene
You can always use class to store data then pass it to state.
I been playing with the idea of GameManager. You just pass it to state/scene.
My State/Scene
from .timer_engine import TimerEngine class Scene: scenes = {} def __init__(self, manager, name=""): self.manager = manager self.engine = manager.engine self.timer = TimerEngine() if name == "": name = self.__class__.__name__ if name: Scene.scenes[name] = self def engine_update(self): self.timer.update() def on_draw(self, surface): pass def on_event(self, event): pass def on_focus(self, last_scene): pass def on_update(self, delta, ticks): pass def on_quit(self): self.engine.running = False def set_scene(self, scene): if isinstance(scene, Scene): self.engine.next_scene = scene else: self.engine.next_scene = Scene.scenes[scene]My Display Engine
import pygame from .timer_engine import Timer class DisplayEngine: def __init__(self, caption, width, height, flags=0): pygame.display.set_caption(caption) self.surface = pygame.display.set_mode((width, height), flags) self.rect = self.surface.get_rect() self.clock = pygame.time.Clock() self.running = False self.delta = 0 self.fps = 60 self._current_scene = None self.next_scene = None self.extension = [] self.ticks = 0 def set_scene(self, scene): self._current_scene = scene def mainloop(self): self.running = True while self.running: if self.next_scene: self.next_scene.on_focus(self._current_scene) self._current_scene = self.next_scene self.next_scene = None if self._current_scene is not None: for event in pygame.event.get(): if event.type == pygame.QUIT: self._current_scene.on_quit() else: self._current_scene.on_event(event) Timer.update_ticks() self.ticks = Timer.ticks self._current_scene.engine_update() self._current_scene.on_draw(self.surface) self._current_scene.on_update(self.delta, self.ticks) else: self.surface.fill('gray10') for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False for ext in self.extension: ext.update(self) pygame.display.flip() self.delta = self.clock.tick(self.fps) * 0.001My Game Manager
import pygame from .display_engine import DisplayEngine from .image_manager import ImageManager class GameManager: def __init__(self, caption, width, height, flags=0): pygame.init() self.engine = DisplayEngine(caption, width, height, flags) self.image = None self.data = None def data(self, data): self.data = data def images(self, image_path): self.images = ImageManager(image_path) def run(self): self.engine.mainloop() pygame.quit() def scene(self, scene): if self.engine: self.engine.set_scene(scene(self))
99 percent of computer problems exists between chair and keyboard.