comparison gamelib/animal.py @ 171:9ea53eb919cf

Make Animals accept Positions when created. move hatch watching from Chicken to Egg
author Neil Muller <drnlmuller@gmail.com>
date Thu, 03 Sep 2009 23:00:29 +0000
parents 0d6e23dcd3af
children ff168162974e
comparison
equal deleted inserted replaced
170:92d11e0544bc 171:9ea53eb919cf
21 Sprite.__init__(self, image_left, (-1000, -1000)) 21 Sprite.__init__(self, image_left, (-1000, -1000))
22 self._image_left = image_left 22 self._image_left = image_left
23 self.image_left = image_left.copy() 23 self.image_left = image_left.copy()
24 self._image_right = image_right 24 self._image_right = image_right
25 self.image_right = image_right.copy() 25 self.image_right = image_right.copy()
26 self.pos = Position(tile_pos[0], tile_pos[1]) 26 if hasattr(tile_pos, 'to_tuple'):
27 self.pos = tile_pos
28 else:
29 self.pos = Position(tile_pos[0], tile_pos[1])
27 self.equipment = [] 30 self.equipment = []
28 self.abode = None 31 self.abode = None
29 self.facing = 'left' 32 self.facing = 'left'
30 self.lives = 1 33 self.lives = 1
31 34
103 def __init__(self, pos): 106 def __init__(self, pos):
104 image_left = imagecache.load_image('sprites/chkn.png') 107 image_left = imagecache.load_image('sprites/chkn.png')
105 image_right = imagecache.load_image('sprites/chkn.png', 108 image_right = imagecache.load_image('sprites/chkn.png',
106 ("right_facing",)) 109 ("right_facing",))
107 Animal.__init__(self, image_left, image_right, pos) 110 Animal.__init__(self, image_left, image_right, pos)
108 self.egg = False 111 self.egg = None
109 self.egg_counter = 0 112 self.egg_counter = 0
110 113
111 def move(self, gameboard): 114 def move(self, gameboard):
112 """A free chicken will move away from other free chickens""" 115 """A free chicken will move away from other free chickens"""
113 pass 116 pass
114 117
115 def lay(self): 118 def lay(self):
116 """See if the chicken lays an egg""" 119 """See if the chicken lays an egg"""
117 if not self.egg: 120 if not self.egg:
118 self.egg = True 121 self.egg = Egg(self.pos)
119 self.egg_counter = 2
120 122
121 def hatch(self): 123 def hatch(self):
122 """See if we have an egg to hatch""" 124 """See if we have an egg to hatch"""
123 if self.egg: 125 if self.egg:
124 self.egg_counter -= 1 126 return self.egg.hatch()
125 if self.egg_counter == 0:
126 # Egg hatches
127 self.egg = False
128 return Chicken(self.pos.to_tuple())
129 return None 127 return None
130 128
131 def _find_killable_fox(self, weapon, gameboard): 129 def _find_killable_fox(self, weapon, gameboard):
132 """Choose a random fox within range of this weapon.""" 130 """Choose a random fox within range of this weapon."""
133 killable_foxes = [] 131 killable_foxes = []
158 """An egg""" 156 """An egg"""
159 157
160 def __init__(self, pos): 158 def __init__(self, pos):
161 image = imagecache.load_image('sprites/egg.png') 159 image = imagecache.load_image('sprites/egg.png')
162 Animal.__init__(self, image, image, pos) 160 Animal.__init__(self, image, image, pos)
161 self.counter = 2
163 162
164 # Eggs don't move 163 # Eggs don't move
164
165 def hatch(self):
166 self.counter -= 1
167 if self.counter == 0:
168 return Chicken(self.pos)
169 return None
165 170
166 class Fox(Animal): 171 class Fox(Animal):
167 """A fox""" 172 """A fox"""
168 173
169 STEALTH = 20 174 STEALTH = 20