comparison gamelib/animal.py @ 243:4f86c2616cdf

Variable number of eggs for chickens
author Neil Muller <drnlmuller@gmail.com>
date Sat, 05 Sep 2009 12:05:08 +0000
parents 1a7000c8211c
children 844bfb23d4b6
comparison
equal deleted inserted replaced
242:23533f3ccd8a 243:4f86c2616cdf
131 def __init__(self, pos): 131 def __init__(self, pos):
132 image_left = imagecache.load_image('sprites/chkn.png') 132 image_left = imagecache.load_image('sprites/chkn.png')
133 image_right = imagecache.load_image('sprites/chkn.png', 133 image_right = imagecache.load_image('sprites/chkn.png',
134 ("right_facing",)) 134 ("right_facing",))
135 Animal.__init__(self, image_left, image_right, pos) 135 Animal.__init__(self, image_left, image_right, pos)
136 self.egg = None 136 self.eggs = []
137 self.egg_counter = 0
138 137
139 def move(self, gameboard): 138 def move(self, gameboard):
140 """A free chicken will move away from other free chickens""" 139 """A free chicken will move away from other free chickens"""
141 pass 140 pass
142 141
143 def lay(self): 142 def lay(self):
144 """See if the chicken lays an egg""" 143 """See if the chicken lays an egg"""
145 if not self.egg: 144 if not self.eggs:
146 self.egg = Egg(self.pos) 145 for x in range(random.randint(1, 4)):
146 self.eggs.append(Egg(self.pos))
147 self.equip(equipment.NestEgg()) 147 self.equip(equipment.NestEgg())
148 148
149 def remove_egg(self): 149 def remove_eggs(self):
150 """Clean up the egg state""" 150 """Clean up the egg state"""
151 self.egg = None 151 self.eggs = []
152 self.unequip_by_name("nestegg") 152 self.unequip_by_name("nestegg")
153 153
154 def hatch(self): 154 def remove_one_egg(self):
155 """Clean up the egg state"""
156 self.eggs.pop()
157 if not self.eggs:
158 self.unequip_by_name("nestegg")
159
160 def get_num_eggs(self):
161 return len(self.eggs)
162
163 def hatch(self, gameboard):
155 """See if we have an egg to hatch""" 164 """See if we have an egg to hatch"""
156 if self.egg: 165 if self.eggs:
157 chick = self.egg.hatch() 166 chick = self.eggs[0].hatch()
158 if chick: 167 if chick:
159 self.remove_egg() 168 # sell the remaining eggs
169 # Remove hatched egg
170 self.eggs.pop()
171 gameboard.eggs -= 1
172 # Sell other eggs
173 for egg in self.eggs[:]:
174 gameboard.sell_one_egg(self)
175 self.remove_eggs() # clean up stale images, etc.
160 return chick 176 return chick
161 return None 177 return None
162 178
163 def _find_killable_fox(self, weapon, gameboard): 179 def _find_killable_fox(self, weapon, gameboard):
164 """Choose a random fox within range of this weapon.""" 180 """Choose a random fox within range of this weapon."""
191 """An egg""" 207 """An egg"""
192 208
193 def __init__(self, pos): 209 def __init__(self, pos):
194 image = imagecache.load_image('sprites/egg.png') 210 image = imagecache.load_image('sprites/egg.png')
195 Animal.__init__(self, image, image, pos) 211 Animal.__init__(self, image, image, pos)
196 self.counter = 2 212 self.timer = 2
197 213
198 # Eggs don't move 214 # Eggs don't move
199 215
200 def hatch(self): 216 def hatch(self):
201 self.counter -= 1 217 self.timer -= 1
202 if self.counter == 0: 218 if self.timer == 0:
203 return Chicken(self.pos) 219 return Chicken(self.pos)
204 return None 220 return None
205 221
206 class Fox(Animal): 222 class Fox(Animal):
207 """A fox""" 223 """A fox"""