Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Scene Game Tutorial. Episode 03
-
Space Escape Episode 03
The reason im doing this tutorial in episodes instead of all at once is so it can evolve over time so to help the community as best it can. that said we have our first contribution! after the fact of losing standard functionality of classes with python by not using Instances i ran a few tests and found that we can return the built in functionality while maintaining only a single instance by creating a Singleton. Attribution for this Episode goes to @mikael for helping improve quality
main focus this Episode will be converting our
EventManager
into a singleton."The singleton pattern is a design pattern that restricts the instantiation of a class to one object."
we add the magic Dunder method__new__
so we can set our classmethod_instance
so we can maintain the singleton status by returningcls._instance
ifcls._instance
is notNone
to__init__
.class EventManager: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) return cls._instance def __init__(self, *arg, **kwargs): self.main=None self.hud=None self.game_state=None def start(self, main, hud): self.main=main self.hud=hud self.game_state=GameState.MAIN_MENU self.main.view.hidden=True self.hud.add_subview(MainMenu()) def update(self, dt): pass def touch_began(self, touch): pass def touch_moved(self, touch): pass def touch_ended(self, touch): pass
In
Main
we grab a refernce toEventManager
fromHUD
class Main(Scene): def setup(self): self.event_manager=self.view.superview.event_manager def did_change_size(self): pass def update(self): self.event_manager.update(self.dt) def touch_began(self, touch): self.event_manager.touch_began(touch) def touch_moved(self, touch): self.event_manager.touch_moved(touch) def touch_ended(self, touch): self.event_manager.touch_ended(touch)
Inside HUD lets ceate our initial instance of
EventManager
since this is our Top Level Object. then after setting ourSceneView
we call start forHUD.event_manager.start
whichmain
andhud
insideHUD.event_manager
and set our game state andin turn present our main menu.class HUD(ui.View): def __init__(self, *args, **kwargs): self.background_color='#66492a' self.event_manager=EventManager() # Game Window self.main = SceneView( scene=Main(), frame=(5, 5, Screen.x-10, Screen.y-10), anti_alias=True, frame_interval=1, shows_fps=True, background_color='#2c00ff') self.add_subview(self.main) self.event_manager.start(self.main.scene, self)
We will call it here for this one and it wont feel like we did anything but in the long run this Episode will help our project 10-fold.
-
Just wanted to say as a complete beginner this is really helpful! I look forward to the next episode :)
-
@Seb said:
Just wanted to say as a complete beginner this is really helpful! I look forward to the next episode :)
Most Excellent!
@karina @mikael @cvp and others lol
I do appollogise for the quite long delay. I was spending time wih my kids that i hadnt seen in little over a year!
I will now continue this tutorial after i go through all my notes and scripts and get back up to speed. there may be a couple changes to the current episodes but it shouldn't be much and it will be to make is series much easier to follow for anyone having troubles.
🙋🏼♂️
-