fade
[sso] / shaken_and_spit_out / SSO.py
1 from os import listdir
2 from glob import glob
3 from random import choice
4
5 from pygame import cursors, PixelArray, Surface, Color
6 from pygame.mouse import set_cursor
7 from pygame.font import Font
8 from pygame.image import load
9 from pygame.locals import *
10
11 from shaken_and_spit_out.pgfw.Game import Game
12 from shaken_and_spit_out.pgfw.GameChild import GameChild
13
14 class SSO(Game):
15
16 def __init__(self):
17 Game.__init__(self)
18 set_cursor(*cursors.tri_left)
19 # string = u"\u2250\u2254\u2258"
20 # font = Font(self.get_resource("display", "font"), 24)
21 # self.message = font.render(string, True, (0, 255, 0))
22 self.backgrounds = [load(path) for path in \
23 glob(self.get_resource("image", "background") + \
24 "*.png")]
25 self.used_backgrounds = []
26 self.set_random_background()
27 self.subscribe(self.respond)
28 self.title.activate()
29
30 def set_children(self):
31 Game.set_children(self)
32 self.title = Title(self)
33
34 def set_random_background(self):
35 index = choice(list(set(range(len(self.backgrounds))).\
36 difference(self.used_backgrounds)))
37 self.used_backgrounds.append(index)
38 if len(self.used_backgrounds) > 3:
39 self.used_backgrounds.pop(0)
40 self.background = self.backgrounds[index]
41
42 def respond(self, event):
43 if self.delegate.compare(event, "reset-game"):
44 self.set_random_background()
45
46 def update(self):
47 self.get_screen().blit(self.background, (0, 0))
48 self.title.update()
49 # self.get_screen().fill((0, 0, 0))
50 # self.get_screen().blit(self.message, (100, 100))
51
52
53 class Title(GameChild):
54
55 def __init__(self, parent):
56 GameChild.__init__(self, parent)
57 self.delegate = self.get_delegate()
58 self.preface = Preface(self)
59 self.subscribe(self.respond)
60 self.subscribe(self.respond, MOUSEBUTTONDOWN)
61 self.deactivate()
62
63 def respond(self, event):
64 if self.delegate.compare(event, "reset-game"):
65 self.activate()
66 elif self.active:
67 if event.type == MOUSEBUTTONDOWN and event.button == 1:
68 print "left-click"
69
70 def deactivate(self):
71 self.active = True
72
73 def activate(self):
74 self.active = True
75 self.preface.reset()
76
77 def update(self):
78 if self.active:
79 self.preface.update()
80
81
82 class Preface(GameChild, Surface):
83
84 def __init__(self, parent):
85 GameChild.__init__(self, parent)
86 ds = self.ds = self.get_display_surface()
87 w, h = ds.get_width(), int(round(ds.get_width() / 1.618))
88 Surface.__init__(self, (w, h), SRCALPHA)
89 self.background = Surface((w, h))
90 self.background.fill((0, 0, 0))
91 mask = self.mask = Surface((w, h), SRCALPHA)
92 pixels = PixelArray(mask)
93 for y in xrange(h):
94 alpha = int(round(255 - (float(y) / h) * 255))
95 for x in xrange(w):
96 pixels[x][y] = Color(255, 255, 255, alpha)
97 del pixels
98
99 def reset(self):
100 pass
101
102 def update(self):
103 self.blit(self.background, (0, 0))
104 self.blit(self.mask, (0, 0), None, BLEND_RGBA_MIN)
105 self.ds.blit(self, (0, 0))