File size: 4,202 Bytes
15f5485 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | """a state engine.
"""
import pygame
from pygame.locals import *
class State:
"""Template Class -- for a state.
<pre>State(game,value...)</pre>
<dl>
<dt>game<dd>The state engine.
<dt>value<dd>I usually pass in a custom value to a state
</dl>
<p>For all of the template methods, they should return None unless they return
a new State to switch the engine to.</p>
"""
def __init__(self,game,value=None):
self.game,self.value = game,value
def init(self):
"""Template Method - Initialize the state, called once the first time a state is selected.
<pre>State.init()</pre>
"""
return
def paint(self,screen):
"""Template Method - Paint the screen. Called once after the state is selected.
<p>State is responsible for calling <tt>pygame.display.flip()</tt> or whatever.</p>
<pre>State.paint(screen)</pre>
"""
return
def repaint(self):
"""Template Method - Request a repaint of this state.
<pre>State.repaint()</pre>
"""
self._paint = 1
def update(self,screen):
"""Template Method - Update the screen.
<p>State is responsible for calling <tt>pygame.display.update(updates)</tt> or whatever.</p>
<pre>State.update(screen)</pre>
"""
return
def loop(self):
"""Template Method - Run a logic loop, called once per frame.
<pre>State.loop()</pre>
"""
return
def event(self,e):
"""Template Method - Recieve an event.
<pre>State.event(e)</pre>
"""
return
class Quit(State):
"""A state to quit the state engine.
<pre>Quit(game,value)</pre>
"""
def init(self):
self.game.quit = 1
class Game:
"""Template Class - The state engine.
"""
def fnc(self,f,v=None):
s = self.state
if not hasattr(s,f): return 0
f = getattr(s,f)
if v != None: r = f(v)
else: r = f()
if r != None:
self.state = r
self.state._paint = 1
return 1
return 0
def run(self,state,screen=None):
"""Run the state engine, this is a infinite loop (until a quit occurs).
<pre>Game.run(state,screen=None)</pre>
<dl>
<dt>game<dd>a state engine
<dt>screen<dd>the screen
</dl>
"""
self.quit = 0
self.state = state
if screen != None: self.screen = screen
self.init()
while not self.quit:
self.loop()
def loop(self):
s = self.state
if not hasattr(s,'_init') or s._init:
s._init = 0
if self.fnc('init'): return
else:
if self.fnc('loop'): return
if not hasattr(s,'_paint') or s._paint:
s._paint = 0
if self.fnc('paint',self.screen): return
else:
if self.fnc('update',self.screen): return
for e in pygame.event.get():
#NOTE: this might break API?
#if self.event(e): return
if not self.event(e):
if self.fnc('event',e): return
self.tick()
return
def init(self):
"""Template Method - called at the beginning of State.run() to initialize things.
<pre>Game.init()</pre>
"""
return
def tick(self):
"""Template Method - called once per frame, usually for timer purposes.
<pre>Game.tick()</pre>
"""
pygame.time.wait(10)
def event(self,e):
"""Template Method - called with each event, so the engine can capture special events.
<pre>Game.event(e): return captured</pre>
<p>return a True value if the event is captured and does not need to be passed onto the current
state</p>
"""
if e.type is QUIT:
self.state = Quit(self)
return 1
# vim: set filetype=python sts=4 sw=4 noet si :
|