comparison gamelib/gameboard.py @ 389:463802281182

Add basic level support (level choosing needs work)
author Neil Muller <drnlmuller@gmail.com>
date Thu, 29 Oct 2009 20:55:37 +0000
parents e89e6ad011ac
children bb75979b58e6
comparison
equal deleted inserted replaced
388:c6f0e3e72e86 389:463802281182
56 getattr(self, counter).update_value("%s " % value) 56 getattr(self, counter).update_value("%s " % value)
57 self.repaint() 57 self.repaint()
58 return update_counter 58 return update_counter
59 59
60 class ToolBar(gui.Table): 60 class ToolBar(gui.Table):
61 def __init__(self, gameboard, **params): 61 def __init__(self, gameboard, level, **params):
62 gui.Table.__init__(self, **params) 62 gui.Table.__init__(self, **params)
63 self.group = gui.Group(name='toolbar', value=None) 63 self.group = gui.Group(name='toolbar', value=None)
64 self._next_tool_value = 0 64 self._next_tool_value = 0
65 self.gameboard = gameboard 65 self.gameboard = gameboard
66 self.cash_counter = mklabel(align=1) 66 self.cash_counter = mklabel(align=1)
85 constants.LOGGING_PRICE, cursors.cursors['ball']) 85 constants.LOGGING_PRICE, cursors.cursors['ball'])
86 self.add_spacer(5) 86 self.add_spacer(5)
87 87
88 self.add_heading("Sell ...") 88 self.add_heading("Sell ...")
89 self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN, 89 self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
90 constants.SELL_PRICE_CHICKEN, cursors.cursors['sell']) 90 level.sell_price_chicken, cursors.cursors['sell'])
91 self.add_tool_button("Egg", constants.TOOL_SELL_EGG, 91 self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
92 constants.SELL_PRICE_EGG, cursors.cursors['sell']) 92 level.sell_price_egg, cursors.cursors['sell'])
93 self.add_tool_button("Building", constants.TOOL_SELL_BUILDING, 93 self.add_tool_button("Building", constants.TOOL_SELL_BUILDING,
94 None, cursors.cursors['sell']) 94 None, cursors.cursors['sell'])
95 self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT, 95 self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
96 None, cursors.cursors['sell']) 96 None, cursors.cursors['sell'])
97 self.add_spacer(5) 97 self.add_spacer(5)
273 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] 273 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
274 FENCE = tiles.REVERSE_TILE_MAP['fence'] 274 FENCE = tiles.REVERSE_TILE_MAP['fence']
275 WOODLAND = tiles.REVERSE_TILE_MAP['woodland'] 275 WOODLAND = tiles.REVERSE_TILE_MAP['woodland']
276 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence'] 276 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
277 277
278 # These don't have to add up to 100, but it's easier to think 278 def __init__(self, main_app, level):
279 # about them if they do.
280 FOX_WEIGHTINGS = (
281 (animal.Fox, 59),
282 (animal.GreedyFox, 30),
283 (animal.NinjaFox, 5),
284 (animal.DemoFox, 5),
285 (animal.Rinkhals, 1),
286 )
287
288 def __init__(self, main_app, max_turns):
289 self.disp = main_app 279 self.disp = main_app
280 self.level = level
290 self.tv = tiles.FarmVid() 281 self.tv = tiles.FarmVid()
291 self.tv.png_folder_load_tiles('tiles') 282 self.tv.png_folder_load_tiles('tiles')
292 self.tv.tga_load_level(data.filepath('levels/farm.tga')) 283 self.tv.tga_load_level(level.map)
293 width, height = self.tv.size 284 width, height = self.tv.size
294 # Ensure we don't every try to create more foxes then is sane 285 # Ensure we don't every try to create more foxes then is sane
295 self.max_foxes = min(height+width-15, constants.ABS_MAX_NUM_FOXES) 286 self.max_foxes = min(height+width-15, level.max_foxes)
296 self.max_turns = max_turns
297 self.create_display() 287 self.create_display()
298 288
299 self.selected_tool = None 289 self.selected_tool = None
300 self.animal_to_place = None 290 self.animal_to_place = None
301 self.sprite_cursor = None 291 self.sprite_cursor = None
304 self.buildings = [] 294 self.buildings = []
305 self.cash = 0 295 self.cash = 0
306 self.eggs = 0 296 self.eggs = 0
307 self.days = 0 297 self.days = 0
308 self.killed_foxes = 0 298 self.killed_foxes = 0
309 self.add_cash(constants.STARTING_CASH) 299 self.add_cash(level.starting_cash)
310 self.day, self.night = True, False 300 self.day, self.night = True, False
311 301
312 self.fix_buildings() 302 self.fix_buildings()
313 303
314 cdata = { 304 cdata = {
322 312
323 def create_display(self): 313 def create_display(self):
324 width, height = self.disp.rect.w, self.disp.rect.h 314 width, height = self.disp.rect.w, self.disp.rect.h
325 tbl = gui.Table() 315 tbl = gui.Table()
326 tbl.tr() 316 tbl.tr()
327 self.toolbar = ToolBar(self, width=self.TOOLBAR_WIDTH) 317 self.toolbar = ToolBar(self, self.level, width=self.TOOLBAR_WIDTH)
328 tbl.td(self.toolbar, valign=-1) 318 tbl.td(self.toolbar, valign=-1)
329 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height) 319 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height)
330 tbl.td(self.tvw) 320 tbl.td(self.tvw)
331 self.top_widget = tbl 321 self.top_widget = tbl
332 322
441 TextDialog("Squuaaawwwwwk!", msg).open() 431 TextDialog("Squuaaawwwwwk!", msg).open()
442 return False 432 return False
443 for item in list(chicken.equipment): 433 for item in list(chicken.equipment):
444 self.add_cash(item.sell_price()) 434 self.add_cash(item.sell_price())
445 chicken.unequip(item) 435 chicken.unequip(item)
446 self.add_cash(constants.SELL_PRICE_CHICKEN) 436 self.add_cash(self.level.sell_price_chicken)
447 sound.play_sound("sell-chicken.ogg") 437 sound.play_sound("sell-chicken.ogg")
448 if update_button: 438 if update_button:
449 update_button(chicken, empty=True) 439 update_button(chicken, empty=True)
450 self.remove_chicken(chicken) 440 self.remove_chicken(chicken)
451 return True 441 return True
458 return 448 return
459 do_sell(chick) 449 do_sell(chick)
460 450
461 def sell_one_egg(self, chicken): 451 def sell_one_egg(self, chicken):
462 if chicken.eggs: 452 if chicken.eggs:
463 self.add_cash(constants.SELL_PRICE_EGG) 453 self.add_cash(self.level.sell_price_egg)
464 chicken.remove_one_egg() 454 chicken.remove_one_egg()
465 self.eggs -= 1 455 self.eggs -= 1
466 self.toolbar.update_egg_counter(self.eggs) 456 self.toolbar.update_egg_counter(self.eggs)
467 return True 457 return True
468 return False 458 return False
775 return True 765 return True
776 return False 766 return False
777 767
778 def advance_day(self): 768 def advance_day(self):
779 self.days += 1 769 self.days += 1
780 if self.days == self.max_turns: 770 if self.days == self.level.turn_limit:
781 self.toolbar.day_counter.style.color = (255, 0, 0) 771 self.toolbar.day_counter.style.color = (255, 0, 0)
782 self.toolbar.update_day_counter("%s/%s" % (self.days, 772 self.toolbar.update_day_counter("%s/%s" % (self.days,
783 self.max_turns if self.max_turns > 0 else "-")) 773 self.level.turn_limit if self.level.turn_limit > 0 else "-"))
784 774
785 def clear_foxes(self): 775 def clear_foxes(self):
786 for fox in self.foxes.copy(): 776 for fox in self.foxes.copy():
787 # Any foxes that didn't make it to the woods are automatically 777 # Any foxes that didn't make it to the woods are automatically
788 # killed 778 # killed
862 self.toolbar.update_egg_counter(self.eggs) 852 self.toolbar.update_egg_counter(self.eggs)
863 853
864 def kill_fox(self, fox): 854 def kill_fox(self, fox):
865 self.killed_foxes += 1 855 self.killed_foxes += 1
866 self.toolbar.update_fox_counter(self.killed_foxes) 856 self.toolbar.update_fox_counter(self.killed_foxes)
867 self.add_cash(constants.SELL_PRICE_DEAD_FOX) 857 self.add_cash(self.level.sell_price_dead_fox)
868 self.remove_fox(fox) 858 self.remove_fox(fox)
869 859
870 def remove_fox(self, fox): 860 def remove_fox(self, fox):
871 self.foxes.discard(fox) 861 self.foxes.discard(fox)
872 if fox in self.tv.sprites: 862 if fox in self.tv.sprites:
897 """Add chickens as specified by the code layer""" 887 """Add chickens as specified by the code layer"""
898 chick = animal.Chicken((tile.tx, tile.ty)) 888 chick = animal.Chicken((tile.tx, tile.ty))
899 self.add_chicken(chick) 889 self.add_chicken(chick)
900 890
901 def _choose_fox(self, (x, y)): 891 def _choose_fox(self, (x, y)):
902 fox_cls = misc.WeightedSelection(self.FOX_WEIGHTINGS).choose() 892 fox_cls = misc.WeightedSelection(self.level.fox_weightings).choose()
903 return fox_cls((x, y)) 893 return fox_cls((x, y))
904 894
905 def spawn_foxes(self): 895 def spawn_foxes(self):
906 """The foxes come at night, and this is where they come from.""" 896 """The foxes come at night, and this is where they come from."""
907 # Foxes spawn just outside the map 897 # Foxes spawn just outside the map
908 x, y = 0, 0 898 x, y = 0, 0
909 width, height = self.tv.size 899 width, height = self.tv.size
910 min_foxes = (self.days+3)/2 # always more than one fox 900 min_foxes = max(self.level.min_foxes, (self.days+3)/2) # always more than one fox
911 new_foxes = min(random.randint(min_foxes, min_foxes*2), self.max_foxes) 901 new_foxes = min(random.randint(min_foxes, min_foxes*2), self.max_foxes)
912 while len(self.foxes) < new_foxes: 902 while len(self.foxes) < new_foxes:
913 side = random.randint(0, 3) 903 side = random.randint(0, 3)
914 if side == 0: 904 if side == 0:
915 # top 905 # top
975 965
976 def is_game_over(self): 966 def is_game_over(self):
977 """Return true if we're complete""" 967 """Return true if we're complete"""
978 if self.trees_left() == 0: 968 if self.trees_left() == 0:
979 return True 969 return True
980 if self.max_turns > 0 and self.days >= self.max_turns: 970 if self.level.turn_limit > 0 and self.days >= self.level.turn_limit:
981 return True 971 return True
982 if len(self.chickens) == 0: 972 if len(self.chickens) == 0:
983 return True 973 return True
984 974
985 975