Space Escape Episode 01
First step is to setup our Scene, View, and EventManager
Import:
uiModule is imported withinsceneModule and by importing scene using the*wildcard we can accessuithrough scene.
using*is the same as saying:
from scene import allthere 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
EventManagereach game session. Therefore we will not be usingselfto refer to an instance. Instead we will usclsto refer to theclassobject. Everything works just about the same only bug difference is we will be uing a Decorator for our methods called@classmethodthis provides access to ourclassvariablesjust like we would usingself.
@classmethod stores a copy of the class dict inside cls
EventManagerwill 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
Sceneobject
- reference to our
-
EventManager.hud[^hud] -
- reference to our
Viewobject.
- 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.updateand 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
Sceneobject
- 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
Scenesetup 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
viewcontainer to hold all our ui elements. we are only setting ourSceneViewto display our Game's windown and callingEventManager.setuppassing ourHUDandMainrefrences.
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.