Python Forum
[PyGame] Best way to pass data to the few game states with the same superclass?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Best way to pass data to the few game states with the same superclass?
#2
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
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.001
My 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))
Milosz likes this post
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Best way to pass data to the few game states with the same superclass? - by Windspar - Oct-16-2021, 03:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Variables shared between game states, how to? michael1789 5 3,575 Dec-10-2019, 10:09 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020