comparison gamelib/gameboard.py @ 118:2c76ed47fc44

Remove chicken respawn at day start. Handle eaten chickens in henhouses better
author Neil Muller <drnlmuller@gmail.com>
date Wed, 02 Sep 2009 20:28:03 +0000
parents d539ef5a3333
children eeb42296b523
comparison
equal deleted inserted replaced
117:f23ea2615b78 118:2c76ed47fc44
137 self.animal_to_place = None 137 self.animal_to_place = None
138 self.chickens = set() 138 self.chickens = set()
139 self.foxes = set() 139 self.foxes = set()
140 self.buildings = [] 140 self.buildings = []
141 self.cash = 0 141 self.cash = 0
142 self.eggs = 0
142 self.killed_foxes = 0 143 self.killed_foxes = 0
143 self.add_cash(constants.STARTING_CASH) 144 self.add_cash(constants.STARTING_CASH)
144 145
145 self.fix_buildings() 146 self.fix_buildings()
147
148 self.add_some_chickens()
146 149
147 def create_disp(self): 150 def create_disp(self):
148 width, height = pygame.display.get_surface().get_size() 151 width, height = pygame.display.get_surface().get_size()
149 tbl = gui.Table() 152 tbl = gui.Table()
150 tbl.tr() 153 tbl.tr()
232 print "Selected animal %r" % (self.animal_to_place,) 235 print "Selected animal %r" % (self.animal_to_place,)
233 return 236 return
234 building = self.get_building(tile_pos) 237 building = self.get_building(tile_pos)
235 if building: 238 if building:
236 # XXX: quick hack so egg loop triggers 239 # XXX: quick hack so egg loop triggers
237 building.add_occupant(self.animal_to_place) 240 if self.animal_to_place:
238 if self.animal_to_place in self.tv.sprites: 241 building.add_occupant(self.animal_to_place)
239 self.tv.sprites.remove(self.animal_to_place) 242 if self.animal_to_place in self.tv.sprites:
240 self.animal_to_place = None 243 self.tv.sprites.remove(self.animal_to_place)
241 self.open_building_dialog(building) 244 self.animal_to_place = None
245 self.open_building_dialog(building)
242 return 246 return
243 if self.tv.get(tile_pos) == self.GRASSLAND: 247 if self.tv.get(tile_pos) == self.GRASSLAND:
244 if self.animal_to_place is not None: 248 if self.animal_to_place is not None:
245 occupant = self.animal_to_place 249 occupant = self.animal_to_place
246 if occupant.abode is not None: 250 if occupant.abode is not None:
368 def add_building(self, building): 372 def add_building(self, building):
369 self.buildings.append(building) 373 self.buildings.append(building)
370 self.tv.sprites.append(building) 374 self.tv.sprites.append(building)
371 375
372 def lay_eggs(self): 376 def lay_eggs(self):
373 eggs = 0 377 self.eggs = 0
374 for building in self.buildings: 378 for building in self.buildings:
375 if building.NAME in [buildings.HenHouse.NAME]: 379 if building.NAME in [buildings.HenHouse.NAME]:
376 for chicken in building.occupants(): 380 for chicken in building.occupants():
377 print 'Laying check', chicken, chicken.egg, chicken.egg_counter 381 print 'Laying check', chicken, chicken.egg, chicken.egg_counter
378 chicken.lay() 382 chicken.lay()
379 if chicken.egg: 383 if chicken.egg:
380 eggs += 1 384 self.eggs += 1
381 self.toolbar.update_egg_counter(eggs) 385 self.toolbar.update_egg_counter(self.eggs)
382 386
383 def hatch_eggs(self): 387 def hatch_eggs(self):
384 eggs = 0
385 for building in self.buildings: 388 for building in self.buildings:
386 if building.NAME in [buildings.HenHouse.NAME]: 389 if building.NAME in [buildings.HenHouse.NAME]:
387 for chicken in building.occupants(): 390 for chicken in building.occupants():
388 print 'Checking', chicken, chicken.egg, chicken.egg_counter 391 print 'Checking', chicken, chicken.egg, chicken.egg_counter
389 new_chick = chicken.hatch() 392 new_chick = chicken.hatch()
390 if chicken.egg:
391 eggs += 1
392 if new_chick: 393 if new_chick:
394 self.eggs -= 1
393 print 'hatching chicken %s in %s ' % (chicken, building) 395 print 'hatching chicken %s in %s ' % (chicken, building)
394 building.add_occupant(new_chick) 396 building.add_occupant(new_chick)
395 self.add_chicken(new_chick) 397 self.add_chicken(new_chick)
396 self.toolbar.update_egg_counter(eggs) 398 self.toolbar.update_egg_counter(self.eggs)
397 399
398 def kill_fox(self, fox): 400 def kill_fox(self, fox):
399 if fox in self.foxes: 401 if fox in self.foxes:
400 self.killed_foxes += 1 402 self.killed_foxes += 1
401 self.toolbar.update_fox_counter(self.killed_foxes) 403 self.toolbar.update_fox_counter(self.killed_foxes)
407 if fox in self.tv.sprites: 409 if fox in self.tv.sprites:
408 self.tv.sprites.remove(fox) 410 self.tv.sprites.remove(fox)
409 411
410 def remove_chicken(self, chick): 412 def remove_chicken(self, chick):
411 self.chickens.discard(chick) 413 self.chickens.discard(chick)
414 if chick.egg:
415 self.eggs -= 1
416 self.toolbar.update_egg_counter(self.eggs)
417 if chick.abode:
418 chick.abode.remove_occupant(chick)
412 self.toolbar.update_chicken_counter(len(self.chickens)) 419 self.toolbar.update_chicken_counter(len(self.chickens))
413 if chick in self.tv.sprites: 420 if chick in self.tv.sprites:
414 if chick.outside(): 421 if chick.outside():
415 self.tv.sprites.remove(chick) 422 self.tv.sprites.remove(chick)
416 423
421 428
422 def add_cash(self, amount): 429 def add_cash(self, amount):
423 self.cash += amount 430 self.cash += amount
424 self.toolbar.update_cash_counter(self.cash) 431 self.toolbar.update_cash_counter(self.cash)
425 432
426 def update_chickens(self): 433 def add_some_chickens(self):
427 """Update the chickens state at the start of the new day""" 434 """Add some random chickens to start the game"""
428 # Currently random chickens appear
429 # Very simple, we walk around the tilemap, and, for each farm tile,
430 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
431 # or we run out of board
432 x, y = 0, 0 435 x, y = 0, 0
433 width, height = self.tv.size 436 width, height = self.tv.size
434 while len(self.chickens) < 5: 437 while len(self.chickens) < 10:
435 if x < width: 438 if x < width:
436 tile = self.tv.get((x, y)) 439 tile = self.tv.get((x, y))
437 else: 440 else:
438 y += 1 441 y += 1
439 if y >= height: 442 if y >= height: