comparison gamelib/gameboard.py @ 382:e89e6ad011ac

Use code layer to place chickens
author Neil Muller <drnlmuller@gmail.com>
date Mon, 26 Oct 2009 20:04:13 +0000
parents 7a58dadfd251
children 463802281182
comparison
equal deleted inserted replaced
381:7a58dadfd251 382:e89e6ad011ac
288 def __init__(self, main_app, max_turns): 288 def __init__(self, main_app, max_turns):
289 self.disp = main_app 289 self.disp = main_app
290 self.tv = tiles.FarmVid() 290 self.tv = tiles.FarmVid()
291 self.tv.png_folder_load_tiles('tiles') 291 self.tv.png_folder_load_tiles('tiles')
292 self.tv.tga_load_level(data.filepath('levels/farm.tga')) 292 self.tv.tga_load_level(data.filepath('levels/farm.tga'))
293 height, width = self.tv.size 293 width, height = self.tv.size
294 # Ensure we don't every try to create more foxes then is sane 294 # 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) 295 self.max_foxes = min(height+width-15, constants.ABS_MAX_NUM_FOXES)
296 self.max_turns = max_turns 296 self.max_turns = max_turns
297 self.create_display() 297 self.create_display()
298 298
309 self.add_cash(constants.STARTING_CASH) 309 self.add_cash(constants.STARTING_CASH)
310 self.day, self.night = True, False 310 self.day, self.night = True, False
311 311
312 self.fix_buildings() 312 self.fix_buildings()
313 313
314 self.add_some_chickens() 314 cdata = {
315 1 : (self.add_start_chickens, None),
316 }
317
318 self.tv.run_codes(cdata, (0,0,width,height))
315 319
316 def get_top_widget(self): 320 def get_top_widget(self):
317 return self.top_widget 321 return self.top_widget
318 322
319 def create_display(self): 323 def create_display(self):
887 891
888 def add_cash(self, amount): 892 def add_cash(self, amount):
889 self.cash += amount 893 self.cash += amount
890 self.toolbar.update_cash_counter(self.cash) 894 self.toolbar.update_cash_counter(self.cash)
891 895
892 def add_some_chickens(self): 896 def add_start_chickens(self, _map, tile, _value):
893 """Add some random chickens to start the game""" 897 """Add chickens as specified by the code layer"""
894 x, y = 0, 0 898 chick = animal.Chicken((tile.tx, tile.ty))
895 width, height = self.tv.size 899 self.add_chicken(chick)
896 tries = 0
897 while len(self.chickens) < constants.START_CHICKENS:
898 if x < width:
899 tile = self.tv.get((x, y))
900 else:
901 y += 1
902 if y >= height:
903 y = 0
904 tries += 1
905 if tries > 3:
906 break # Things have gone wierd
907 x = 0
908 continue
909 # See if we place a chicken
910 if 'grassland' == tiles.TILE_MAP[tile]:
911 # Farmland
912 roll = random.randint(1, 20)
913 # We don't place within a tile of the fence, this is to make things
914 # easier
915 for xx in range(x-1, x+2):
916 if xx >= width or xx < 0:
917 continue
918 for yy in range(y-1, y+2):
919 if yy >= height or yy < 0:
920 continue
921 neighbour = self.tv.get((xx, yy))
922 if 'fence' == tiles.TILE_MAP[neighbour]:
923 # Fence
924 roll = 10
925 if roll == 1:
926 # Create a chicken
927 chick = animal.Chicken((x, y))
928 self.add_chicken(chick)
929 x += 1
930 900
931 def _choose_fox(self, (x, y)): 901 def _choose_fox(self, (x, y)):
932 fox_cls = misc.WeightedSelection(self.FOX_WEIGHTINGS).choose() 902 fox_cls = misc.WeightedSelection(self.FOX_WEIGHTINGS).choose()
933 return fox_cls((x, y)) 903 return fox_cls((x, y))
934 904