comparison gamelib/gameboard.py @ 109:48019afde338

Equipment purchasing and some toolbar tweaks.
author Jeremy Thurgood <firxen@gmail.com>
date Wed, 02 Sep 2009 18:46:10 +0000
parents 437cbd856a03
children 2b2007e231da
comparison
equal deleted inserted replaced
108:437cbd856a03 109:48019afde338
21 def update_value(self, value): 21 def update_value(self, value):
22 self.value = value 22 self.value = value
23 self.style.width, self.style.height = self.font.size(self.value) 23 self.style.width, self.style.height = self.font.size(self.value)
24 self.repaint() 24 self.repaint()
25 25
26 def mklabel(text=" ", color=constants.FG_COLOR):
27 return OpaqueLabel(text, color=color)
28
29 def mkcountupdate(counter):
30 def update_counter(self, value):
31 getattr(self, counter).update_value("%s" % value)
32 self.repaint()
33 return update_counter
26 34
27 class ToolBar(gui.Table): 35 class ToolBar(gui.Table):
28 def __init__(self, gameboard, **params): 36 def __init__(self, gameboard, **params):
29 gui.Table.__init__(self, **params) 37 gui.Table.__init__(self, **params)
30 self.gameboard = gameboard 38 self.gameboard = gameboard
31 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR) 39 self.cash_counter = mklabel()
32 self.chicken_counter = OpaqueLabel(" ", color=constants.FG_COLOR) 40 self.chicken_counter = mklabel()
33 self.killed_foxes = OpaqueLabel(" ", color=constants.FG_COLOR) 41 self.killed_foxes = mklabel()
34 42 self.rifle_counter = mklabel()
35 self.add_counter(None, self.cash_counter) 43
44 self.add_counter(mklabel("Groats:"), self.cash_counter)
36 self.add_counter(icons.CHKN_ICON, self.chicken_counter) 45 self.add_counter(icons.CHKN_ICON, self.chicken_counter)
37 self.add_counter(icons.KILLED_FOX, self.killed_foxes) 46 self.add_counter(icons.KILLED_FOX, self.killed_foxes)
38 47
39 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS) 48 self.add_tool_button("Move Animals", constants.TOOL_PLACE_ANIMALS)
40 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) 49 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
41 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) 50 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
42 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) 51 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING)
43 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE) 52 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
44 for building_cls in buildings.BUILDINGS: 53 for building_cls in buildings.BUILDINGS:
45 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls) 54 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls)
55 for equipment_cls in equipment.EQUIPMENT:
56 self.add_tool_button("Buy %s" % (equipment_cls.NAME,), equipment_cls)
46 self.add_spacer() 57 self.add_spacer()
47 self.add_button("Finished Day", self.day_done) 58 self.add_button("Finished Day", self.day_done)
48 59
49 def day_done(self): 60 def day_done(self):
50 import engine 61 import engine
51 pygame.event.post(engine.START_NIGHT) 62 pygame.event.post(engine.START_NIGHT)
52 63
53 def update_cash_counter(self, amount): 64 update_cash_counter = mkcountupdate('cash_counter')
54 self.cash_counter.update_value("Groats: %s" % amount) 65 update_fox_counter = mkcountupdate('killed_foxes')
55 self.repaint() 66 update_chicken_counter = mkcountupdate('chicken_counter')
56
57 def update_chicken_counter(self, number):
58 self.chicken_counter.update_value(" %s" % number)
59 self.repaint()
60
61 def update_fox_counter(self, number):
62 self.killed_foxes.update_value(" %s" % number)
63 self.repaint()
64 67
65 def add_spacer(self, height=30): 68 def add_spacer(self, height=30):
66 self.tr() 69 self.tr()
67 self.add(gui.Spacer(0, height)) 70 self.add(gui.Spacer(0, height))
68 71
183 self.buy_fence(self.tv.screen_to_tile(e.pos)) 186 self.buy_fence(self.tv.screen_to_tile(e.pos))
184 elif self.selected_tool == constants.TOOL_SELL_BUILDING: 187 elif self.selected_tool == constants.TOOL_SELL_BUILDING:
185 self.sell_building(self.tv.screen_to_tile(e.pos)) 188 self.sell_building(self.tv.screen_to_tile(e.pos))
186 elif buildings.is_building(self.selected_tool): 189 elif buildings.is_building(self.selected_tool):
187 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool) 190 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
191 elif equipment.is_equipment(self.selected_tool):
192 self.buy_equipment(self.tv.screen_to_tile(e.pos), self.selected_tool)
188 193
189 def get_chicken(self, tile_pos): 194 def get_chicken(self, tile_pos):
190 for chick in self.chickens: 195 for chick in self.chickens:
191 if chick.covers(tile_pos): 196 if chick.covers(tile_pos):
192 return chick 197 return chick
271 if self.cash < building.buy_price(): 276 if self.cash < building.buy_price():
272 return 277 return
273 if building.place(self.tv): 278 if building.place(self.tv):
274 self.add_cash(-building.buy_price()) 279 self.add_cash(-building.buy_price())
275 self.add_building(building) 280 self.add_building(building)
281
282 def buy_equipment(self, tile_pos, equipment_cls):
283 chicken = self.get_chicken(tile_pos)
284 equipment = equipment_cls()
285 if chicken is None or self.cash < equipment.buy_price():
286 return
287 if equipment.place(chicken):
288 self.add_cash(-equipment.buy_price())
289 chicken.equip(equipment)
276 290
277 def sell_building(self, tile_pos): 291 def sell_building(self, tile_pos):
278 if self.tv.get(tile_pos) == self.FENCE: 292 if self.tv.get(tile_pos) == self.FENCE:
279 return self.sell_fence(tile_pos) 293 return self.sell_fence(tile_pos)
280 building = self.get_building(tile_pos) 294 building = self.get_building(tile_pos)
388 # Fence 402 # Fence
389 roll = 10 403 roll = 10
390 if roll == 1: 404 if roll == 1:
391 # Create a chicken 405 # Create a chicken
392 chick = animal.Chicken((x, y)) 406 chick = animal.Chicken((x, y))
393 if random.randint(0, 1) == 0:
394 chick.equip(equipment.Rifle())
395 self.add_chicken(chick) 407 self.add_chicken(chick)
396 x += 1 408 x += 1
397 409
398 def spawn_foxes(self): 410 def spawn_foxes(self):
399 """The foxes come at night, and this is where they come from.""" 411 """The foxes come at night, and this is where they come from."""