-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.py
More file actions
465 lines (363 loc) · 13.2 KB
/
master.py
File metadata and controls
465 lines (363 loc) · 13.2 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""
Intended to be the top level for
PowerSim. This is the top level parent.
"""
import sys
import pygame
import threading
import time
import random
import Queue
from collections import defaultdict
from pygame.locals import *
from constants import *
import controller
class Master(object):
"""
Top level parent object.
Maintains the pygame program.
"""
def __init__(self, size=None):
"""Initialise the PowerSim game."""
# enable a large buffer to stop crackle in the sound.
pygame.mixer.pre_init(44100, -16, 2, 1024 * 6)
pygame.init()
# set the screen size.
self.size = size or SCREEN_SIZE
# initialise the screen and background.
self.window = pygame.display.set_mode(self.size)
self.screen = pygame.display.get_surface()
# background is used to 'clear' the screen between frames.
self.background = pygame.Surface(self.size).convert()
self.background.fill(GREY)
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
# add a clock.
self.clock = pygame.time.Clock()
# children threads.
self.children = []
# listening children.
self.listening = []
# create the controller.
self.c = controller.Controller()
self.c.register('exit', self.stop)
self.c.register('pause', self.pause_child)
def mainloop(self):
"""Maintain the children and event queue."""
self.running = True
while self.running:
self.clock.tick(FPS)
self.prune()
self.events()
self.redraw()
def prune(self):
"""remove all dead children from children and listening."""
for child in reversed(self.children):
if not child.running.isSet():
self.children.remove(child)
try:
self.listening.remove(child)
except ValueError:
pass
# now fill over their empty space with background.
topleft = child.topleft
area = pygame.Rect(topleft, child.size)
self.screen.blit(self.background, topleft, area)
pygame.display.update([area])
def events(self):
"""tend to the event queue, pass events to global controller."""
for event in pygame.event.get():
# pass the event to children.
if event.type == QUIT:
self.c.send('exit', event)
for child, types in self.listening:
if not child.pause.isSet():
# dont send events to suspended children.
continue
elif event.type in types:
child.events.put(event)
def redraw(self):
"""check each of the children to see if they have a surface to
publish and publish it."""
# a list of dirty rectangles to redraw.
dirty = []
for child in self.children:
if not child.pause.isSet():
# if the child is paused don't redraw.
continue
# request a redraw from the child.
try:
surface = child.redraw()
except NoUpdate:
continue
topleft = child.topleft
area = surface.get_rect(topleft=topleft)
# draw over screen with background first.
self.screen.blit(self.background, topleft, area)
self.screen.blit(surface, topleft)
# create a kind of 'dirty' list to redraw.
dirty.append(area)
pygame.display.update(dirty)
def stop(self, event):
# exit the main loop.
self.running = False
for thread in self.children:
thread.stop()
thread.join()
def pause_child(self, pause):
pause()
def listen(self, child, types):
"""Notify parent that this child is listening for the listed event
types."""
self.listening.append((child, types))
def add_child(self, Child, *args, **kws):
newchild = Child(parent=self, controller=self.c, *args, **kws)
newchild.setDaemon(True)
# child begins in a paused state.
newchild.start()
self.children.append(newchild)
return newchild
def reserve(self, child, topleft, size):
"""Child makes a request to reserve some surface space for it's
blitting. Attempt to give child as much space as possible with top left
as specified."""
# current parent total size.
p_xlen, p_ylen = self.size
# child x and y size request.
c_xlen, c_ylen = size
c_x, c_y = topleft
# limit child topleft to (0, 0) cannot be negative.
a_x, a_y = max(c_x, 0), max(c_y, 0)
# allowable x and y lengths.
a_xlen = min(p_xlen, a_x + c_xlen) - a_x
a_ylen = min(p_ylen, a_y + c_ylen) - a_y
# normalise the size w.r.t zero. Child could request topleft outside
# allowable area.
a_xlen = max(a_xlen, 0)
a_ylen = max(a_ylen, 0)
# create a surface and return it to the child. Also register this
# child's size and topleft.
childsurface = pygame.Surface((a_xlen, a_ylen)).convert()
child.topleft = (a_x, a_y)
child.size = (a_xlen, a_ylen)
return childsurface
class FakeSemaphore(object):
def __init__(self, *args):
pass
def acquire(self):
pass
def release(self):
pass
class BaseChild(threading.Thread):
"""Defines an empty Null child."""
def __init__(self, parent, controller, *args, **kws):
threading.Thread.__init__(self)
self.running = threading.Event()
self.pause = threading.Event()
self.parent = parent
self.controller = controller
# define a queue.
self.events = Queue.Queue()
# define a mapping between object_id and callable fn.
self._bound = defaultdict(set)
# create a clock to limit the FPS.
self.clock = pygame.time.Clock()
self.init(**kws)
# create a 'screen' lock in effect around loop.
self.looplock = FakeSemaphore(1)#threading.Semaphore(1)
self.drawlock = FakeSemaphore(1)#threading.Semaphore(0)
def init(self, **kws):
"""A do nothing initialisation function called from baseclass
__init__."""
pass
def run(self):
self.running.set()
while True:
# halt here if paused.
self.pause.wait()
# check if process is terminated.
if not self.running.isSet():
break
# get all the events.
events = []
while not self.events.empty():
events.append(self.events.get())
# executed once per loop.
self.looplock.acquire()
try:
self._loop(events)
finally:
self.drawlock.release()
self.clock.tick(FPS)
def redraw(self):
self.drawlock.acquire()
try:
screen = self._redraw()
return screen
finally:
self.looplock.release()
def _redraw(self):
raise NoUpdate("BaseChild does not redraw!")
def _loop(self, events):
time.sleep(1.0/FPS)
time.sleep(1.0/FPS)
def stop(self):
"""Override this function to hook into the
stop sequence. Ensure you call _stop at the end."""
self._stop()
def _stop(self):
self.resume()
self.running.clear()
def suspend(self):
self.pause.clear()
def resume(self):
self.pause.set()
def bind(self, name, fn, obj):
"""registers with the controller to listen for
'name' events. matches the recieved event with id(obj)
and calls the mapped fn."""
self.controller.register(name, self._event)
self._bound[id(obj)].update([fn])
def _event(self, object_id):
"""
called by the controller function when a registered
event is sent. object_id was mapped earlier with the appropriate
function call."""
# do not complete request if we are paused.
if not self.pause.isSet():
return
try:
fns = self._bound[object_id]
except KeyError:
pass
else:
for fn in fns:
fn(object_id)
def reserve(self, child, topleft, size):
"""Child makes a request to reserve some surface space for it's
blitting. Attempt to give child as much space as possible with top left
as specified."""
# current parent total size.
p_xlen, p_ylen = self.size
# child x and y size request.
c_xlen, c_ylen = size
c_x, c_y = topleft
# limit child topleft to (0, 0) cannot be negative.
a_x, a_y = max(c_x, 0), max(c_y, 0)
# allowable x and y lengths.
a_xlen = min(p_xlen, a_x + c_xlen) - a_x
a_ylen = min(p_ylen, a_y + c_ylen) - a_y
# normalise the size w.r.t zero. Child could request topleft outside
# allowable area.
a_xlen = max(a_xlen, 0)
a_ylen = max(a_ylen, 0)
# create a surface and return it to the child. Also register this
# child's size and topleft.
childsurface = pygame.Surface((a_xlen, a_ylen)).convert()
child.topleft = (a_x, a_y)
child.size = (a_xlen, a_ylen)
return childsurface
def __contains__(self, pos):
x, y = self.localise(pos)
if x < 0 or y < 0:
return False
x_size, y_size = self.size
if x > x_size or y > y_size:
return False
return True
def localise(self, pos):
# user clicked at absolute position a,b.
a, b = pos
x, y = self.topleft
return (a - x, b - y)
class NoUpdate(Exception):
pass
class MovingObjectChild(BaseChild):
"""defines a child that produces an image of a moving block."""
def init(self, **kws):
# request some surface space from the parent, specify topleft and size.
self.surface = self.parent.reserve(child=self,
topleft=(0, 0), size=(100, 100))
self.events = Queue.Queue()
self.parent.listen(self, (KEYDOWN,))
def _redraw(self):
# return a surface to blit.
colour = random.choice([RED, BLUE, GREEN, WHITE])
self.surface.fill(colour)
return self.surface
from menuitem import MenuItem
class Menu(BaseChild):
"""Defines a menu object, Menus have a plain background and
a few 'buttons' and accept mouse and keyboard input."""
def init(self, **kws):
# request some surface space from the parent.
X, Y = SCREEN_SIZE
self.surface = self.parent.reserve(child=self,
topleft=(X/2 - 50, Y/2 - 50), size=(100, 100))
self.events = Queue.Queue()
self.parent.listen(self, (KEYDOWN, MOUSEBUTTONDOWN))
self.surface.fill(WHITE)
buttons = ['Play Game!', 'Exit Game!']
self.buttons = pygame.sprite.Group()
# set up the buttons.
play = MenuItem(parent=self, controller=self.controller,
text=buttons[0], containers=[self.buttons])
play.topleft = (20, 20)
quit_ = MenuItem(parent=self, controller=self.controller,
text=buttons[1], containers=[self.buttons])
quit_.topleft = (20, 60)
# bind button state to an event.
self.bind('button', self.on_play, play)
self.bind('button', self.on_quit, quit_)
def _on_play(self, event):
import powersim
Game = powersim.Game
child = self.parent.add_child(Game)
child.resume()
def on_play(self, event):
"""Show the user a level select Menu."""
from menu import LevelMenu
# import the levels.
import level
c = self.controller
levels = [
level.Level1(c),
level.Level2(c),
level.Level3(c)
]
child = self.parent.add_child(LevelMenu, levels=levels,
prev=self) # menus have a 'prev' to go back.
child.resume()
# be good and suspend myself.
self.suspend()
def on_quit(self, event):
print "quit!"
self.controller.send('exit', None)
def _loop(self, events):
# called per frame.
for event in events:
if event.type == MOUSEBUTTONDOWN:
pos = event.pos
# check to see if click was on us.
if not pos in self:
return
# localise pos to this child.
localpos = self.localise(pos)
# was click in a button?
for b in self.buttons:
b.click(localpos)
def _redraw(self):
# return a surface to blit.
self.buttons.update()
self.buttons.draw(self.surface)
return self.surface
if __name__ == '__main__':
c = controller.Controller()
c.setDaemon(True)
c.start()
m = Master()
# start a child.
kid3 = m.add_child(Menu)
kid3.resume()
# start the master.
m.mainloop()