4 from pygame
.locals import *
7 """Template Class -- for a state.
9 <pre>State(game,value...)</pre>
12 <dt>game<dd>The state engine.
13 <dt>value<dd>I usually pass in a custom value to a state
16 <p>For all of the template methods, they should return None unless they return
17 a new State to switch the engine to.</p>
19 def __init__(self
,game
,value
=None):
20 self
.game
,self
.value
= game
,value
22 """Template Method - Initialize the state, called once the first time a state is selected.
24 <pre>State.init()</pre>
27 def paint(self
,screen
):
28 """Template Method - Paint the screen. Called once after the state is selected.
30 <p>State is responsible for calling <tt>pygame.display.flip()</tt> or whatever.</p>
32 <pre>State.paint(screen)</pre>
37 """Template Method - Request a repaint of this state.
39 <pre>State.repaint()</pre>
42 def update(self
,screen
):
43 """Template Method - Update the screen.
45 <p>State is responsible for calling <tt>pygame.display.update(updates)</tt> or whatever.</p>
47 <pre>State.update(screen)</pre>
51 """Template Method - Run a logic loop, called once per frame.
53 <pre>State.loop()</pre>
57 """Template Method - Recieve an event.
59 <pre>State.event(e)</pre>
64 """A state to quit the state engine.
66 <pre>Quit(game,value)</pre>
73 """Template Class - The state engine.
75 def fnc(self
,f
,v
=None):
77 if not hasattr(s
,f
): return 0
79 if v
!= None: r
= f(v
)
87 def run(self
,state
,screen
=None):
88 """Run the state engine, this is a infinite loop (until a quit occurs).
90 <pre>Game.run(state,screen=None)</pre>
93 <dt>game<dd>a state engine
94 <dt>screen<dd>the screen
99 if screen
!= None: self
.screen
= screen
108 if not hasattr(s
,'_init') or s
._init
:
110 if self
.fnc('init'): return
112 if self
.fnc('loop'): return
113 if not hasattr(s
,'_paint') or s
._paint
:
115 if self
.fnc('paint',self
.screen
): return
117 if self
.fnc('update',self
.screen
): return
119 for e
in pygame
.event
.get():
120 #NOTE: this might break API?
121 #if self.event(e): return
122 if not self
.event(e
):
123 if self
.fnc('event',e
): return
129 """Template Method - called at the beginning of State.run() to initialize things.
131 <pre>Game.init()</pre>
136 """Template Method - called once per frame, usually for timer purposes.
138 <pre>Game.tick()</pre>
143 """Template Method - called with each event, so the engine can capture special events.
145 <pre>Game.event(e): return captured</pre>
147 <p>return a True value if the event is captured and does not need to be passed onto the current
151 self
.state
= Quit(self
)
154 # vim: set filetype=python sts=4 sw=4 noet si :