Mercurial > rinkhals
comparison gamelib/animal.py @ 53:f20dd3dcb118
foxes don't run backwards
author | Adrianna Pińska <adrianna.pinska@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 17:47:10 +0000 |
parents | be2496df2368 |
children | d92a2f973cc4 |
comparison
equal
deleted
inserted
replaced
52:0d4799866bcf | 53:f20dd3dcb118 |
---|---|
5 import imagecache | 5 import imagecache |
6 | 6 |
7 class Animal(Sprite): | 7 class Animal(Sprite): |
8 """Base class for animals""" | 8 """Base class for animals""" |
9 | 9 |
10 def __init__(self, image, pos): | 10 def __init__(self, image_left, image_right, pos): |
11 # Create the animal somewhere far off screen | 11 # Create the animal somewhere far off screen |
12 Sprite.__init__(self, image, (-1000, -1000)) | 12 Sprite.__init__(self, image_left, (-1000, -1000)) |
13 self.image_left = image_left | |
14 self.image_right = image_right | |
13 self.pos = pos | 15 self.pos = pos |
14 | 16 |
15 def loop(self, tv, _sprite): | 17 def loop(self, tv, _sprite): |
16 ppos = tv.tile_to_view(self.pos) | 18 ppos = tv.tile_to_view(self.pos) |
17 self.rect.x = ppos[0] | 19 self.rect.x = ppos[0] |
24 | 26 |
25 class Chicken(Animal): | 27 class Chicken(Animal): |
26 """A chicken""" | 28 """A chicken""" |
27 | 29 |
28 def __init__(self, pos): | 30 def __init__(self, pos): |
29 image = imagecache.load_image('sprites/chkn.png') | 31 image_left = imagecache.load_image('sprites/chkn.png') |
30 Animal.__init__(self, image, pos) | 32 image_right = imagecache.load_image('sprites/chkn.png', ("right_facing",)) |
33 Animal.__init__(self, image_left, image_right, pos) | |
31 | 34 |
32 def move(self, gameboard): | 35 def move(self, gameboard): |
33 """A free chicken will move away from other free chickens""" | 36 """A free chicken will move away from other free chickens""" |
34 return self.pos | 37 return self.pos |
35 | 38 |
36 class Egg(Animal): | 39 class Egg(Animal): |
37 """An egg""" | 40 """An egg""" |
38 | 41 |
39 def __init__(self, pos): | 42 def __init__(self, pos): |
40 image = imagecache.load_image('sprites/egg.png') | 43 image = imagecache.load_image('sprites/egg.png') |
41 Animal.__init__(self, image, pos) | 44 Animal.__init__(self, image, image, pos) |
42 | 45 |
43 # Eggs don't move | 46 # Eggs don't move |
44 | 47 |
45 class Fox(Animal): | 48 class Fox(Animal): |
46 """A fox""" | 49 """A fox""" |
47 | 50 |
48 def __init__(self, pos): | 51 def __init__(self, pos): |
49 image = imagecache.load_image('sprites/fox.png') | 52 image_left = imagecache.load_image('sprites/fox.png') |
53 image_right = imagecache.load_image('sprites/fox.png', ("right_facing",)) | |
50 self.full = False | 54 self.full = False |
51 Animal.__init__(self, image, pos) | 55 Animal.__init__(self, image_left, image_right, pos) |
52 | 56 |
53 def move(self, gameboard): | 57 def move(self, gameboard): |
54 """Foxes will aim to move towards the closest henhouse or free | 58 """Foxes will aim to move towards the closest henhouse or free |
55 chicken""" | 59 chicken""" |
56 if self.full: | 60 if self.full: |
67 min_vec = vec | 71 min_vec = vec |
68 closest = chicken | 72 closest = chicken |
69 xpos, ypos = self.pos | 73 xpos, ypos = self.pos |
70 if min_vec[0] < 0: | 74 if min_vec[0] < 0: |
71 xpos -= 1 | 75 xpos -= 1 |
76 self.setimage(self.image_left) | |
72 elif min_vec[0] > 0: | 77 elif min_vec[0] > 0: |
73 xpos += 1 | 78 xpos += 1 |
79 self.setimage(self.image_right) | |
74 if min_vec[1] < 0: | 80 if min_vec[1] < 0: |
75 ypos -= 1 | 81 ypos -= 1 |
76 elif min_vec[1] > 0: | 82 elif min_vec[1] > 0: |
77 ypos += 1 | 83 ypos += 1 |
78 if closest.pos == self.pos: | 84 if closest.pos == self.pos: |