Mercurial > rinkhals
annotate gamelib/animations.py @ 237:4a87bfa5cc63
Twiddle animation cycle
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 09:48:48 +0000 |
parents | d0760fccce14 |
children | 1a7000c8211c |
rev | line source |
---|---|
202 | 1 """Animation Loops""" |
2 | |
3 from pgu.vid import Sprite | |
4 | |
5 import imagecache | |
6 from misc import Position | |
7 | |
8 class Animation(Sprite): | |
9 """Animation loop. | |
10 | |
11 These are derived from sprites, since they behave similiary in most | |
12 respects, but, to ensure draw ordering, we don't add them to | |
13 the sprites list. | |
14 | |
15 Ideally, animations should be quite short.""" | |
237 | 16 # In the current implementation, sequences longer than 4 frames |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
202
diff
changeset
|
17 # will cause issues as this will overrun the next move loop. |
237 | 18 # (assuming all animations are triggered by the move loop, of course) |
202 | 19 |
20 def __init__(self, sequence, tile_pos): | |
21 # Create the first frame | |
22 self.iter = iter(sequence) | |
23 Sprite.__init__(self, self.iter.next(), (-1000, -1000)) | |
24 if hasattr(tile_pos, 'to_tuple'): | |
25 self.pos = tile_pos | |
26 else: | |
27 self.pos = Position(tile_pos[0], tile_pos[1]) | |
28 self.removed = False | |
29 | |
30 def fix_pos(self, tv): | |
31 ppos = tv.tile_to_view(self.pos.to_tuple()) | |
32 self.rect.x = ppos[0] | |
33 self.rect.y = ppos[1] | |
34 | |
35 def animate(self): | |
36 """Step to the next frame. | |
37 | |
38 Set removed flag when we hit the end of the sequence""" | |
39 try: | |
40 self.setimage(self.iter.next()) | |
41 except StopIteration: | |
42 self.removed = True | |
43 | |
44 class MuzzleFlash(Animation): | |
45 | |
235
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
46 FLASH_LEFT = imagecache.load_image('sprites/muzzle_flash.png') |
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
47 FLASH_RIGHT = imagecache.load_image('sprites/muzzle_flash.png', |
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
48 ("right_facing",)) |
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
49 |
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
50 SEQUENCE_LEFT = [FLASH_LEFT, FLASH_LEFT] |
d0760fccce14
Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents:
233
diff
changeset
|
51 SEQUENCE_RIGHT = [FLASH_RIGHT, FLASH_RIGHT] |
202 | 52 |
53 def __init__(self, chicken): | |
54 if chicken.facing == 'right': | |
55 Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos) | |
56 else: | |
57 Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos) | |
58 |