Mercurial > rinkhals
comparison gamelib/gameboard.py @ 116:d539ef5a3333
Add basic chicken->egg cycle
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 20:04:47 +0000 |
parents | 2b2007e231da |
children | 2c76ed47fc44 |
comparison
equal
deleted
inserted
replaced
115:2b2007e231da | 116:d539ef5a3333 |
---|---|
36 def __init__(self, gameboard, **params): | 36 def __init__(self, gameboard, **params): |
37 gui.Table.__init__(self, **params) | 37 gui.Table.__init__(self, **params) |
38 self.gameboard = gameboard | 38 self.gameboard = gameboard |
39 self.cash_counter = mklabel() | 39 self.cash_counter = mklabel() |
40 self.chicken_counter = mklabel() | 40 self.chicken_counter = mklabel() |
41 self.egg_counter = mklabel() | |
41 self.killed_foxes = mklabel() | 42 self.killed_foxes = mklabel() |
42 self.rifle_counter = mklabel() | 43 self.rifle_counter = mklabel() |
43 | 44 |
44 self.add_counter(mklabel("Groats:"), self.cash_counter) | 45 self.add_counter(mklabel("Groats:"), self.cash_counter) |
46 self.add_counter(mklabel("Eggs:"), self.egg_counter) | |
45 self.add_counter(icons.CHKN_ICON, self.chicken_counter) | 47 self.add_counter(icons.CHKN_ICON, self.chicken_counter) |
46 self.add_counter(icons.KILLED_FOX, self.killed_foxes) | 48 self.add_counter(icons.KILLED_FOX, self.killed_foxes) |
47 | 49 |
48 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) | 50 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) |
49 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) | 51 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) |
62 pygame.event.post(engine.START_NIGHT) | 64 pygame.event.post(engine.START_NIGHT) |
63 | 65 |
64 update_cash_counter = mkcountupdate('cash_counter') | 66 update_cash_counter = mkcountupdate('cash_counter') |
65 update_fox_counter = mkcountupdate('killed_foxes') | 67 update_fox_counter = mkcountupdate('killed_foxes') |
66 update_chicken_counter = mkcountupdate('chicken_counter') | 68 update_chicken_counter = mkcountupdate('chicken_counter') |
69 update_egg_counter = mkcountupdate('egg_counter') | |
67 | 70 |
68 def add_spacer(self, height=30): | 71 def add_spacer(self, height=30): |
69 self.tr() | 72 self.tr() |
70 self.add(gui.Spacer(0, height)) | 73 self.add(gui.Spacer(0, height)) |
71 | 74 |
130 self.tv.tga_load_level(data.filepath('level1.tga')) | 133 self.tv.tga_load_level(data.filepath('level1.tga')) |
131 self.create_disp() | 134 self.create_disp() |
132 | 135 |
133 self.selected_tool = None | 136 self.selected_tool = None |
134 self.animal_to_place = None | 137 self.animal_to_place = None |
135 self.chickens = [] | 138 self.chickens = set() |
136 self.foxes = [] | 139 self.foxes = set() |
137 self.buildings = [] | 140 self.buildings = [] |
138 self.cash = 0 | 141 self.cash = 0 |
139 self.killed_foxes = 0 | 142 self.killed_foxes = 0 |
140 self.add_cash(constants.STARTING_CASH) | 143 self.add_cash(constants.STARTING_CASH) |
141 | 144 |
228 self.animal_to_place = chicken | 231 self.animal_to_place = chicken |
229 print "Selected animal %r" % (self.animal_to_place,) | 232 print "Selected animal %r" % (self.animal_to_place,) |
230 return | 233 return |
231 building = self.get_building(tile_pos) | 234 building = self.get_building(tile_pos) |
232 if building: | 235 if building: |
236 # XXX: quick hack so egg loop triggers | |
237 building.add_occupant(self.animal_to_place) | |
238 if self.animal_to_place in self.tv.sprites: | |
239 self.tv.sprites.remove(self.animal_to_place) | |
240 self.animal_to_place = None | |
233 self.open_building_dialog(building) | 241 self.open_building_dialog(building) |
234 return | 242 return |
235 if self.tv.get(tile_pos) == self.GRASSLAND: | 243 if self.tv.get(tile_pos) == self.GRASSLAND: |
236 if self.animal_to_place is not None: | 244 if self.animal_to_place is not None: |
237 occupant = self.animal_to_place | 245 occupant = self.animal_to_place |
329 self.tvw.move_view(self.TILE_DIMENSIONS[0], 0) | 337 self.tvw.move_view(self.TILE_DIMENSIONS[0], 0) |
330 else: | 338 else: |
331 self.disp.event(e) | 339 self.disp.event(e) |
332 | 340 |
333 def clear_foxes(self): | 341 def clear_foxes(self): |
334 for fox in self.foxes[:]: | 342 for fox in self.foxes.copy(): |
335 # Any foxes that didn't make it to the woods are automatically | 343 # Any foxes that didn't make it to the woods are automatically |
336 # killed | 344 # killed |
337 if self.in_bounds(fox.pos) and self.tv.get(fox.pos.to_tuple()) \ | 345 if self.in_bounds(fox.pos) and self.tv.get(fox.pos.to_tuple()) \ |
338 != self.WOODLAND: | 346 != self.WOODLAND: |
339 self.kill_fox(fox) | 347 self.kill_fox(fox) |
340 else: | 348 else: |
341 self.tv.sprites.remove(fox) | 349 self.tv.sprites.remove(fox) |
342 self.foxes = [] # Remove all the foxes | 350 self.foxes = set() # Remove all the foxes |
343 | 351 |
344 def move_foxes(self): | 352 def move_foxes(self): |
345 for fox in self.foxes: | 353 for fox in self.foxes: |
346 fox.move(self) | 354 fox.move(self) |
347 for chicken in self.chickens: | 355 for chicken in self.chickens: |
348 chicken.attack(self) | 356 chicken.attack(self) |
349 | 357 |
350 def add_chicken(self, chicken): | 358 def add_chicken(self, chicken): |
351 self.chickens.append(chicken) | 359 self.chickens.add(chicken) |
352 if chicken.outside(): | 360 if chicken.outside(): |
353 self.tv.sprites.append(chicken) | 361 self.tv.sprites.append(chicken) |
354 self.toolbar.update_chicken_counter(len(self.chickens)) | 362 self.toolbar.update_chicken_counter(len(self.chickens)) |
355 | 363 |
356 def add_fox(self, fox): | 364 def add_fox(self, fox): |
357 self.foxes.append(fox) | 365 self.foxes.add(fox) |
358 self.tv.sprites.append(fox) | 366 self.tv.sprites.append(fox) |
359 | 367 |
360 def add_building(self, building): | 368 def add_building(self, building): |
361 self.buildings.append(building) | 369 self.buildings.append(building) |
362 self.tv.sprites.append(building) | 370 self.tv.sprites.append(building) |
371 | |
372 def lay_eggs(self): | |
373 eggs = 0 | |
374 for building in self.buildings: | |
375 if building.NAME in [buildings.HenHouse.NAME]: | |
376 for chicken in building.occupants(): | |
377 print 'Laying check', chicken, chicken.egg, chicken.egg_counter | |
378 chicken.lay() | |
379 if chicken.egg: | |
380 eggs += 1 | |
381 self.toolbar.update_egg_counter(eggs) | |
382 | |
383 def hatch_eggs(self): | |
384 eggs = 0 | |
385 for building in self.buildings: | |
386 if building.NAME in [buildings.HenHouse.NAME]: | |
387 for chicken in building.occupants(): | |
388 print 'Checking', chicken, chicken.egg, chicken.egg_counter | |
389 new_chick = chicken.hatch() | |
390 if chicken.egg: | |
391 eggs += 1 | |
392 if new_chick: | |
393 print 'hatching chicken %s in %s ' % (chicken, building) | |
394 building.add_occupant(new_chick) | |
395 self.add_chicken(new_chick) | |
396 self.toolbar.update_egg_counter(eggs) | |
363 | 397 |
364 def kill_fox(self, fox): | 398 def kill_fox(self, fox): |
365 if fox in self.foxes: | 399 if fox in self.foxes: |
366 self.killed_foxes += 1 | 400 self.killed_foxes += 1 |
367 self.toolbar.update_fox_counter(self.killed_foxes) | 401 self.toolbar.update_fox_counter(self.killed_foxes) |
368 self.add_cash(constants.SELL_PRICE_DEAD_FOX) | 402 self.add_cash(constants.SELL_PRICE_DEAD_FOX) |
369 self.remove_fox(fox) | 403 self.remove_fox(fox) |
370 | 404 |
371 def remove_fox(self, fox): | 405 def remove_fox(self, fox): |
372 if fox in self.foxes: | 406 self.foxes.discard(fox) |
373 self.foxes.remove(fox) | 407 if fox in self.tv.sprites: |
374 self.tv.sprites.remove(fox) | 408 self.tv.sprites.remove(fox) |
375 | 409 |
376 def remove_chicken(self, chick): | 410 def remove_chicken(self, chick): |
377 if chick in self.chickens: | 411 self.chickens.discard(chick) |
378 self.chickens.remove(chick) | 412 self.toolbar.update_chicken_counter(len(self.chickens)) |
413 if chick in self.tv.sprites: | |
379 if chick.outside(): | 414 if chick.outside(): |
380 self.tv.sprites.remove(chick) | 415 self.tv.sprites.remove(chick) |
381 self.toolbar.update_chicken_counter(len(self.chickens)) | |
382 | 416 |
383 def remove_building(self, building): | 417 def remove_building(self, building): |
384 if building in self.buildings: | 418 if building in self.buildings: |
385 self.buildings.remove(building) | 419 self.buildings.remove(building) |
386 self.tv.sprites.remove(building) | 420 self.tv.sprites.remove(building) |