annotate nagslang/mutators.py @ 56:b9430b4a48da

Now with a werewolf
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 01 Sep 2013 18:51:06 +0200
parents c62ed518e5c8
children 72a91d64c088
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
1 '''Mutations to apply to images'''
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
2
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
3 import pygame
56
b9430b4a48da Now with a werewolf
Stefano Rivera <stefano@rivera.za.net>
parents: 31
diff changeset
4 from pygame.transform import rotate, flip
24
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
5
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
6
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
7 class Mutator(object):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
8 def __init__(self, func, *args):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
9 self._func = func
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
10 self._args = tuple(args)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
11
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
12 def __call__(self, image):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
13 return self._func(image, *self._args)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
14
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
15 def __hash__(self):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
16 return hash((self._func, self._args))
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
17
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
18 def __eq__(self, other):
31
c62ed518e5c8 Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
19 if not isinstance(other, Mutator):
c62ed518e5c8 Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
20 return NotImplemented
24
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
21 return (self._func is other._func) and (self._args == other._args)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
22
31
c62ed518e5c8 Fix repor and add type-check to __eq__.
Simon Cross <hodgestar@gmail.com>
parents: 24
diff changeset
23 def __repr__(self):
24
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
24 return '<%s %r>' % (self.__class__.__name__, self._args)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
25
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
26
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
27 class Colour(Mutator):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
28 '''Overlay a colour onto an image'''
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
29 def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
30 super(Colour, self).__init__(Colour.colour, colour, blend)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
31
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
32 @classmethod
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
33 def colour(self, image, colour, blend):
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
34 image = image.copy()
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
35 overlay = pygame.surface.Surface(image.get_size(),
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
36 pygame.locals.SRCALPHA, image)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
37 overlay.fill(colour)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
38 image.blit(overlay, (0, 0), None, blend)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
39 return image
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
40
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
41
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
42 # Identity mutator
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
43 NULL = Mutator(lambda x: x)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
44
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
45 # Rotation
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
46 R90 = Mutator(rotate, 90)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
47 R180 = Mutator(rotate, 180)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
48 R270 = Mutator(rotate, -90)
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
49
56
b9430b4a48da Now with a werewolf
Stefano Rivera <stefano@rivera.za.net>
parents: 31
diff changeset
50 FLIP_H = Mutator(flip, True, False)
b9430b4a48da Now with a werewolf
Stefano Rivera <stefano@rivera.za.net>
parents: 31
diff changeset
51 FLIP_V = Mutator(flip, False, True)
b9430b4a48da Now with a werewolf
Stefano Rivera <stefano@rivera.za.net>
parents: 31
diff changeset
52
24
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
53 # Colour
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
54 RED = Colour((255, 0, 0))
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
55 GREEN = Colour((0, 255, 0))
50babb330261 Forgot to add mutators...
Stefano Rivera <stefano@rivera.za.net>
parents:
diff changeset
56 BLUE = Colour((0, 0, 255))