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 01
-
Space Escape Episode 01
First step is to setup our Scene, View, and EventManager
Import:
ui
Module is imported withinscene
Module and by importing scene using the*
wildcard we can accessui
through scene.
using*
is the same as saying:from scene import all
there is no Performance gain from this and if you import ui yourself it will use the same instance used within scene. only gain here is ww save a line of code.
Create a new script named
main.py
from scene import *
Globals:
Avoid using Global Variables. Couple reasons for this is scene's EventLoop doesnt handle them properly and second there is no functionality to them. with a function you can apply Logic before returning the value and you return a new object each time so our Event loop does not need to maintain a reference to it.
A() => Action
will be used for creating Animations In Later Episodes.def A(): return Action
EventManager
We will only have one
EventManager
each game session. Therefore we will not be usingself
to refer to an instance. Instead we will uscls
to refer to theclass
object. Everything works just about the same only bug difference is we will be uing a Decorator for our methods called@classmethod
this provides access to ourclass
variables
just like we would usingself
.@classmethod
stores a copy of the classdict
insidecls
EventManager
will be used to handle all the interactions between our game objects and Touch Events. We will Alo hanle and Timers we need to here.-
EventManager.main
-
- reference to our
Scene
object
- reference to our
-
EventManager.hud
[^hud] -
- reference to our
View
object.
- reference to our
class EventManager(object): main=None hud=None
EventManager.setup(*args)
[^args]-
- Called before
Scene.setup()
inside ourHUD.__init__()
- Called before
@classmethod def setup(cls, *args): cls.main=args[0] cls.hud=args[1]
EventManager.update(dt)
[^dt]-
- Called every frame from
Scene.update
and passed dt to be used with timers
- Called every frame from
@classmethod def update(cls, dt): pass
EventManager.touch_began()
,EventManager.touch_moved()
,EventManager.touch_ended()
-
- Touch events send from
Scene
object
- Touch events send from
@classmethod def touch_began(cls, touch): pass @classmethod def touch_moved(cls, touch): pass @classmethod def touch_ended(cls, touch): pass
Main:
Standard
Scene
setup providing all event calls toEventManager
class Main(Scene): def setup(self): pass def did_change_size(self): pass def update(self): EventManager.update(self.dt) def touch_began(self, touch): EventManager.touch_began(touch) def touch_moved(self, touch): EventManager.touch_moved(touch) def touch_ended(self, touch): EventManager.touch_ended(touch)
HUD:
main
view
container to hold all our ui elements. we are only setting ourSceneView
to display our Game's windown and callingEventManager.setup
passing ourHUD
andMain
refrences.class HUD(ui.View): def __init__(self, *args, **kwargs): # Game Window self.main = SceneView( scene=Main(), frame=(5, 5, 600, 800), anti_alias=True,frame_interval=1, shows_fps=True, background_color='#2c00ff') self.add_subview(self.main) EventManager.setup(self.main.scene, self)
Finally if we are running this Script and not Importing it then present our game Windows!
if name=='main':
HUD().present('fullscreen', hide_title_bar=True)
In the next Episode we will start some Presetting And Setup Our Draft of the main menu
[^args]: args, kwargs, list arguments and dict keyword arguments
[^hud]: hud, Heads Up Diplay.
[^dt]: dt, DeltaTime, Time passed since last Frame. Set every Frame. -
-
@stephen, just curious, what is the benefit or goal of using EventManager as ”class only”?
-
@mikael By using
EventManager
as a class and not creating an instance it insures i only have oneEventManager
at a time. since i handle the touch checks, timers, hit collision and other events in this class it should both keep fom using more memory from instances and should never be garbage collected since it maintains an update call fromScene.update
. -
@mikael lol well ive never checked the memory lol so in theory this is what i expect lol
-
This post is deleted!