annotate gamelib/animations.py @ 236:9a6ac9c9ff46

chickens turn to face target foxes
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Sat, 05 Sep 2009 09:46:29 +0000
parents d0760fccce14
children 4a87bfa5cc63
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."""
233
d3d5352f5853 Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents: 202
diff changeset
16 # In the current implementation, sequences longer than 3 frames
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.
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 def __init__(self, sequence, tile_pos):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 # Create the first frame
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 self.iter = iter(sequence)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 Sprite.__init__(self, self.iter.next(), (-1000, -1000))
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 if hasattr(tile_pos, 'to_tuple'):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 self.pos = tile_pos
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 self.pos = Position(tile_pos[0], tile_pos[1])
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
27 self.removed = False
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 def fix_pos(self, tv):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30 ppos = tv.tile_to_view(self.pos.to_tuple())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 self.rect.x = ppos[0]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 self.rect.y = ppos[1]
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34 def animate(self):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 """Step to the next frame.
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 Set removed flag when we hit the end of the sequence"""
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 try:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 self.setimage(self.iter.next())
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 except StopIteration:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 self.removed = True
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 class MuzzleFlash(Animation):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44
235
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
45 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
46 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
47 ("right_facing",))
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
48
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
49 SEQUENCE_LEFT = [FLASH_LEFT, FLASH_LEFT]
d0760fccce14 Hold flash for a bit longer
Neil Muller <drnlmuller@gmail.com>
parents: 233
diff changeset
50 SEQUENCE_RIGHT = [FLASH_RIGHT, FLASH_RIGHT]
202
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 def __init__(self, chicken):
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 if chicken.facing == 'right':
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 Animation.__init__(self, self.SEQUENCE_RIGHT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 else:
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 Animation.__init__(self, self.SEQUENCE_LEFT, chicken.pos)
3074784c93f4 Animation support
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57