4 from pygame.locals import *
7 """Template Class -- for a state.
10 game -- The state engine.
11 value -- I usually pass in a custom value to a state
13 For all of the template methods, they should return None unless they return
14 a new State to switch the engine to.
17 def __init__(self,game,value=None):
18 self.game,self.value = game,value
21 """Template Method - Initialize the state, called once the first time a state is selected."""
24 def paint(self,screen):
25 """Template Method - Paint the screen. Called once after the state is selected.
27 State is responsible for calling pygame.display.flip() or whatever.
33 """Template Method - Request a repaint of this state."""
36 def update(self,screen):
37 """Template Method - Update the screen.
39 State is responsible for calling pygame.display.update(updates) or whatever.
45 """Template Method - Run a logic loop, called once per frame."""
49 """Template Method - Recieve an event."""
54 """A state to quit the state engine."""
61 """Template Class - The state engine."""
63 def fnc(self,f,v=None):
65 if not hasattr(s,f): return 0
67 if v != None: r = f(v)
75 def run(self,state,screen=None):
76 """Run the state engine, this is a infinite loop (until a quit occurs).
79 game -- a state engine
85 if screen != None: self.screen = screen
94 if not hasattr(s,'_init') or s._init:
96 if self.fnc('init'): return
98 if self.fnc('loop'): return
99 if not hasattr(s,'_paint') or s._paint:
101 if self.fnc('paint',self.screen): return
103 if self.fnc('update',self.screen): return
105 for e in pygame.event.get():
106 #NOTE: this might break API?
107 #if self.event(e): return
108 if not self.event(e):
109 if self.fnc('event',e): return
114 """Template Method - called at the beginning of State.run() to initialize things."""
118 """Template Method - called once per frame, usually for timer purposes."""
122 """Template Method - called with each event, so the engine can capture special events.
124 Rturn a True value if the event is captured and does not need to be passed onto the current
129 self.state = Quit(self)