Daniel Gabriele 2009

  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
# simulation.py
from collections import deque
import copy
import pygame
#``````````````````````!
class Simulator(object):
def __init__(self, resolution=None, fullscreen=False):
if fullscreen:
FS = pygame.FULLSCREEN
else:
FS = 0
self._winrect = pygame.Rect((0, 0) + resolution)
self._screen = pygame.display.set_mode(resolution, FS)
self._clock = pygame.time.Clock()
self._running = False
self._deltat = 0
self._fps = 36
self._kctrl = False
self._kmeta = False
self._ksuper = False
self._event = None
self._event_buffer = deque()
self._event_buffer_size = 5
self._modes = deque()
def main(self):
self._mode = self._modes[0]
self._mode.winrect = self._winrect
self._mode.load_content()
self._mode._initialize()
self._running = True
while self._running:
self._clock.tick(self._fps)
self._deltat = pygame.time.get_ticks()
for e in pygame.event.get():
if not self._mode.paused:
self._mode.set_event(e)
self._on_global_event(e)
self._buffer_events(e)
if not self._mode.paused:
self._mode._update(self._deltat)
self._mode.draw(self._screen)
self._mode.refresh()
## Global Event Handling ---------------------------------------!
def _buffer_events(self, e):
if not self._event_buffer:
self._event_buffer.append(e)
return
if len(self._event_buffer) == self._event_buffer_size:
self._event_buffer.popleft()
self._event_buffer.append(e)
def _on_global_event(self, e):
self._set_keymode_flags(e)
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
self._flip_modes(e)
elif e.key == pygame.K_ESCAPE:
self._running = False
def _set_keymode_flags(self, e):
if hasattr(e, 'key'):
if e.key == pygame.K_LCTRL or e.key == pygame.K_RCTRL:
if e.type == pygame.KEYDOWN:
self._kctrl = True
else:
self._kctrl = False
elif e.key == pygame.K_LMETA or e.key == pygame.K_RMETA:
if e.type == pygame.KEYDOWN:
self._kalt = True
else:
self._kalt = False
elif e.key == pygame.K_LSUPER or e.key == pygame.K_RSUPER:
if e.type == pygame.KEYDOWN:
self._ksuper = True
else:
self._ksuper = False
def _flip_modes(self, _e):
if _e.key == pygame.K_LEFT and self._kctrl:
self._mode.pause()
self._modes.rotate()
self._mode = self._modes[0]
self._mode.unpause()
elif _e.key == pygame.K_RIGHT and self._kctrl:
self._mode.pause()
self._modes.rotate(-1)
self._mode = self._modes[0]
self._mode.unpause()
## -------------------------------------------------------------!
def __getitem__(self, i):
return self._modes[i]
def __contains__(self, label):
for mode in self._modes:
if mode.label and mode.label == label:
return True
else:
return False
def __add__(self, mode):
if isinstance(mode, Mode):
mode.winrect = copy.copy(self._winrect)
self._modes.append(mode)
return self
def __len__(self):
return len(self._modes)
#`````````````````!
class Mode(object):
def __init__(self, label=None):
self._event_type_map = { pygame.KEYDOWN : {},
pygame.KEYUP : {},
pygame.MOUSEMOTION : {},
pygame.MOUSEBUTTONDOWN : {},
pygame.MOUSEBUTTONUP : {} }
pygame.event.set_allowed(self._event_type_map.keys())
self._timers = {}
self.event = None
self.paused = False
self.wasinit = False
self.reset = False
self.label = label
@property
def winrect(self):
return self._winrect
@winrect.setter
def winrect(self, rect):
if not isinstance(rect, pygame.Rect):
rect = pygame.Rect(rect)
self._winrect = rect
## Derivable Methods --------------------------------------------!
def _initialize(self):
self.wasinit = True
self.initialize()
def load_content(self):
pass
def unload_content(self):
pass
def initialize(self):
pass
def _update(self, deltat):
self.deltat = deltat
if self.reset:
self.unload_content()
self.load_content()
self.initialize()
self.reset = False
self.update()
def update(self):
pass
def draw(self, screen):
pass
def refresh(self):
pygame.display.flip()
## -------------------------------------------------------------!
def pause(self):
self.paused = True
def unpause(self):
if self.paused:
self.paused = False
if not self.wasinit:
self.load_content()
self._initialize()
def new_event_handler(self, type, predicate, callback):
self._event_type_map[type][predicate] = callback
def new_event_timer(self, user_event, delay, callback):
pygame.time.set_timer(user_event, delay)
self._timers[user_event] = callback
def set_event(self, e):
self.event = e
if e.type in self._timers:
self._timers[e.type]()
elif e.type in self._event_type_map:
for predicate in self._event_type_map[e.type]:
if predicate is None or predicate(e):
self._event_type_map[e.type][predicate]()
return
# TESTING ...............!
if __name__ == "__main__":
sim = Simulator((500, 500), False)
sim += Mode("test")
sim.main()