annotate gamelib/animations.py @ 231:857040b211d5

Note why we're disabling a warning.
author Simon Cross <hodgestar@gmail.com>
date Fri, 04 Sep 2009 23:59:28 +0000
parents 3074784c93f4
children d3d5352f5853
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Animation Loops"""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pgu.vid import Sprite
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5 import imagecache
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from misc import Position
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8 class Animation(Sprite):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 """Animation loop.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 These are derived from sprites, since they behave similiary in most
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12 respects, but, to ensure draw ordering, we don't add them to
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 the sprites list.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 Ideally, animations should be quite short."""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 def __init__(self, sequence, tile_pos):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18 # Create the first frame
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 self.iter = iter(sequence)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 Sprite.__init__(self, self.iter.next(), (-1000, -1000))
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 if hasattr(tile_pos, 'to_tuple'):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 self.pos = tile_pos
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 self.pos = Position(tile_pos[0], tile_pos[1])
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 self.removed = False
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 def fix_pos(self, tv):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 ppos = tv.tile_to_view(self.pos.to_tuple())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 self.rect.x = ppos[0]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30 self.rect.y = ppos[1]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 def animate(self):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33 """Step to the next frame.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 Set removed flag when we hit the end of the sequence"""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36 try:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 self.setimage(self.iter.next())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 except StopIteration:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 self.removed = True
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 class MuzzleFlash(Animation):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 SEQUENCE_LEFT = [imagecache.load_image('sprites/muzzle_flash.png')]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 SEQUENCE_RIGHT = [imagecache.load_image('sprites/muzzle_flash.png',
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 ("right_facing",))]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47 def __init__(self, chicken):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 if chicken.facing == 'right':
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52